0

I have 3 questions:

1) Is XPath string "//table[position()=8 or position()=10]/td[1]/span[2]/text()" faster than the XPath string "//table[8]/td[1]/span[2]/text() | //table[10]/td[1]/span[2]/text()"?

I use XPath with .NET CSharp and HTMLAgilityPack.

2) How can I determine what version of XPath I use. If I use XPath 1.0, how to upgrade to XPath 2.0?

3) Is there a performance optmimization and improvement into XPath 2.0 or just new features and new syntax?

user569008
  • 1,963
  • 2
  • 11
  • 10

2 Answers2

2

XPath 2.0 expands significantly on XPath 1.0 (read here for a summary), though you don't need to switch unless you would benefit from the new functionality.

As for which one would be faster I believe the first one would be faster because you're repeating the node search in the second case. The first case is also more readable, and in general you want to go with the more readable one anyways.

Ben G
  • 26,091
  • 34
  • 103
  • 170
1

As to the performance question, I'm afraid I don't know. It depends on the optimizer in the particular XPath processor you are using. If it's important to you, measure it. If it's not important enough to measure, then it's not important enough to worry about.

As I mentioned in my previous reply, //table[8] smells wrong to me. I think it's much more likely that you want (//table)[8]. (Both are valid XPath expressions, but they produce different answers).

You can probably assume that a processor is XPath 1.0 unless it says otherwise - if it supports 2.0, they'll want you to know. But you can easily test, for example by seeing what happens when you do //a except //b.

There's no intrinsic reason why an XPath 2.0 processor should be faster than a 1.0 processor on the same queries. In fact, it might be a bit slower, because it's required to do more careful type-checking. On the other hand it might be a lot faster, because many 1.0 processors were dashed off very quickly and never upgraded. But there are massive improvements in functionality in 2.0, for example regular expression support.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Michael, i want all table[8], so i use correctly "//" without brackets. How can i swith/upgrade to XPath 2.0? To check the version of XPath trying to run a function is what i did and it seems that my PC is with XPath 1.0. But i want to know if there's a valid way to check the version? – user569008 Jun 23 '11 at 09:49
  • I can't believe you can't answer my question #2 – user569008 Jun 24 '11 at 07:53
  • See Michael's answer at https://stackoverflow.com/questions/7951879/which-version-of-xpath-and-xslt-am-i-using for versioning information and an update for version 3. Also see Michael's answer at https://stackoverflow.com/questions/51374344/what-are-the-differences-between-versions-of-xpath-1-0-2-0-3-1 that explains the differences. – B5A7 Aug 22 '22 at 03:21