XPath 从 XML 中获取元素值

XPath get element value from XML

提问人:user3441151 提问时间:7/25/2023 更新时间:8/14/2023 访问量:44

问:

我有下面的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>

我想获得 和 的值。请在这里帮忙。<sf:Id>005030000040AQeAAM</sf:Id><size>1</size>

xml xpath xpath-2.0 xpath-1.0

评论

0赞 Siebe Jongebloed 7/25/2023
有相同的元素是正确的吗?2sf:Id
0赞 user3441151 7/25/2023
是的,这是正确的,我们可以使用其中任何一个。
0赞 Yitzhak Khabinsky 7/25/2023
请编辑您的问题,并指定您正在使用的语言/环境。

答:

-1赞 Daniel Haley 7/26/2023 #1

您可以使用此 2.0 XPath...

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

评论

0赞 Daniel Haley 8/14/2023
不知道为什么除了选择元素而不是值之外,它还会得到反对票,所以我添加了 xpath。string()
0赞 Daniel Haley 8/14/2023
刚刚注意到我没有考虑默认命名空间。更新。
0赞 Dimitre Novatchev 8/14/2023 #2

只需使用以下 2 个 XPath 1.0 表达式

sf:Id

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

对于:size

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

基于 XSLT 1.0 的验证

此转换:

<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>

当应用于提供的 XML 文档时

<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>

计算 2 个 XPath 表达式并输出这些计算的结果 -- 我们得到正确的、想要的值

005030000040AQeAAM
1