libxml xmlXPathEvalExpression order

2019-08-05 15:06发布

问题:

I've started using libxml in C, and I'm using the xmlXPathEvalExpression function to evaluate XPath.

My XML file actually represents a table, with each child node representing a row in that table and its attributes are the corresponding values, so the order is important.

I couldn't find information about that function in regards to its order. Does it return the nodes in the document order

for example, the following xml file:

<?xml version="1.0" encoding="UTF-8"?>
<Root>
    <TABLE0>
      <Row Item0="1" Item1="2"/>
      <Row Item0="2" Item1="3"/>
      <Row Item0="3" Item1="4"/>
    </TABLE0>
    <TABLE1>
      <Row Item0="1" Item1="12"/>
      <Row Item0="6" Item1="15"/>
    </TABLE1>
</Root>

Can I be sure that after evaluating /Root/TABLE0/* and getting the nodeset, calling nodeSet->nodeTab[0] would get the first row, nodeSet->nodeTab[1] would get the second and so on?

If not, is there a way to sort it by document order?

Thanks

回答1:

Yes, the results of XPath expressions evaluated with xmlXPathEvalExpression or compiled with xmlXPathCompile are always sorted in document order. (Internally, they're compiled by calling xmlXPathCompileExpr with a true sort flag.)