我有两个元素的同名“理由”。当我使用//*:reason/text()时,它给出了两个元素,但我需要第一个元素。(不是“细节”中的那个)。请帮忙..。
<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>发布于 2016-05-15 12:26:11
您正在使用//限定符,该限定符将下降到任何子树并查找所有出现的reason。您可以尝试更具体地说明子路径:
//fault/*:fault/*:reason/text()这将只匹配外部reason,而不匹配内部reason.。
发布于 2016-05-15 13:38:29
"...but我需要第一个“
您可以使用位置索引获得第一个匹配的reason元素:
(//*:reason)[1]/text()“(不是”细节“中的那个)
上面的内容可以表示为查找没有祖先reason的details元素:
//*:reason[not(ancestor::*:details)]/text()对于大型XML文档,使用更具体的路径,即在开始时避免使用//,将产生更高效的XPath:
/xml/fault/*:fault/*:reason/text()但是对于一个小型的XML来说,这只是个人偏好的问题,因为改进可能是微不足道的。
https://stackoverflow.com/questions/37238135
复制相似问题