Brenden Kehren
Brenden Kehren

Reputation: 6117

XPath Select OR with multiple strings

I have the following XML I need to get all the products with DESCR of '30 year fixed rate', '15 year fixed rate' and 'Biweekly 30 year fixed'.

<FEATURED_RATES>
<RATE_UPLOAD_DATE>03/19/2015 11:22 AM CT</RATE_UPLOAD_DATE>
<GROUPS>
    <GROUP>
        <NAME>FIXED RATE PRODUCTS</NAME>
        <PRODUCT>
            <DESCR>30 Year Fixed Rate</DESCR>
            <RATE>3.75</RATE>
            <APR>3.77</APR>
            <POINTS>0</POINTS>
            <PAYMENT_STREAM_URL></PAYMENT_STREAM_URL>
        </PRODUCT>
        <PRODUCT>
            <DESCR>20 Year Fixed Rate</DESCR>
            <RATE>3.625</RATE>
            <APR>3.653</APR>
            <POINTS>0</POINTS>
            <PAYMENT_STREAM_URL>
            </PAYMENT_STREAM_URL>
        </PRODUCT>
        <PRODUCT>
            <DESCR>15 Year Fixed Rate</DESCR>
            <RATE>3</RATE>
            <APR>3.036</APR>
            <POINTS>0</POINTS>
            <PAYMENT_STREAM_URL>
            </PAYMENT_STREAM_URL>
        </PRODUCT>
    </GROUP>
    <GROUP>
        <NAME>BIWEEKLY FIXED RATE PRODUCTS</NAME>
        <PRODUCT>
            <DESCR>Biweekly 30 Year Fixed</DESCR>
            <RATE>3.75</RATE>
            <APR>3.763</APR>
            <POINTS>0</POINTS>
            <PAYMENT_STREAM_URL>
            </PAYMENT_STREAM_URL>
        </PRODUCT>
        <PRODUCT>
            <DESCR>Biweekly 15 Year Fixed</DESCR>
            <RATE>3</RATE>
            <APR>3.031</APR>
            <POINTS>0</POINTS>
            <PAYMENT_STREAM_URL>
            </PAYMENT_STREAM_URL>
        </PRODUCT>
    </GROUP>
</GROUPS>

I've tried as suggested in other posts and a single string works and brings back the information I expect:

//FEATURED_RATES/GROUPS/GROUP/PRODUCT[DESCR = '30 Year Fixed Rate']

But I'm unable to get multiple OR statements to return any data without an error:

//FEATURED_RATES/GROUPS/GROUP/PRODUCT[DESCR=('30 Year Fixed Rate') OR DESCR=('15 Year Fixed Rate')]

I receive an error stating //FEATURED_RATES/GROUPS/GROUP/PRODUCT[DESCR=('30 Year Fixed Rate') OR DESCR=('15 Year Fixed Rate')] has an invalid token. What token is invalid or what syntax do I have incorrect?

Upvotes: 2

Views: 106

Answers (2)

JLRishe
JLRishe

Reputation: 101680

The invalid token in this case is the OR. It should be or. The parentheses around your string values are superfluous, but they are not a problem. The correct version of your attempt would be:

//FEATURED_RATES/GROUPS/GROUP/PRODUCT[DESCR='30 Year Fixed Rate' or DESCR='15 Year Fixed Rate']

If you had to match DESCR against several strings, it would be repetitive to have to use DESCR= over and over. An alternative you can use in that case would be:

//FEATURED_RATES/GROUPS/GROUP/PRODUCT[DESCR[.='30 Year Fixed Rate' or .='15 Year Fixed Rate']]

Upvotes: 1

kjhughes
kjhughes

Reputation: 111551

Make these corrections to your XPath expression:

  1. Move the opening parentheses to the beginning of their respective expressions.
  2. Change OR to or (lower-case).

Altogether:

//FEATURED_RATES/GROUPS/GROUP/PRODUCT[(DESCR='30 Year Fixed Rate') or (DESCR='15 Year Fixed Rate')]

Then given your XML, the above XPath expression will select

  <PRODUCT>
    <DESCR>30 Year Fixed Rate</DESCR>
    <RATE>3.75</RATE>
    <APR>3.77</APR>
    <POINTS>0</POINTS>
    <PAYMENT_STREAM_URL/>
  </PRODUCT>

  <PRODUCT>
    <DESCR>15 Year Fixed Rate</DESCR>
    <RATE>3</RATE>
    <APR>3.036</APR>
    <POINTS>0</POINTS>
    <PAYMENT_STREAM_URL>
    </PAYMENT_STREAM_URL>
  </PRODUCT>

Alternatively, the following XPath 2.0 expression would work too:

//FEATURED_RATES/GROUPS/GROUP/PRODUCT[DESCR = ('30 Year Fixed Rate', '15 Year Fixed Rate')]

Upvotes: 2

Related Questions