Subject: Re: [xsl] XSLT grouping without Muenchen Method From: "Ismael Cams" <xslt@xxxxxxxxxxx> Date: Tue, 31 Aug 2004 07:54:49 +0200 |
On Mon, 30 Aug 2004 20:47:46 +0200 "cking" <cking@xxxxxxxxxx> wrote:
Kind regards, Ismakl
Hi Ismael,
Here's a solution in two passes:
--- pass1.xsl ---
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/root">
<root>
<xsl:apply-templates select="object"/>
</root>
</xsl:template>
<xsl:template match="object">
<object name="{@name}" instance="{concat('Instance', instance/@number)}">
<xsl:attribute name="interval">
<xsl:value-of select="substring-before(instance/sample/@date_time, ':')"/>
<xsl:call-template name="calculate-interval">
<xsl:with-param name="time" select="substring-after(instance/sample/@date_time, ':')"/>
</xsl:call-template>
</xsl:attribute>
</object>
</xsl:template>
<xsl:template name="calculate-interval">
<xsl:param name="time"/>
<xsl:variable name="hour" select="number(substring-before($time, ':'))"/>
<xsl:choose>
<xsl:when test="$hour < 15">:00:00</xsl:when>
<xsl:when test="$hour < 30">:15:00</xsl:when>
<xsl:when test="$hour < 45">:30:00</xsl:when>
<xsl:otherwise>:45:00</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
This will produce this intermediate xml file:
<root>
<object name="object1" instance="Instance0" interval="2004-08-30 15:00:00"/>
<object name="object2" instance="Instance0" interval="2004-08-30 15:30:00"/>
<object name="object3" instance="Instance0" interval="2004-08-30 15:00:00"/>
<object name="object4" instance="Instance1" interval="2004-08-30 15:00:00"/>
<object name="object5" instance="Instance1" interval="2004-08-30 15:30:00"/>
<object name="object6" instance="Instance1" interval="2004-08-30 15:00:00"/>
</root>
Which then you can transform in a second pass:
--- pass2.xsl ---
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/root">
<xsl:apply-templates select="object">
<xsl:sort select="@interval"/>
<xsl:sort select="@instance"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="object">
<xsl:variable name="interval" select="@interval"/>
<xsl:variable name="instance" select="@instance"/>
<xsl:if test="not(preceding-sibling::object[@interval=$interval and @instance=$instance])">
<!-- first of group -->
<xsl:apply-templates select="self::object | following-sibling::object[@interval=$interval and @instance=$instance]"
mode="write"/>
<xsl:value-of select="@instance"/>
<xsl:text>;</xsl:text>
<xsl:value-of select="@interval"/>
<xsl:text>
</xsl:text>
</xsl:if>
</xsl:template>
<xsl:template match="object" mode="write">
<xsl:value-of select="@name"/>
<xsl:text>;</xsl:text>
</xsl:template>
</xsl:stylesheet>
Which will give you this text file: object1;object3;Instance0;2004-08-30 15:00:00 object4;object6;Instance1;2004-08-30 15:00:00 object2;Instance0;2004-08-30 15:30:00 object5;Instance1;2004-08-30 15:30:00
I'm not familiar with XT, does it support node-sets? If so, you could use that to do it in one pass only.
HTH,
Best regards Anton Triest
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] XSLT grouping without Mue, cking | Thread | RE: [xsl] XSLT grouping without Mue, Kienle, Steven C |
Re: [xsl] variable question, Bruce D'Arcus | Date | RE: [xsl] variable question, Michael Kay |
Month |