[英]Problems grouping in XSLT 1.0?
I've gotten XSLT 2.0 grouping to work, but I'm having trouble with XSLT 1.0.我已经让 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>
Your XSLT 2.0 stylesheet groups the peli
elements by their ordre
attribute. XSLT 2.0 样式表按
peli
元素的ordre
属性对它们进行分组。 If you take an XML example where some peli
have the same ordre
:如果以 XML 示例为例,其中某些
peli
具有相同的ordre
:
XML 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>
and apply the following stylesheet:并应用以下样式表:
XSLT 1.0 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>
you will get:你会得到:
Result结果
<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>
rendered:呈现:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.