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

Thanks for replying, Jenda! I haven't tried XML::Rules before, but the technical reason why I switched to XML::Twig from XML::Simple was that XML::Twig would preserve the order of entries. (I later learned it'd also do cool stuff like DTD processing and a few other things I don't need right away.) Since XML::Simple was hash-based, there's no guarantee that ordering is preserved. From a glance at the XML::Rules page on CPAN, it looks like it may use hashes, too. Is that the case? If so, then I may be unable to use such a solution. It's also possible that I'm missing some way to solve the ordering problem.
  • Comment on Re^2: XML::Twig replace method behaving counter-intuitively

Replies are listed 'Best First'.
Re^3: XML::Twig replace method behaving counter-intuitively
by Jenda (Abbot) on Dec 01, 2007 at 18:01 UTC

    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.

      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.