6

Do XML::LibXML::Node::find and related methods guarantee that the list of nodes will always be ordered as in the XML document?

This is important for me because my document corresponds to a big array in which I want to be able to delete series of items in certain circumstances, and I must make sure something like this works consistently:

my @nodes = $dom->find('//MyElement[@attr=something]/descendant::Token/@id')
                ->get_nodelist;
for my $token ( reverse map { $_->value } @nodes ) {
    splice @my_big_array, $token, 1;
}

The difficulty is that this is not documented in XML::LibXML, and I don't know whether this depends on libxml2 implementation, whose documentation I don't really understand, or on the DOM standard or some other W3C standard, which were clearly not written to be read my mere mortals.

scozy
  • 2,511
  • 17
  • 34
  • 1
    Short Answer: Yes. It's almost so obvious a requirement, that it goes without saying. But given XPath is ordered, any module that implements XPath would have to follow that requirement as well. So yes to `XML::Twig`, `XML::LibXML`. And for separate reasons, but heck even `XML::Simple`. – Miller May 28 '14 at 17:39

2 Answers2

2

To my best knowledge the current XML standard does care about the order of the nodes and the current implementaion of XML::LibXML will give you back the same order every time.

See more: In XML, is order important?, Does XML care about the order of elements?

Good reading about this: http://www.ibm.com/developerworks/xml/library/x-eleord/index.html

1: Its needs to be ordered some way so each run of the same parser should return the same result. 99% of parsers uses the order in xml file the remaining ones orders by the order in the schema

The XML Information Set (InfoSet -- see Resources), the core XML data model defined by the W3C, characterizes element children as: An ordered list of child information items, in document order.

2: The ordering could enforced or removed

The ampersand (&) characters between the sibling element subpatterns indicate that any order is acceptable.

element memo {
  element title { text } &
  element date { text } &
  element from { text } &
  element to { text } &
  element body { text }
}
Community
  • 1
  • 1
user1126070
  • 5,059
  • 1
  • 16
  • 15
1

I essentially answered this in a related question. XML::LibXML calls xmlXPathCompile from libxml2 which makes sure that the resulting node-set will be sorted in document order.

Community
  • 1
  • 1
nwellnhof
  • 32,319
  • 7
  • 89
  • 113