[xsl] Re: XSL to write XML tag values in columns

Subject: [xsl] Re: XSL to write XML tag values in columns
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Tue, 23 Dec 2003 22:01:17 +0100
"Jaime Alejandro Stuardo Bahamonde" <jstuardo@xxxxxxxxxxx> wrote in message
news:CA4A243A1C219949AABAEBF59C313C3D3AB9E7@xxxxxxxxxxxxxxxxxx
> Hi all..
>
> I have an XML like this:
>
> <test>
>   <ROW>
>     <f_key>1</f_key>
>     <field>blabla</field>
>   </ROW>
>   <ROW>
>     <f_key>1</f_key>
>     <field>bleble</field>
>   </ROW>
>   <ROW>
>     <f_key>2</f_key>
>     <field>blibli</field>
>   </ROW>
>   <ROW>
>     <f_key>2</f_key>
>     <field>bloblo</field>
>   </ROW>
> </test>
>
> I want that to be shown this way:
>
> 1           2
> blabla     blibli
> bleble     bloblo
>
> Using muenchian method I could achieve:
>
> 1           2
> blabla     bleble
> blibli     bloblo
>
> what I was not wanting.
>
> Long time ago someone suggested me a way to do it and it worked, by using
mode="row" in XSL but I don't remember exactly how can I use it.


This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

  <xsl:output method="text"/>

  <xsl:key name="kValByField" match="field"
       use="preceding-sibling::f_key[1]"/>

  <xsl:template match="/">
    <xsl:value-of select="concat('   1','&#09;','   2','&#xA;')"/>

    <xsl:call-template name="twoColTable">
      <xsl:with-param name="pCol1" select="key('kValByField', '1')"/>
      <xsl:with-param name="pCol2" select="key('kValByField', '2')"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="twoColTable">
    <xsl:param name="pCol1" select="/.."/>
    <xsl:param name="pCol2" select="/.."/>

    <xsl:variable name="vnumCol1" select="count($pCol1)"/>
    <xsl:variable name="vnumCol2" select="count($pCol2)"/>

    <xsl:variable name="vLongerCol"
     select="$pCol1[last() > $vnumCol2]
            |
             $pCol2[last() >= $vnumCol1]"/>


    <xsl:for-each select="$vLongerCol">
      <xsl:variable name="vPos" select="position()"/>
      <xsl:value-of select="concat($pCol1[position() = $vPos],
                                   '&#09;',
                                   $pCol2[position() = $vPos],
                                   '&#xA;'
                                   )"/>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

when applied on this source.xml:

<test>
  <ROW>
    <f_key>1</f_key>
    <field>blabla</field>
  </ROW>
  <ROW>
    <f_key>1</f_key>
    <field>bleble</field>
  </ROW>
  <ROW>
    <f_key>2</f_key>
    <field>blibli</field>
  </ROW>
  <ROW>
    <f_key>2</f_key>
    <field>bloblo</field>
  </ROW>
  <ROW>
    <f_key>1</f_key>
    <field>bleubleu</field>
  </ROW>
</test>

produces the wanted result:

   1    2
blabla blibli
bleble bloblo
bleubleu



Dimitre Novatchev.
FXSL developer

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html




 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread