in reply to Re^2: XML::Twig replace method behaving counter-intuitively
in thread XML::Twig replace method behaving counter-intuitively

If you try that code you will see that it does preserve the ordering. While XML::Rules does use hashes most of the time, it' really up to you what data do you need to preserve from what tag and how. Using that code the <tweak> tags' data end up in the array referenced by $_[1]->{_content} within the rule specified for the <tcf> tag. How are the data from a tag available within the $attr hashref of the parent tag depends on the tag's rule.

Replies are listed 'Best First'.
Re^4: XML::Twig replace method behaving counter-intuitively
by Human (Initiate) on Dec 03, 2007 at 20:27 UTC
    Ok, so it looks like ordering would not be a problem in some cases. What about DTD support and ENTITY references? Do they work? And does the toXML method preserve ordering? I appreciate your replies, but I was also hoping to find out why my usage of XML::Twig was not working. Any ideas about that?

      XML::Rules is built on top of XML::Parser, I do not try to parse the XML, load the DTDs and all that myself. So yes DTD and ENTITY references should work just fine.

      The toXML() preserves ordering whereever the data stucture does. So if the datastructure contains something like

      ... foo => { name => "Jenda", address => 'Jenda@Krynicky.cz', bar => {_content => 'hello'}, ban => {_content => 'hi'}, },
      then of course the hash defining the contents of the foo tag doesn't keep the order so the attributes and the subtags are printed in alphabetical order:
      <foo address="Jenda@Krynicky.cz" name="Jenda"><ban>hi</ban><bar>hello< +/bar></foo>
      If on the other hand you include the subtags in the _content like this:
      ... foo => { name => "Jenda", address => 'Jenda@Krynicky.cz', _content => [ "\n ", [bar => {_content => 'hello'}], "\n ", [ban => {_content => 'hi'}], "\n" ], },
      you end up with this:
      <foo address="Jenda@Krynicky.cz" name="Jenda"> <bar>hello</bar> <ban>hi</ban> </foo>

      Same way if you repeat the child tag for example like this

      foo => { name => 'Jenda', sibling => ['Pavel', 'Martin', 'Hana'], }
      and end up with
      <foo name="Jenda"><sibling>Pavel</sibling><sibling>Martin</sibling><si +bling>Hana</sibling></foo>

      I'm sorry, I haven't used XML::Twig for quite long and when I was using it, I just extracted data, I've never tried to use it as a filter.