问题描述
限时送ChatGPT账号..考虑以下 XML 结构:
Consider the following XML structure:
<a>
<b>
<c>
<d x="1"/>
<d x="2"/>
<d x="3"/>
<d x="3"/>
<d x="4"/>
</c>
</b>
<b>
<c>
<d x="1"/>
<d x="1"/>
<d x="2"/>
<d x="3"/>
<d x="4"/>
<d x="5"/>
<d x="5"/>
</c>
</b>
<b>
<c>
<d x="1"/>
<d x="2"/>
<d x="3"/>
<d x="3"/>
<d x="4"/>
<d x="5"/>
<d x="5"/>
</c>
</b>
我想要一个 XPath 1.0 语句来给我@x 的最小值和最大值?到目前为止,我有以下最低要求:
I'd like a XPath 1.0 statement to give me the minimum and maximum values of @x? So far I have the following for the minimum:
//a/b/c/d[not(preceding-sibling::d/@x <= @x) and not(following-sibling::d/@x <= @x)]/@x
//a/b/c/d[not(preceding-sibling::d/@x <= @x) and not(following-sibling::d/@x <= @x)]/@x
很接近,但没有雪茄:-(
which is close, but no cigar :-(
非常感谢任何帮助!
谢谢,J
推荐答案
要获取最大值,请查找没有较小值的所有属性值.如果有多个结果,取第一个 - 它们必须相等.
To fetch the maximum value, look for all attribute values for which there are not smaller ones. If there are multiple results, take the first one - they must be equal.
(//@x[not(. < //@x)])[1]
对于最小值,只需将 <
替换为 >
.
For the minimum value, just replace <
by >
.
出于完整性原因:如果您的 XPath 引擎支持 XPath 2.0(或更好),只需使用 max(//@x)
尊重 min(//@x)
这可能会更快,更易读.
For completeness reasons: if your XPath engine supports XPath 2.0 (or better), just use max(//@x)
respecting min(//@x)
which will probably be faster and more readable.
这篇关于在 XPath 1.0 中选择多个节点下属性的最小值/最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论