你好,我是新来的XML,并想使用XSL样式表来比较某些值
`<a>
<b> <name>foo</name> </b>
<b> <name>bar</name> </b>
<b> <name>fred</name> </b>
<b> <name>fred</name> </b>
</a>`
我想编写,检查所有B节点,并返回具有相同的值,从而使用简单的例子以上我想输出到类似的值的样式表:
“你重复的字符串是弗雷德”
我使用了每个循环返回所有的值,但比较名称和返回重复一直困扰me.If可能的话我想实现通过使用而循环类型的比较。
感谢您的任何帮助。
一个<xsl:key>
基溶液:
<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::
检查。
XSLT 1.0:用键A简单,溶液 :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:key name="kNameByVal" match="name" use="."/>
<xsl:template match="/*">
Your duplicate strings are: <xsl:text/>
<xsl:apply-templates select=
"b/name[generate-id() = generate-id(key('kNameByVal', .)[2])]"/>
</xsl:template>
<xsl:template match="name">
<xsl:if test="position() >1">, </xsl:if>
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
II。 XSLT 2.0溶液 :
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="vSeq" select="data(/a/b/name)"/>
<xsl:template match="/">
Your duplicate strings are: <xsl:text/>
<xsl:sequence select="$vSeq[index-of($vSeq,.)[2]]"/>
</xsl:template>
</xsl:stylesheet>
III。 的XPath 2.0单行
$vSeq[index-of($vSeq,.)[2]]
这producws在给定序列的所有值中,具有重复(一种选自重复的)。
使用while循环是对XSLT的理念,即使是可以做到的。
有一些更简单的方法做你想要什么,例如:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method='text' />
<xsl:template match="b">
<xsl:if test='preceding::b/name/text()=./name/text()'>
Your duplicate is: <xsl:copy-of select='./name/text()' />
</xsl:if>
</xsl:template>
</xsl:stylesheet>
这是寻找节点B,并检查是否在前b节点具有相同名称的文本