user3441151
user3441151

Reputation: 1910

XPath get element value from XML

I have below XML

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="urn:partner.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sf="urn:sobject.partner.soap.sforce.com">
    <soapenv:Header>
        <LimitInfoHeader>
            <limitInfo>
                <current>142795</current>
                <limit>49754000</limit>
                <type>API REQUESTS</type>
            </limitInfo>
        </LimitInfoHeader>
    </soapenv:Header>
    <soapenv:Body>
        <queryAllResponse>
            <result xsi:type="QueryResult">
                <done>true</done>
                <queryLocator xsi:nil="true"/>
                <records xsi:type="sf:sObject">
                    <sf:type>User</sf:type>
                    <sf:Id>005030000040AQeAAM</sf:Id>
                    <sf:Id>005030000040AQeAAM</sf:Id>
                </records>
                <size>1</size>
            </result>
        </queryAllResponse>
    </soapenv:Body>
</soapenv:Envelope>

I want to get the value of <sf:Id>005030000040AQeAAM</sf:Id> and <size>1</size>. Please help here.

Upvotes: 0

Views: 48

Answers (2)

Daniel Haley
Daniel Haley

Reputation: 52848

You could use this 2.0 XPath...

//*:queryAllResponse/*:result/(*:size|*:records/*:Id[1])/string()

Upvotes: -1

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243459

Just use these 2 XPath 1.0 expressions:

For sf:Id

string((//*[name() = 'sf:Id'])[1])

And for size:

string((//*[name() = 'size'])[1])

XSLT 1.0 - based verification:

This transformation:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:template match="/">
    <xsl:copy-of select="string((//*[name() = 'sf:Id'])[1])"/>
    <xsl:value-of select="'&#xA;'"/>
    <xsl:copy-of select="string((//*[name() = 'size'])[1])"/>
  </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="urn:partner.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sf="urn:sobject.partner.soap.sforce.com">
    <soapenv:Header>
        <LimitInfoHeader>
            <limitInfo>
                <current>142795</current>
                <limit>49754000</limit>
                <type>API REQUESTS</type>
            </limitInfo>
        </LimitInfoHeader>
    </soapenv:Header>
    <soapenv:Body>
        <queryAllResponse>
            <result xsi:type="QueryResult">
                <done>true</done>
                <queryLocator xsi:nil="true"/>
                <records xsi:type="sf:sObject">
                    <sf:type>User</sf:type>
                    <sf:Id>005030000040AQeAAM</sf:Id>
                    <sf:Id>005030000040AQeAAM</sf:Id>
                </records>
                <size>1</size>
            </result>
        </queryAllResponse>
    </soapenv:Body>
</soapenv:Envelope>

evaluates the 2 XPath expressions and outputs the results of these evaluations -- and we get the correct, wanted values:

005030000040AQeAAM
1

Upvotes: 0

Related Questions