Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml XPath替换FOR语句_Xml_Xpath - Fatal编程技术网

Xml XPath替换FOR语句

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

我有这个XPath(感谢Ian Roberts在SO中的回答):

XPath很棒,但我使用的工具不接受
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,那么它可能不够现代。请注意,我已经稍微简化了我的答案。