xml - XPath 选择:相对路径不适用于跟随兄弟
问题描述
我已经多次看到这个问题被问过,但方式略有不同。看看类似的例子,我仍然缺少一些东西。
使用以下信息,我正在尝试执行 XPath 1.0 选择以从带有帐户的 XML 中获取一组唯一的客户帐户值。使用模板、for-each 和 Muenchian 分组来执行 XSLT 是不可能的。
我有这个 XML:
<?xml version="1.0" encoding="utf-8"?>
<Envelope xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/Message">
<Body xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/Message">
<MessageParts xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/Message">
<ReportArchive xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/ReportArchive">
<Report class="entity">
<_DocumentHash>d1fd3992e1d6cde8fd06512cea125792</_DocumentHash>
<ReportSection class="entity">
<Name>AddressBody</Name>
<Type>Body</Type>
<ReportSectionField class="entity">
<Name>CustTable_AccountNum</Name>
<Value>0000000001</Value>
</ReportSectionField>
</ReportSection>
<ReportSection class="entity">
<Name>AddressBody</Name>
<Type>Body</Type>
<ReportSectionField class="entity">
<Name>CustTable_AccountNum</Name>
<Value>0000000001</Value>
</ReportSectionField>
</ReportSection>
<ReportSection class="entity">
<Name>AddressBody</Name>
<Type>Body</Type>
<ReportSectionField class="entity">
<Name>CustTable_AccountNum</Name>
<Value>0000000002</Value>
</ReportSectionField>
</ReportSection>
<ReportSection class="entity">
<Name>AddressBody</Name>
<Type>Body</Type>
<ReportSectionField class="entity">
<Name>CustTable_AccountNum</Name>
<Value>0000000003</Value>
</ReportSectionField>
</ReportSection>
<ReportSection class="entity">
<Name>AddressBody</Name>
<Type>Body</Type>
<ReportSectionField class="entity">
<Name>CustTable_AccountNum</Name>
<Value>0000000004</Value>
</ReportSectionField>
</ReportSection>
<ReportSection class="entity">
<Name>AddressBody</Name>
<Type>Body</Type>
<ReportSectionField class="entity">
<Name>CustTable_AccountNum</Name>
<Value>0000000005</Value>
</ReportSectionField>
</ReportSection>
</Report>
</ReportArchive>
</MessageParts>
</Body>
</Envelope>
使用这个 XPath:(//*[local-name()='ReportSectionField'][./*[local-name()='Name'] = 'CustTable_AccountNum']/*[local-name()='Value'])[not(. = following-sibling::*)]/text()
使用“()”,我的假设是相对路径选择将创建一个节点集,然后“not()”过滤器部分将解析结果集中的每个兄弟并返回唯一值。当我使用“following-sibling”轴时,这会失败,但“following”轴有效。我不想遍历后代,所以“跟随”不是我想要使用的轴。我错过了什么,有人可以帮我想象发生了什么吗?
其他需要注意的事项: - XPath 编译器是 BizTalk 使用的基于 .Net (1.0) 的(请参阅本文了解原因:XPath 和 XSLT 2.0 for .NET?)
解决方案
XPath 寻址源文档中的节点。因此,相对于所选value
元素,您仍然需要查看following::
轴,因为其他value
元素不是同级元素。
在选择要比较值的元素时,您可以将 XPath 调整得更具体一点,方法value
是“向上跳”到ReportSection
元素,然后查看following-sibling::*
满足相同选择标准的value
元素以查找要比较和过滤的元素:
//*[local-name() = 'ReportSectionField' and
*[local-name() = 'Name' and . = 'CustTable_AccountNum']]/
*[local-name() = 'Value' and
not(. = ../../following-sibling::*/
*[local-name() = 'ReportSectionField' and
*[local-name() = 'Name' and . = 'CustTable_AccountNum']]/
*[local-name() = 'Value']
)
]/text()
推荐阅读
- node.js - 错误:连接丢失:服务器关闭了连接
- c# - ViewBag 值未显示在选择框中?
- neo4j - Neo4j 密码查询,用于获取与节点的关系数,对于每个不同的标签
- postgresql - 如果 docker-compose 启动时不存在,则创建 postgres 数据库
- angular - 刷新浏览器窗口时不会编译对我的应用程序进行的更改
- c# - Mutex.WaitAll 和 Semaphore.WaitOne 同时
- pdf - PDFBox - 如何将 Xheight 转换为点
- asp.net-mvc - 如何使用实体和过滤器以及具有一百万行的分页列表进行查询
- ios - 协议中的可选函数未在 Xcode 10.2 中的子类上调用
- html - 我想从 ViewBag 为 Html.Partial(string partialViewName) 传递 partialViewName