首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XPath中特定元素的选择

XPath中特定元素的选择
EN

Stack Overflow用户
提问于 2016-05-15 12:11:02
回答 2查看 370关注 0票数 0

我有两个元素的同名“理由”。当我使用//*:reason/text()时,它给出了两个元素,但我需要第一个元素。(不是“细节”中的那个)。请帮忙..。

代码语言:javascript
复制
<xml xmlns:gob="http://osb.yes.co.il/GoblinAudit">
    <fault>
        <ctx:fault xmlns:ctx="http://www.bea.com/wli/sb/context">
            <ctx:errorCode>BEA-382500</ctx:errorCode>
            <ctx:reason>OSB Service Callout action received SOAP Fault response</ctx:reason>
            <ctx:details>
                <ns0:ReceivedFaultDetail xmlns:ns0="http://www.bea.com/wli/sb/stages/transform/config">
                    <ns0:faultcode xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">soapenv:Server</ns0:faultcode>
                    <ns0:faultstring>BEA-380001: Internal Server Error</ns0:faultstring>
                    <ns0:detail>
                        <con:fault xmlns:con="http://www.bea.com/wli/sb/context">
                            <con:errorCode>BEA-380001</con:errorCode>
                            <con:reason>Internal Server Error</con:reason>
                            <con:location>
                                <con:node>RouteTo_FinancialControllerBS</con:node>
                                <con:path>response-pipeline</con:path>
                            </con:location>
                        </con:fault>
                    </ns0:detail>
                </ns0:ReceivedFaultDetail>
            </ctx:details>
            <ctx:location>
                <ctx:node>PipelinePairNode2</ctx:node>
                <ctx:pipeline>PipelinePairNode2_request</ctx:pipeline>
                <ctx:stage>set maintain offer</ctx:stage>
                <ctx:path>request-pipeline</ctx:path>
            </ctx:location>
        </ctx:fault>
    </fault>
</xml>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-05-15 12:26:11

您正在使用//限定符,该限定符将下降到任何子树并查找所有出现的reason。您可以尝试更具体地说明子路径:

代码语言:javascript
复制
//fault/*:fault/*:reason/text()

这将只匹配外部reason,而不匹配内部reason.

票数 2
EN

Stack Overflow用户

发布于 2016-05-15 13:38:29

"...but我需要第一个“

您可以使用位置索引获得第一个匹配的reason元素:

代码语言:javascript
复制
(//*:reason)[1]/text()

“(不是”细节“中的那个)

上面的内容可以表示为查找没有祖先reasondetails元素:

代码语言:javascript
复制
//*:reason[not(ancestor::*:details)]/text()

对于大型XML文档,使用更具体的路径,即在开始时避免使用//,将产生更高效的XPath:

代码语言:javascript
复制
/xml/fault/*:fault/*:reason/text()

但是对于一个小型的XML来说,这只是个人偏好的问题,因为改进可能是微不足道的。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37238135

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档