我已经可以使用XSLT 2.0分组了,但是我在使用XSLT 1.0时遇到了麻烦。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE cataleg SYSTEM "cataleg.dtd">
<?xml-stylesheet href="cataleg.css" ?>
<?xml-stylesheet type="text/xsl" href="cataleg.xsl" ?>
<cataleg>
<peli ordre="X">
<titol>X-Men</titol>
<caratula>XMen.jpg</caratula>
</peli>
<peli ordre="B">
<titol>X-Men 2</titol>
<caratula>XMen2.jpg</caratula>
</peli>
<peli ordre="C">
<titol>X-Men: La Decisió Final</titol>
<caratula>XMenFD.jpg</caratula>
</peli>
<peli ordre="A">
<titol>X-Men Origins: Wolverine</titol>
<caratula>XMenOW.jpg</caratula>
</peli>
</cataleg>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<html>
<head>
<title>PRACTICA 1 XML</title>
<link href="https://procomprador.com/PRACTICA/cataleg.css" rel="stylesheet" />
</head>
<xsl:apply-templates/>
</html>
</xsl:template>
<xsl:template match="cataleg">
<div id="main">
<xsl:for-each-group select="peli" group-by="@ordre">
<xsl:sort select="titol"/>
<Inicial value="{@ordre}">
<h1><xsl:value-of select="@ordre"></xsl:value-of></h1>
<xsl:for-each select="current-group()">
<div class="peli">
<img src="https://procomprador.com/PRACTICA/imatges/{caratula}" alt=" "/>
</div>
</xsl:for-each>
</Inicial>
</xsl:for-each-group>
</div>
</xsl:template>
</xsl:transform>
<xsl:key name="contacts-by-surname" match="cataleg/peli" use="titol" />
<xsl:key name="pelis-by-surname" match="peli" use="@peli" />
<xsl:template match="cataleg">
<xsl:for-each select="peli[count(. | key('pelis-by-surname', surname)[1]) = 1]">
<xsl:sort select="surname" />
<xsl:value-of select="surname" />,<br />
<xsl:for-each select="key('pelis-by-peli', peli)">
<xsl:sort select="caratula" />
<xsl:value-of select="caratula" /> (<xsl:value-of select="titol" />)<br />
</xsl:for-each>
</xsl:for-each>
</xsl:template>
答案 0 :(得分:1)
您的XSLT 2.0样式表通过其peli
属性将ordre
元素分组。如果以XML示例为例,其中某些peli
具有相同的ordre
:
XML
<cataleg>
<peli ordre="X">
<titol>X-Men</titol>
<caratula>XMen.jpg</caratula>
</peli>
<peli ordre="A">
<titol>X-Men 2</titol>
<caratula>XMen2.jpg</caratula>
</peli>
<peli ordre="X">
<titol>X-Men: La Decisió Final</titol>
<caratula>XMenFD.jpg</caratula>
</peli>
<peli ordre="A">
<titol>X-Men Origins: Wolverine</titol>
<caratula>XMenOW.jpg</caratula>
</peli>
</cataleg>
并应用以下样式表:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="pelis-by-ordre" match="peli" use="@ordre" />
<xsl:template match="/cataleg">
<html>
<xsl:for-each select="peli[count(. | key('pelis-by-ordre', @ordre)[1]) = 1]">
<h1>
<xsl:value-of select="@ordre" />
</h1>
<xsl:for-each select="key('pelis-by-ordre', @ordre)">
<div>
<xsl:value-of select="titol" />
</div>
</xsl:for-each>
</xsl:for-each>
</html>
</xsl:template>
</xsl:stylesheet>
您将得到:
结果
<html>
<h1>X</h1>
<div>X-Men</div>
<div>X-Men: La Decisió Final</div>
<h1>A</h1>
<div>X-Men 2</div>
<div>X-Men Origins: Wolverine</div>
</html>
渲染: