Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working With Nested XPath Predicates ... Refined

Tags:

xpath

All great answers! But the question deserves refinement ...

I've got the following sample XML ...

<objects>
    <object objectId="1123" ... />
    <properties refObjectId="1123" ... />
    <properties refObjectId="1123" refPropertyId="2311" ... />
    <properties refObjectId="1123" refPropertyId="4611" ... />
    <object objectId="2123" ... />
    <properties refObjectId="2123" refPropertyId="4311" ... />
    <properties refObjectId="2123" refPropertyId="8611" ... />
    ....
</objects>

... and the following XPath query ...

//object[//properties[@refObjectId=@objectId and not(@refPropertyId)]]

I thought this query would return all object nodes where there is a properties node that has a refObjectId attribute that equals the objectId attribute of the object node and no 'refPropertyId' attribute ... namely object 1123 only, not object 2123 ... but it doesn't. It seems the @objectId in the nested predicate does not refer to the objectId attribute of the object node.

Any ideas? I know the XML structure isn't nested as you would expect, but there are reasons for this structure.

like image 326
Frank Avatar asked Jun 08 '09 04:06

Frank


1 Answers

Generally you should avoid using // where you can. I'd consider rephrasing:

//object[../properties/@refObjectId=@objectId]

In the expression provided, your nested predicate is actually checking for

//properties/@refObjectId=//properties/@objectId 

of which there are none.

I hope this helps!

EDIT: Since the question has been updated here is an updated response: You added "It seems the @objectId in the nested predicate does not refer to the objectId attribute of the object node." You're absolutely right! So let's fix it!!

//object[../properties[not(@refPropertyId)]/@refObjectId=@objectId]

This should be closer to what you're after!

like image 158
Jon W Avatar answered Oct 21 '22 03:10

Jon W