[xsl] conditional multiline output

Subject: [xsl] conditional multiline output
From: "Erwin Kloeck" <kloeck@xxxxxxxxx>
Date: Tue, 6 Feb 2007 10:44:30 +0100
Hi,

A week ago a posted the following problem and Abel Braaksma was kind enough to
to give it some consideration.

I have in the meantime come up with a solution and would like to
solicit your comments
on how to make it more simple and clear. There is a real question here
as well: Where
does the space in front of the closing bracket come from and how do I
get rid of it?

Thanks

Erwin

Here is my solution (I omitted the header line) and I appended my
original problem:

--- begin example.xsl ---
<?xml version="1.0" encoding="UTF-8"?>
<?altova_samplexml C:\workspaces\tmp\xsl\example.xml?>
<xsl:stylesheet version="2.0"
               xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
               xmlns:fn="http://www.w3.org/2005/xpath-functions";
               xmlns:xs="http://www.w3.org/2001/XMLSchema";>

<xsl:output method="text" version="1.0" encoding="iso-8859-1"/>

   <xsl:variable name="delimiter" select="'; '" />
   <xsl:variable name="newline" select="'&#10;'" />

   <xsl:template match="/">
       <xsl:apply-templates/>
   </xsl:template>

<xsl:template match="logpoint">

       <xsl:variable name="this" select="." />
       <xsl:variable name="dates" select="distinct-values(date/@value)" />

       <xsl:variable name="max-count" >
           <xsl:value-of select="max(for $date in $dates return
count(date[@value = $date]))" />
       </xsl:variable>

       <xsl:for-each select="(1 to $max-count)" >
           <xsl:variable name="i" select="." />
           <xsl:value-of select="$this/@value" />
           <xsl:value-of select="$delimiter" />
           <xsl:for-each-group select="$this/date" group-by="@value" >
               <xsl:if test="current-group()[$i]/@string" >
                   <xsl:value-of
select="concat(current-group()[$i]/@string, ' [',
current-group()[$i]/@count), ']'" />
               </xsl:if>
               <xsl:value-of select="$delimiter" />
           </xsl:for-each-group>

           <xsl:value-of select="$newline" />
       </xsl:for-each>
   </xsl:template>
</xsl:stylesheet>
--- end example.xsl ---


I have a list of date elements in my xml that I want to output in columns in a csv file. If the same date occurs more than once, I want another line in the respective column and the label in the first column.


I think I should group the dates and then cicle thru and output the line with all 1st elements in the group, then all 2nd elements in the second line etc.


I would appreciate some pointers at how to do this.



--- begin example.xml --- <?xml version="1.0" encoding="UTF-8"?> <top> <logpoint value="AAA" > <date value="2007-01-01" count="1" string="aaa"/> <date value="2007-01-01" count="11" string="xxx"/> <date value="2007-01-02" count="2" string="bbb"/> <date value="2007-01-03" count="3" string="ccc"/> <date value="2007-01-03" count="4" string="ddd"/> <date value="2007-01-03" count="41" string="yyy"/> <date value="2007-01-04" count="5" string="eee"/> </logpoint> <logpoint value="BBB" > <date value="2007-01-01" count="31" string="lll"/> <date value="2007-01-02" count="32" string="mmm"/> <date value="2007-01-03" count="33" string="nnn"/> <date value="2007-01-04" count="34" string="ooo"/> </logpoint> </top> --- begin example.xml ---


desired output: --- begin example.csv --- logpoint;2007-01-01;2007-01-02;2007-01-03;2007-01-04; AAA;aaa[1];bbb[2];ccc[3];eee[5]; AAA;xxx[11];;ddd[4];; AAA;;;yyy[41];; BBB;lll[31];mmm[32];nnn[33];ooo[34]; --- end example.csv ---

Current Thread