节点的 XSL 比较 [英] XSL comparison of nodes
问题描述
您好,我是 xml 新手,想使用 xsl 样式表比较一些值
Hello I am new to xml and would like to compare some values using an xsl stylesheet
`<a>
<b> <name>foo</name> </b>
<b> <name>bar</name> </b>
<b> <name>fred</name> </b>
<b> <name>fred</name> </b>
</a>`
我想编写一个样式表来检查所有 b 节点并返回具有相同值的值,因此使用上面的简单示例,我希望输出类似于:
你的重复字符串是 fred"
I would like to write a style sheet that checks all the b nodes and returns the values that have the same value so using the simple example above i would like the output to resemble :
"Your duplicate strings are fred"
我已经使用 for each 循环来返回所有值,但是比较名称和返回重复项我没有做到.如果可能,我想通过使用 while 类型循环来实现比较.
I have used a for each loop to return all the values but comparing the names and returning the duplicates has eluded me.If possible i would like to achieve the comparison by the use of a while type loop.
感谢您的帮助.
推荐答案
一个基于
的解决方案:
An <xsl:key>
-based solution:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="kName" match="b/name" use="text()" />
<xsl:template match="/">
<xsl:for-each select="//b/name">
<xsl:if test="count(key('kName', text())) > 1">
<xsl:value-of select="concat('Your duplicate is: ', text(), '
')" />
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
对于大型输入文档,这比使用 preceding::
检查的解决方案更有效.
For large input documents this will be more efficient than a solution that uses a preceding::
check.
这篇关于节点的 XSL 比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!