Subject: Re: Beginner-Problem
From: "Vasu Chakkera" <vasucv@xxxxxxxxxxx>
Date: Fri, 22 Nov 2002 11:33:13 -0000
|
Hi Sorin,
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="energyreport">
<xsl:for-each select="energyconsumption"> <!-- Line 1 -->
<xsl:value-of select="@year"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
<xsl:for-each select="//terajoule[not(@energy =
preceding::terajoule/@energy)]"><!-- line 2 -->
<xsl:variable name = "energy" select="@energy"/> <!-- Line 3 store the
atribute energy in a variable called "energy" -->
<xsl:value-of select="$energy"/><!-- line 4-->
<xsl:text>
</xsl:text>
<xsl:apply-templates
select="/energyreport/energyconsumption/terajoule[@energy =$energy]"/><!--
line 5-->
</xsl:for-each>
</xsl:template>
<xsl:template match = "terajoule">
<xsl:value-of select="."/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
The explanation is as follows
line1 goes through each energyconsumption elements and outputs the year.
<xsl:text>
</xsl:text> is for printing the newline.
output :
1970
1980
Line 2 is for grouping the data based on the energy atribute.
Line 2 collects all the Unique energy atributes.
This can also be done using keys ( muenchean method)
So in your case you have Oil,Gaz and Coal evertime the for-each runs.
Line 3 Prints the Energy Attribute
Line 4 Calls the Template to match the terajoule element applying the
predicate that the attribute energy is equal to the value of variable
"enerygy" of liine 3
In other words we are trying to group the terajoule elements of same
attribute value together. So in your case this will first group all
terajoule elements with energy = Oil,
then Gaz and then Coal.
so your output will look like
1970
1980
Oil
2222
9222
Gaz
3333
9333
Coal
4444
9444
which is what you wanted..
HTH
----- Original Message -----
From: "Sorin Marti" <mas@xxxxxxxxxx>
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Friday, November 22, 2002 10:32 AM
Subject: Beginner-Problem
> Hi all,
>
> I am new to XSL and I am experimenting around... I've got an XML-File
> with the following structure
>
> <energyreport>
>
> <energyconsumption year="1970">
> <terajoule energy="Oil">2222</terajoule>
> <terajoule energy="Gaz">3333</terajoule>
> <terajoule energy="Coal">4444</terajoule>
> </energyconsumption>
>
> <energyconsumption year="1980">
> <terajoule energy="Oil">9222</terajoule>
> <terajoule energy="Gaz">9333</terajoule>
> <terajoule energy="Coal">9444</terajoule>
> </energyconsumption>
>
> [and so on...]
>
> </energyreport>
>
> I want to get out following structure, as simple text output (it is for
> a LaTeX-File):
>
>
> 1970 1980
> Oil 2222 9222
> Gaz 3333 9333
> Coal 4444 9444
>
>
> How to make that work? I tried this:
>
> <xsl:template match="/">
> <xsl:for-each select="/energyreport/energyconsumption">
> <xsl:value-of select="@year"/>
> </xsl:for-each>
> </xsl:template>
>
> That gives out the years, that's fine... but how to select the
> energy-attribute (only once each)?
>
> Every advice apreciated
>
> Thanks in advance
>
> Sorin Marti
>
>
>
> XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
>
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
|