tags:

views:

368

answers:

1

Head hurts on this one so can someone throw me a line?

XML is like so (not the real stuff but this is an example for clarity):

<root>
    <groups name="A">
     <entry from="1" to="10" />
    </groups>
    <groups name="A">
     <entry from="11" to="20" />
     <entry from="21" to="30" />
    </groups>
    <groups name="A">
     <entry from="31" to="40" />
     <entry from="41" to="50" />
     <entry from="51" to="60" />
    </groups>  
</root>

Given a entry number I want to find the entry node that this number falls between. There should only be one node that matches (if any).

Heres the xpath I'm using:

/root/groups/entry[@from >= 45 and @to <= 45]

I've tried wrapping the attributes in number() too but still no luck.

This is not returning what I'd expect, infact it returns nothing and I can't see why! What am I missing?

Edit: Corrected my typo of the opening groups tags...sorry.

+2  A: 

That particular expression is returning nothing because there are no entry elements that satisfy the condition. First of all I'd suggest changing it to:

/root/groups/entry[@from <= 45 and @to >= 45]

Plus if that really is your source XML, it won't work because it's invalid. You have <group> open tags, but </groups> closing tags. They don't match. You should change them all to either group or groups, and your XPath expression should reflect that.

I tested that expression against a modified version of your XML (with group changed to groups), and it returned one result as expected:

<entry from="41" to="50" />
Welbog
DOH! Of course wrong comparisions! Thank you so much, thats been winding me up for an hour. Oh the shame...
Pete Duncanson
What about: `/root/groups/entry[@from <= 45 and 45 <= @to ]`? Much clearer, IMHO.
Tomalak