XPath 替换 FOR 语句 [英] XPath replace FOR statement
问题描述
我有这个 XPath(感谢 Ian Roberts 在 SO Xpath2.0 通过迭代选择字符串的第一次和最后一次出现) :
I have this XPath (Thanks to Ian Roberts' answer in SO Xpath2.0 selecting first and last occurance of string with iteration) :
Events/Properties[
count(
for $me in (Property[@Descriptor="200"]) return
(preceding-sibling::Properties[1][Property[@Descriptor="200"] = $me],
following-sibling::Properties[1][Property[@Descriptor="200"] = $me])
) != 2
]
XPath 很棒,但我使用的工具不接受 for
语句.
The XPath is great but the tool I'm using is not accepting for
statement.
我怎样才能重写这个 XPath 来避免这个问题[没有 for
语句]?
How could I rewrite this XPath to avoid this issue [without for
statement]?
推荐答案
假设您的输入是有序的(这是重要的一点!),您可以通过发出这些 XPath 1.0 表达式来实现:
Assuming that your input is ordered (that's the important bit!), you can do it by issuing these XPath 1.0 expressions:
小组第一:
//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]
.
You can go from there and select Property[@Descriptor = 100]
or Property[@Descriptor = 200]
individually.
您可以将两个表达式合二为一:
You can combine both expressions into one:
//Properties[
Property/@Descriptor = 200 and (
not(
Property = preceding-sibling::*/Property[@Descriptor = 200]
) or not(
Property = following-sibling::*/Property[@Descriptor = 200]
)
)
]
您将获得
对(各自组中的第一个和最后一个)的列表,但一个元素长的组除外.这意味着无法保证您不会返回偶数个节点.
You would get a list of <Properties>
pairs (first and last ones in their respective groups), except for groups that are one element long. That means there's no guarantee that you would not get an even number of nodes returned.
这篇关于XPath 替换 FOR 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!