Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does "find" return nodes in order?

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.

like image 830
scozy Avatar asked May 28 '14 12:05

scozy


2 Answers

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 }
}
like image 107
user1126070 Avatar answered Sep 28 '22 04:09

user1126070


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.

like image 32
nwellnhof Avatar answered Sep 28 '22 03:09

nwellnhof