XPath: How to check multiple attributes across similar nodes
If I have some xml l开发者_开发知识库ike:
<root>
<customers>
<customer firstname="Joe" lastname="Bloggs" description="Member of the Bloggs family"/>
<customer firstname="Joe" lastname="Soap" description="Member of the Soap family"/>
<customer firstname="Fred" lastname="Bloggs" description="Member of the Bloggs family"/>
<customer firstname="Jane" lastname="Bloggs" description="Is a member of the Bloggs family"/>
</customers>
</root>
How do I get, in pure XPath - not XSLT - an xpath expression that detects rows where lastname is the same, but has a different description? So it would pull the last node above?
How do I get, in pure XPath - not XSLT - an xpath expression that detects rows where lastname is the same, but has a different description?
Here's how to do this with a single XPath expression:
"/*/*/customer
[@lastname='Bloggs'
and
not(@description
= preceding-sibling::*[@lastname='Bloggs']/@description
)
]"
This expression selects all <customer>
elements with attribute lastname
equal to "Bloggs" and different value of the attribute description
.
The selected nodes are:
<customer firstname="Joe" lastname="Bloggs" description="Member of the Bloggs family"/>
<customer firstname="Jane" lastname="Bloggs" description="Is a member of the Bloggs family"/>
/root/customers/customer[@lastname='Bloggs'
and not(@description = preceding-sibling::*[@lastname='Bloggs']/@description)
and not(@description = following-sibling::*[@lastname='Bloggs']/@description)]
It would perform better doing it in steps, though.
精彩评论