What XPath can I use to select any category with a name attribute specified and any child node author with the value specified.
I've tried different variations of the path below with no success:
//quotes/category[@name='Sport' and author="James Small"]
The XML:
<?xml version="1.0" encoding="utf-8"?> <quotes> <category name="Sport"> <author>James Small<quote date="09/02/1985">Quote One</quote><quote date="11/02/1925">Quote nine</quote></author> </category> <category name="Music"> <author>Stephen Swann <quote date="04/08/1972">Quote eleven</quote></author> </category> </quotes>
XPath multiple conditions are used to select multiple attributes, by using XPath in multiple conditions we can select any category with the attribute by specifying child node. We can define the condition in the path when we have to forward the message to more than one interface or more than one receiver.
Using OR & AND This means that any one condition should be true to find the element. In the below XPath expression, it identifies the elements whose single or both conditions are true. Highlight both elements as 'First Name' element having attribute 'id' and 'Last Name' element having attribute 'name'.
XPath expressions can use comparison operators. Comparison operators compare one value to another. The resulting value is a "boolean" value.
Use:
/category[@name='Sport' and author/text()[1]='James Small']
or use:
/category[@name='Sport' and author[starts-with(.,'James Small')]]
It is a good rule to try to avoid using the //
pseudo-operator whenever possible, because its evaluation can typically be very slow.
Also:
./somename
is equivalent to:
somename
so it is recommended to use the latter.
Try://category[@name='Sport' and ./author/text()='James Small']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With