AlbertoGiorgino
AlbertoGiorgino

Reputation: 21

Get only attribute value using xpath

I have following xml code:

<?xml version="1.0" encoding="UTF-8"?>
    <party date="31.12.01"> 
        <guest name="Albert">
            <drink>wine</drink>
            <drink>beer</drink>
            <status single="true" sober="false" />
        </guest>
        <guest name="Martina">
            <drink>apple juice</drink>
            <status single="true" sober="true" />
        </guest>
        <guest name="Zacharias"> 
            <drink>wine</drink>
            <status single="false" sober="false" /> 
        </guest>
    </party>

Using xpath I just want the name of the guests that drink wine or beer. So I just need the value of the name attribute.

I have tried following:

string(//guest/@name/drink[.="wine" or .="beer"])

//guest/@name/drink[.="wine" or .="beer"]

But I don't get just the attribute value of the name attribute. How do I do that?

Upvotes: 0

Views: 48

Answers (2)

kjhughes
kjhughes

Reputation: 111541

This XPath,

//guest[drink = "wine" or drink = "beer"]/@name

selects the name attributes of those guest elements that have drink child elements with "wine" or "beer" string values, as requested.

Upvotes: 1

Gilles Qu&#233;not
Gilles Qu&#233;not

Reputation: 185106

What you need :

'//guest[drink[text()="wine" or text()="beer"]]/@name'

or

'//guest[drink[.="wine" or .="beer"]]/@name'

Upvotes: 2

Related Questions