Xpath表达式仅检索所有子节点的值

时间:2016-08-03 11:08:04

标签: xml xpath xml-parsing

我需要一个XPath表达式,以便从以下示例XML脚本中仅检索 BalanceResult 属性的特定值:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
    <ars:QueryBalanceResultMsg xmlns:ars="http://www.huawei.com/bme/cbsinterface/arservices" xmlns:arc="http://cbs.huawei.com/ar/wsservice/arcommon" xmlns:cbs="http://www.huawei.com/bme/cbsinterface/cbscommon">
        <QueryBalanceResult>
            <ars:AcctList>
                <ars:BalanceResult>
                    <arc:BalanceType>C_MAIN_ACCOUNT</arc:BalanceType>
                    <arc:BalanceTypeName>MainAccount</arc:BalanceTypeName>
                    <arc:TotalAmount>80000</arc:TotalAmount>
                    <arc:BalanceDetail>
                        <arc:BalanceInstanceID>702000000000008916</arc:BalanceInstanceID>
                        <arc:EffectiveTime>20160803080942</arc:EffectiveTime>
                        <arc:ExpireTime>20370101010000</arc:ExpireTime>
                    </arc:BalanceDetail>
                </ars:BalanceResult>
                <ars:BalanceResult>
                    <arc:BalanceType>C_Bonus_Account</arc:BalanceType>
                    <arc:BalanceTypeName>Bonus Balance Account</arc:BalanceTypeName>
                    <arc:TotalAmount>3900</arc:TotalAmount>
                    <arc:BalanceDetail>
                        <arc:BalanceInstanceID>702000000000008543</arc:BalanceInstanceID>
                        <arc:EffectiveTime>20160803082506</arc:EffectiveTime>
                        <arc:ExpireTime>20191211121212</arc:ExpireTime>
                    </arc:BalanceDetail>
                </ars:BalanceResult>
               .......
            </ars:AcctList>
        </QueryBalanceResult>
    </ars:QueryBalanceResultMsg>
</soapenv:Body>

这样我的表达式返回如下内容:

C_MAIN_ACCOUNT,80000,20370101010000,C_Bonus_Account,3900,20191211121212,... 上面的模板:(arc:BalanceType,arc:TotalAmount,arc:ExpireTime)

下面的表达式会返回所有内容,包括不需要的标记。

//*[local-name()='BalanceResult'][*[local-name()='BalanceType']]

此外,由于可能有n个 BalanceResult 属性(在上面的示例中为2),我无法提出解决方案。

感谢任何帮助!谢谢。

1 个答案:

答案 0 :(得分:2)

怎么样:

//BalanceResult/descendant::*[local-name() = 'BalanceType' or local-name() = 'TotalAmount' or local-name() = 'ExpireTime']

这导致:

<BalanceType>C_MAIN_ACCOUNT</BalanceType>
<TotalAmount>80000</TotalAmount>
<ExpireTime>20370101010000</ExpireTime>
<BalanceType>C_Bonus_Account</BalanceType>
<TotalAmount>3900</TotalAmount>
<ExpireTime>20191211121212</ExpireTime>

请注意,我从示例中删除了命名空间。