I have to select rating codes out of a ratings tag similar to the one below, but only when the agency is 'SP' or 'SNP'. Right now I have:
./ratings/rating/agency[text()='SNP'|text()='SP']/../code
This doesn't seem to be working though. What am I doing wrong?
<ratings>
<rating>
<agency>SP</agency>
<provider>SP</provider>
<type>LONG TERM</type>
<currencyType>LOCAL</currencyType>
<description>SP Standard LT LC rating</description>
<code>BBB+</code>
<date>2011-09-07</date>
</rating>
</ratings>
Thanks,
Jared
If an xpath refers multiple elements on the DOM, It should be surrounded by brackets first () and then use numbering. if the xpath refers 4 elements in DOM and you need 3rd element then working xpath is (common xpath)[3].
At the time of working XPath multiple attributes, we can use two or more attributes in a single class. We can utilize the distinct attribute available for only the tag or attribute combination and values for identifying the element, for the same we need to use the XPath multiple expression.
The main thing is the union operator |
which you've tried to use as an 'or'. Change it to or
:
./ratings/rating/agency[text()='SNP' or text()='SP']/../code
Or more naturally,
ratings/rating[agency[. = 'SNP' or . = 'SP']]/code
In XPath 2.0, you can use a sequence:
ratings/rating[agency = ('SNP', 'SP')]/code
Use or
instead of |
./ratings/rating/agency[text()='SNP' or text()='SP']/../code
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