ygoe
ygoe

Reputation: 20374

Can XPath compare hexadecimal attribute values?

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.

Upvotes: 0

Views: 643

Answers (1)

nwill001
nwill001

Reputation: 745

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.

Upvotes: 1

Related Questions