开发者

Sorting XPath expressions into document order

I need to evaluate multiple XPath expressions against a document. The documents are large, so we're using a streaming parser. The XPath expressions simply return the value of a node. I need to order the XPath expressions so that they are in document order (since once I evaluate an expression, evaluatio开发者_Go百科n of the next expression begins at the point in the document where the last value was found). I have the schema for the document, but I'm unsure how to use it to establish the order.

I'm using C# with .NET 4, if that matters. If someone could point me in the right direction, I'd appreciate it!

FOLLOWUP: As Dimitre noted, this is not possible with full XPath. Restricting expressions to the XML Signature Streaming Profile, I was able to construct a tree, with each node containing the element name and an ordinal position (the order in which it appeared within the schema). Once I had the tree built, I then traversed it with the XPath, collecting the positions in a list. Once I had the list of positions, it was easy to compare the lists and determine the relative positions.


This is not achievable without posing some limitations on the XPath expressions.

For example, any XPath expression that contains the axes descendant::, descendant-or-self::, ancestor::, ancestor-or-self::, preceding::, following:: and even the axes preceding-sibling:: and following-sibling:: -- may require reading the XML document completely.

In case the XPath expressions don't contain these axes, you could arrange them by the number of location steps (they also shouldn't contain parent:: or ..), and you can temporarily stop evaluating any expression with number of location paths smaller than the current depth.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜