我有这个XML:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<ExtractResponse xmlns="http://tempuri.org/">
<ExtractResult xmlns:a="http://schemas.datacontract.org/2004/07/Sunstein.Services.WebServiceClient.ClientMatter" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:ClientMatterNumber>
<a:ClientNumber>00005</a:ClientNumber>
<a:MatterNumber>00003</a:MatterNumber>
</a:ClientMatterNumber>
<a:ClientMatterNumber>
<a:ClientNumber>01234</a:ClientNumber>
<a:MatterNumber>00101</a:MatterNumber>
</a:ClientMatterNumber>
<a:ClientMatterNumber>
<a:ClientNumber>01234</a:ClientNumber>
<a:MatterNumber>01234</a:MatterNumber>
</a:ClientMatterNumber>
</ExtractResult>
</ExtractResponse>
</s:Body>
</s:Envelope>
Run Code Online (Sandbox Code Playgroud)
我正在尝试构建XPath以最终引用其中一个ClientMatterNumber
节点,但在构建它时,我遇到了麻烦.例如,这有效:
/s:Envelope/s:Body
Run Code Online (Sandbox Code Playgroud)
但这不会返回任何结果:
/s:Envelope/s:Body/ExtractResponse
Run Code Online (Sandbox Code Playgroud)
我错过了什么?我已经尝试了许多在线XPath测试人员,包括这个,但他们都导致了相同的行为.
ExtractResponse
文档中的元素具有名称空间uri http://tempuri.org/
.
XPath表达式中任何未加前缀的位置步骤(例如 ExtractResponse
表达式中)都没有名称空间uri,因此无法将元素与URI匹配.
要解决此问题,您需要声明命名空间映射(例如x=http://tempuri.org/
),将其传递给XPath引擎并相应地修改表达式(例如/s:Envelope/s:Body/x:ExtractResponse
).
注意:您提到的在线XPath测试程序是一个很好的工具,但对XML命名空间的支持有限,因此您无法使用该工具成功运行表达式.