こんにちは、私は次のxmlを持っています
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item>
<name>john</name>
<year>2010</year>
</item>
<item>
<name>sam</name>
<year>2000</year>
</item>
<item>
<name>jack</name>
<year>2007</year>
</item>
<item>
<name>smith</name>
<year>2010</year>
</item>
</root>
次の xslt を使用して年ごとにグループ化します
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
<xsl:template match="/">
<xsl:for-each-group select="r//*[(name=*)]" group-by="year">
<xsl:sort select="year" order="descending"/>
<xsl:variable name="total" select="count(/r//*[(name=*)]) + 1" />
<xsl:value-of select="year"/><br />
<xsl:for-each select="current-group()/name">
<xsl:variable name="i" select="position()"/>
<xsl:call-template name="row">
<xsl:with-param name="name" select="."/>
<xsl:with-param name="number" select="$total - $i"/>
</xsl:call-template>
</xsl:for-each>
<br />
</xsl:for-each-group>
</xsl:template>
<xsl:template name="row">
<xsl:param name="name"/>
<xsl:param name="number"/>
<xsl:value-of select="concat($number, '. ')"/>
<xsl:value-of select="concat($name, ' ')"/><br />
</xsl:template>
</xsl:stylesheet>
これは出力しています。私が望む出力にかなり近いです。
2010
4. john
3. smith
2007
4. jack
2000
4. sam
私が欲しいのは、単にすべての名前に番号を付けることです(名前の総数から1までの降順)。
2010
4. john
3. smith
2007
2. jack
2000
1. sam
変数を新しい値に再割り当てできれば簡単ですが、それは不可能だと思うので、別の解決策を見つける必要があります。誰でもこの問題を解決する方法を見つけるのを手伝ってくれますか?
ありがとう