|
Subject: Re: [xsl] Fwd: level grouping problem From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx> Date: Wed, 05 Sep 2007 22:57:29 -0400 |
My appologies in advance for the possible incorrect use of terminology, I'm still new to xslt.
I have an xml file that looks like that: ... and the output that I'm trying to achieve is the following: (the grouping needs to occur within each <reference> tag, i.e. I need to get) ... what is happening instead, though, is that elements are being grouped together, so the output that I get is:
here is the stylesheet that I'm using:
<xsl:stylesheet xmlns:xsl=" http://www.w3.org/1999/XSL/Transform "
version="1.0">
<xsl:key name="det" match="/export/references/reference/detail" use="@name"/>
<xsl:template match="/export/references">
<html>
<body>
<table>
<xsl:apply-templates select="reference"/> </table> </body> </html> </xsl:template>
<xsl:template match="reference"> <xsl:for-each select="detail[generate-id(.)=generate-id(key('det', @name))]/@name">
Any help would me much appreciated!! its probably obvious, but I've been staring at it for hours.... (The parser we have can work with both xslt 1.0 and 2.0)
T:\ftemp>type maria.xml <export> <references> <reference> <detail name="Detail1"> <title>Detail 1 text</title> </detail> <detail name="Detail2"> <title>Detail2 text</title> </detail> <detail name="Detail2"> <title>Detail2 text</title> </detail> </reference> <reference> <detail name="Detail1"> <title>Detail 1 text</title> </detail> <detail name="Detail2"> <title>Detail2 text</title> </detail> <detail name="Detail2"> <title>Detail2 text</title> </detail> </reference> </references> </export>
T:\ftemp>type maria.xsl <?xml version="1.0" encoding="US-ASCII"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="export/references/reference"/>
</body>
</html>
</xsl:template><xsl:template match="reference">
<!--put what is being grouped in a variable-->
<xsl:variable name="details" select="detail"/>
<!--walk through the variable-->
<xsl:for-each select="detail">
<xsl:sort select="@name"/>
<!--act on the first in document order-->
<xsl:if test="generate-id(.)=
generate-id($details[@name=current()/@name][1])">
<h2>
<xsl:value-of select="@name"/>
</h2>
<!--revisit all-->
<xsl:for-each select="$details[@name=current()/@name]">
<xsl:sort select="title"/>
<i><xsl:value-of select="title"/></i>
<br/>
</xsl:for-each>
</xsl:if>
</xsl:for-each>
</xsl:template></xsl:stylesheet> T:\ftemp>call xslt maria.xml maria.xsl maria.htm
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="export/references/reference"/>
</body>
</html>
</xsl:template><xsl:template match="reference">
<xsl:for-each-group select="detail" group-by="@name">
<xsl:sort select="@name"/>
<h2>
<xsl:value-of select="@name"/>
</h2>
<xsl:for-each select="current-group()">
<xsl:sort select="title"/>
<i><xsl:value-of select="title"/></i>
<br/>
</xsl:for-each>
</xsl:for-each-group>
</xsl:template>
-- Upcoming public training: XSLT/XSL-FO Sep 10, UBL/code lists Oct 1 World-wide corporate, govt. & user group XML, XSL and UBL training RSS feeds: publicly-available developer resources and training G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (F:-0995) Male Cancer Awareness Jul'07 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal
| Current Thread |
|---|
|
| <- Previous | Index | Next -> |
|---|---|---|
| [xsl] Fwd: level grouping problem, Maria Shvedova | Thread | Re: [xsl] Fwd: level grouping probl, Maria Shvedova |
| [xsl] Fwd: level grouping problem, Maria Shvedova | Date | RE: [xsl] Templates, SINGH Navpreet |
| Month |