Xml XPath替换FOR语句
我有这个XPath(感谢Ian Roberts在SO中的回答): XPath很棒,但我使用的工具不接受Xml XPath替换FOR语句,xml,xpath,Xml,Xpath,我有这个XPath(感谢Ian Roberts在SO中的回答): XPath很棒,但我使用的工具不接受for语句 我如何重写这个XPath来避免这个问题[没有for语句]?假设您的输入是有序的(这很重要!),您可以通过发出以下XPath 1.0表达式来完成: 第一组: //Properties[ Property/@Descriptor = 200 and not( Property = preceding-sibling::*/Property[@Descriptor = 20
for
语句
我如何重写这个XPath来避免这个问题[没有
for
语句]?假设您的输入是有序的(这很重要!),您可以通过发出以下XPath 1.0表达式来完成:
第一组:
//Properties[
Property/@Descriptor = 200
and not(
Property = preceding-sibling::*/Property[@Descriptor = 200]
)
]
//Properties[
Property/@Descriptor = 200
and not(
Property = following-sibling::*/Property[@Descriptor = 200]
)
]
最后一组:
//Properties[
Property/@Descriptor = 200
and not(
Property = preceding-sibling::*/Property[@Descriptor = 200]
)
]
//Properties[
Property/@Descriptor = 200
and not(
Property = following-sibling::*/Property[@Descriptor = 200]
)
]
您可以从那里开始,分别选择Property[@Descriptor=100]
或Property[@Descriptor=200]
可以将两个表达式组合为一个:
//Properties[
Property/@Descriptor = 200 and (
not(
Property = preceding-sibling::*/Property[@Descriptor = 200]
) or not(
Property = following-sibling::*/Property[@Descriptor = 200]
)
)
]
您将得到一个
对的列表(它们各自的组中的第一个和最后一个),只有一个元素长的组除外。这意味着无法保证您不会返回偶数个节点。那么您使用的工具是什么?一个专有的报告工具。。。老实说,没有与Xpath相关的规范。只使用XPath作为选择XML文档中节点的一种方式。因此我假设您需要XPath 1.0表达式,因为如果您的工具使用的是2.0,那么它可能不够现代。请注意,我已经稍微简化了我的答案。