我是XSLT的新手,刚开始学习它。我需要改变一件简单的事情。
我的输入XML文件是
`<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>GO Home</title>
<artist>Bonnie Tyler</artist>
<country>USA</country>
<company>Columbia</company>
<price>9.90</price>
<year>1987</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
</catalog>`
输出应该是
<?xml version="1.0" encoding="UTF-8"?>
<list>
<singer artist="Bob Dylan">
<song>Empire Burlesque</song>
</singer>
<singer artist="Bonnie Tyler">
<song>GO Home</song>
<song>Hide you heart</song>
</singer>
</list>
我的XSL是
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:element name="list">
<xsl:apply-templates select="catalog"/>
</xsl:element>
</xsl:template>
<xsl:template match="catalog">
<xsl:for-each-group select="cd" group-by="artist">
<xsl:element name="singer">
<xsl:attribute name="artist">
<xsl:value-of select="artist"/>
</xsl:attribute>
<xsl:apply-templates select="artist"/>
</xsl:element>
</xsl:for-each-group>
</xsl:template>
<xsl:template match="artist">
<xsl:element name="song">
<xsl:value-of select="../title"/>
</xsl:element>
</xsl:template>
我似乎遇到分组问题,无法解决问题。提前谢谢。