How to use XPath to select several possible text values?

I need to select rating codes from a rating tag similar to the one below, but only when the agent is โ€œSPโ€ or โ€œSNPโ€. Right now I have:

./ratings/rating/agency[text()='SNP'|text()='SP']/../code

This does not seem to work. 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>

Thank,

Jared

+5
source share
2 answers

The main thing is the union operator |, which you tried to use as "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 the sequence:

ratings/rating[agency = ('SNP', 'SP')]/code
+9
source

Use orinstead|

./ratings/rating/agency[text()='SNP' or text()='SP']/../code
+1
source

All Articles