【发布时间】:2022-01-05 01:27:17
【问题描述】:
考虑以下 XML 文档:
<cities>
<city name="Milano" country="Italia" pop="5"/>
<city name="Paris" country="France" pop="7"/>
<city name="München" country="Deutschland" pop="4"/>
<city name="Lyon" country="France" pop="2"/>
<city name="Venezia" country="Italia" pop="1"/>
</cities>
使用上面的 XML 文档和 XSLT,我想得到以下输出(期望的输出):
1.Country: Italia:
1.1. City: Milano
Population: 5 Millions
1.2. City: Venezia
Population: 1 Millions
total Population: 6 Millions
2.Country: France:
2.1. City: Paris
Population: 7 Millions
2.2 City: Lyon
Population: 2 Millions
total Population: 9 Millions
3.Country: Deutschland:
3.1 City: München
Population: 4 Millions
total Population: 4 Millions
这是我的尝试(它没有给我想要的东西):
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:template match="cities">
<xsl:for-each-group select="city" group-by="@country">
<xsl:number format="1."/>Country: <xsl:value-of select="current-grouping-key()"/>:
<xsl:for-each select="current-group()">
<!--<xsl:number count="current-group()|@name" format="1.1." level="multiple"/>: Error The current-group() function cannot be used in a pattern -->
City: <xsl:value-of select="@name" />
Population: <xsl:value-of select="@pop"/> Millions</xsl:for-each>
total Population: <xsl:value-of select="sum(current-group()/@pop)"/> Millions
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
【问题讨论】: