bra*_*zoo 30 xml xpath xpath-1.0
如果我有像这样的XML:
<foo>
<bar id="1" score="192" />
<bar id="2" score="227" />
<bar id="3" score="105" />
...
</foo>
Run Code Online (Sandbox Code Playgroud)
我可以使用XPath查找最小值和最大值score
吗?
编辑:我正在使用的工具(Andariel ant任务)不支持XPath 2.0解决方案.
Pav*_*aev 40
这是一个稍微短一点的解决方案.
最大值:
/foo/bar/@score[not(. < ../../bar/@score)][1]
Run Code Online (Sandbox Code Playgroud)
最低:
/foo/bar/@score[not(. > ../../bar/@score)][1]
Run Code Online (Sandbox Code Playgroud)
我已经编辑了谓词,因此它适用于任何序列bar
,即使您决定更改路径.请注意,attribute的parent是它所属的元素.
如果嵌入在像XSLT或Ant脚本的XML文件这些查询,请记住编码<
和>
作为<
尊重>
.
bra*_*zoo 19
原来这个工具不支持XPath 2.0.
XPath 1.0没有花哨min()
和max()
功能,所以要找到这些值,我们需要对XPath逻辑有点棘手,并比较节点的兄弟节点上的值:
最大值:
/foo/bar[not(preceding-sibling::bar/@score >= @score)
and not(following-sibling::bar/@score > @score)]/@score
Run Code Online (Sandbox Code Playgroud)
最低:
/foo/bar[not(preceding-sibling::bar/@score <= @score)
and not(following-sibling::bar/@score < @score)]/@score
Run Code Online (Sandbox Code Playgroud)
如果嵌入在像XSLT或Ant脚本的XML文件这些查询,请记住编码<
和>
作为<
尊重>
.
这应该工作......
max(foo/bar/@score)
Run Code Online (Sandbox Code Playgroud)
......而且......
min(foo/bar/@score)
Run Code Online (Sandbox Code Playgroud)
...查看此功能参考.
小智 5
我偶然发现了该线程并没有找到对我有用的答案,所以我最终最终使用的方法在哪里......
输出最低值,当然你可以选择从最低值的节点输出@id,而不是你自己选择。
<xsl:for-each select="/foo">
<xsl:sort select="@score"/>
<xsl:if test="position()=1">
<xsl:value-of select="@score"/>
</xsl:if>
</xsl:for-each>
Run Code Online (Sandbox Code Playgroud)
最大值相同:
<xsl:for-each select="/foo">
<xsl:sort select="@score" order="descending"/>
<xsl:if test="position()=1">
<xsl:value-of select="@score"/>
</xsl:if>
</xsl:for-each>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
33238 次 |
最近记录: |