Robin
Robin

Reputation: 605

Using XPath to grab the text of a specific tag

I am trying to extract the value from a specific key, in my XML, through XPath.


I have the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<OMRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <ApplicationId>User</ApplicationId>
    <BusinessProcessId>GiftCard</BusinessProcessId>
    <Data>
        <Item>
            <Key>fsf_addr</Key>
            <Value>Lancelot</Value>
        </Item>
        <Item>
            <Key>email_addr</Key>
            <Value>[email protected]</Value>
        </Item>
    </Data>
</OMRequest>

and I want to grab the value from the key that has email_addr via XPath, so I want an XPath that returns the value [email protected].

I have the following XPath, however, this will grab the first value:

/OMRequest/Data/Item/Value/text().

I am wondering what XPath I can use in order to get that specific value?

Upvotes: 2

Views: 39

Answers (2)

kjhughes
kjhughes

Reputation: 111541

A little late, but a bit more concise and complete...

This XPath,

string(/OMRequest/Data/Item[Key="email_addr"]/Value)

will select the string value of the Value element associated with the given Key element.

Replace string() with normalize-space() and Key with normalize-space(Key) if you wish to return and compare space-normalized values rather than exact string values.

Upvotes: 1

LMC
LMC

Reputation: 12672

This XPath should do

/OMRequest/Data/Item[Key[.="email_addr"]]/Value/text()

Upvotes: 1

Related Questions