xsl-list
[Top] [All Lists]

Re: XSL to write XML tag values in columns

2003-12-23 14:01:17

"Jaime Alejandro Stuardo Bahamonde" <jstuardo(_at_)security(_dot_)cl> wrote in 
message
news:CA4A243A1C219949AABAEBF59C313C3D3AB9E7(_at_)srv-security-53(_dot_)(_dot_)(_dot_)
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



<Prev in Thread] Current Thread [Next in Thread>