cp5
cp5

Reputation: 1133

Determine if XML Element with specific value is present using XPATH

I googled the following but I don't get an aswer. Maybe my wording for this is not correct. I cannot believe that I am the only one who is trying to do this. Appologies if this is an easy question

I have an XML like this

<responseMessage>
<list>
    <reason>RC1</reason>
    <value>
        <id>1</id>          
    </value>

</list>
<list>
    <reason>RC2</reason>
    <value>
        <id>2</id>
    </value>
</list>
<list>
    <reason>RC3</reason>
    <value>
        <id>3</id>
    </value>
</list>
<list>
    <reason>RC4</reason>
    <value>
        <id>3</id>
    </value>
</list>

What XPath expression can I use to test if a List Element is present with sub child where the id of a value is 3? The order of the list is undeterminend and can be the first List item now and the 2nd time it can be last

Upvotes: 0

Views: 47

Answers (1)

Bas Leijdekkers
Bas Leijdekkers

Reputation: 26562

Something like

boolean(//list/value/id[text()='3'])

you mean? Here the boolean() function converts the result to true or false. An empty result is false and a result of one or more elements evaluates to true. Or even simpler:

//list/value/id/text()='3'

Upvotes: 1

Related Questions