0

Simple task. Here's the XPath expression:

/methods/method[@token = '06000009']/sequencePoints/entry[string(@ilOffset) <= string('00000e')][last()]

And here's a part of the XML document to query:

<methods>
  <method token="06000009">
    <sequencePoints>
      <entry ilOffset="000000" startLine="178" startColumn="3" />
      <entry ilOffset="000001" startLine="179" startColumn="4" />
      <entry ilOffset="000008" startLine="180" startColumn="4" />
      <entry ilOffset="000009" startLine="181" startColumn="5" />
      <entry ilOffset="00000f" startLine="182" startColumn="4" />
      <entry ilOffset="000010" hidden="true" />
      <entry ilOffset="000022" hidden="true" />
      <entry ilOffset="000023" startLine="183" startColumn="3" />
    </sequencePoints>
  </method>
</methods>

Finds nothing. But it should find this element:

<entry ilOffset="000009" ...>

Why doesn't it work?

I'm not sure whether I need the string() conversion. But without it it doesn't work either. I just don't want XPath to compare numbers because that can go wrong with hex values.

ygoe
  • 18,655
  • 23
  • 113
  • 210
  • Are you using xpath 1.0 or 2.0? If 1.0 then it explains why its not working. – nwill001 May 29 '14 at 18:49
  • AFAIK, .NET only supports XPath 1.0, but I'm not sure. I also test here: http://www.xpathtester.com/xpath It has the same result. – ygoe May 29 '14 at 19:03
  • Ok. Looks like Xpath 1.0 does not have support for hexadecimal whereas xpath 2.0 does support it. Your xpath works when set to xpath 2.0 as well. – nwill001 May 29 '14 at 19:08
  • But I want plain string comparison, not some number magic. It seems to work with decimal numbers, just skipping every node where the attribute doesn't look like a number, finding the wrong node. – ygoe May 29 '14 at 19:11
  • Sorry, your title threw me off. The only xpath 1.0 comparison operators that work for string() is = and !=. – nwill001 May 29 '14 at 19:19
  • Yeah, just found that out, too. Microsoft doesn't want to implement XPath 2.0 because they consider it too complicated. Stupid. http://stackoverflow.com/q/1525299/143684 And nobody knows how to use the `ms:string-compare` function. – ygoe May 29 '14 at 19:33

1 Answers1

1

XPath 2.0:

Yes. In your scenario you are comparing two strings. This is legal to do and should work fine. If its not working then you are most likely using xpath 1.0.

XPath 1.0:

Not really. It can only compare strings with the = and != operators.

nwill001
  • 705
  • 5
  • 6
  • Okay. Using the `ms:string-compare` function - as advertised in the MSDN library - is not possible, so I need to change my XML document to decimal numbers. Will be unusable for manual inspection but at least it should work in the program. :-( – ygoe May 29 '14 at 19:49