次の XML があります。
<assessment>
<section>
<item>
<attributes>
<variables>
<variable>
<variable_name value="MORTIMER"/>
</variable>
</variables>
</attributes>
</item>
<item>
<attributes>
<variables>
<variable>
<variable_name value="FRED"/>
</variable>
</variables>
</attributes>
</item>
<item>
<attributes>
<variables>
<variable>
<variable_name value="MORTIMER"/>
</variable>
</variables>
</attributes>
</item>
</section>
</assessment>
その XML を処理するための次の XSLT があります。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:key name="kValueByVal" match="item//variables//variable_name"
use="@value"/>
<xsl:template match="assessment">
<xsl:for-each select="
.//item//variables//variable_name/@value
">
<xsl:value-of select=
"concat(., ' ', count(key('kValueByVal', .)), '
')"/>
<br/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
それは次のように出力します。これはほとんど私が望むものです:
MORTIMER 2
FRED 1
MORTIMER 2
それぞれの variable_names と、それぞれの発生回数がリストされます。唯一の問題は、変数名が 1 回だけではなく発生するたびに、このカウントが 1 回与えられることです。
これは私が出力したいものです:
MORTIMER 2
FRED 1
XSLTコードを変更してそれを得るにはどうすればよいですか? XSLT 1.0 を使用していることに注意してください。
うまくいくように見える次のソリューションは、何も出力しません。
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:key name="kValueByVal" match="item//variables//variable_name"
use="@value"/>
<xsl:template match="assessment">
<xsl:for-each select=".//item//variables//variable_name/@value[generate-id()
=
generate-id(key('kValueByVal',.)[1])]">
<xsl:value-of select=
"concat(., ' ', count(key('kValueByVal', .)), '
')"/>
<br/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>