xslt – XSL Transform XML – Education Career Blog

I have some xml in the following format:

<fileExport>
  <rows>
    <row id="0">
      <columns>
        <column id="0">
          <name>Date Time</name>
          <value>2010-10-2 23:00:00 GMT</value>
        </column>
        <column id="1">
          <name>Primary</name>
          <value>100.1</value> 
        </column>
        <column id="2">
          <name>Manual</name>
          <value>20.5</value>
        </column>
        ...
      </columns>
    </row>
    <row id="1">
      <columns>
        <column id="0">
          <name>Date Time</name>
          <value>2010-10-3 00:00:00 GMT</value>
        </column>
        <column id="1">
          <name>Primary</name>
          <value>110.5</value> 
        </column>
        ...
        <column id="2">
          <name>Manual</name>
          <value>23.1</value>
        </column>
      </columns>
    </row>
  </rows>  
</fileExport>

Now the idea is to transform this to CSV text. Everything seems to be working fine – but I have 32 columns for each row in the above. To transform this to CSV, I am using the following XSL:

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

  <xsl:output method="text" />
  <xsl:strip-space elements="*"/>

  <xsl:template match="/">
    <xsl:for-each select="//rows/row">
      <!-- Now add a new row for each column -->

      <!-- first row in file for this row in table -->
      <xsl:text>A</xsl:text>
      <xsl:text>,</xsl:text>
      <xsl:value-of select="columns/columnname='Date Time'/value"/>
      <xsl:text>,</xsl:text>
      <xsl:value-of select="columns/columnname='Primary'/value"/>
      <xsl:text>,</xsl:text>
      <xsl:text>P</xsl:text>
      <xsl:text>&#xA;</xsl:text>

      <!-- second row in file for this row in table -->
      <xsl:text>A</xsl:text>
      <xsl:text>,</xsl:text>
      <xsl:value-of select="columns/columnname='Date Time'/value"/>  
      <xsl:text>,</xsl:text>
      <xsl:value-of select="columns/columnname='Manual'/value"/>
      <xsl:text>,</xsl:text>
      <xsl:text>M</xsl:text>
      <xsl:text>&#xA;</xsl:text>

      ... now repeat for the next 30 columns ...

      <!-- next row in table-->
    </xsl:for-each>
  <xsl:text>&lt;EOF&gt;</xsl:text>
  </xsl:template>
</xsl:stylesheet>

Now that gives us the output:

A,2010-10-2 23:00:00 GMT,100.1,P
A,2010-10-2 23:00:00 GMT,20.5,M
A,2010-10-3 00:00:00 GMT,110.5,P
A,2010-10-3 00:00:00 GMT,23.1,M
<EOF>

Question is, having 32 or so columns in the XML is it possible to shorten the XSL required to achieve the same results?

Any ideas?

Thanks,

Andez

,

This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" />
    <xsl:strip-space elements="*"/>
    <xsl:template match="row">
        <xsl:text>A</xsl:text>
        <xsl:apply-templates/>
        <xsl:text>&#xA;</xsl:text>
    </xsl:template>
    <xsl:template match="value">
        <xsl:value-of select="concat(',',.)"/>
    </xsl:template>
    <xsl:template match="columnname='Primary' or name='Manual'">
        <xsl:apply-templates/>
        <xsl:value-of select="concat(',',substring(name,1,1))"/>
    </xsl:template>
    <xsl:template match="rowposition() mod 2/*/columnname='Manual'|
                         rowposition() mod 2 = 0/*/columnname='Primary'|
                         text()" priority="1"/>
</xsl:stylesheet>

Output:

A,2010-10-2 23:00:00 GMT,100.1,P
A,2010-10-3 00:00:00 GMT,23.1,M

,

Could this help ?

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

    <xsl:output method="text" />
    <xsl:strip-space elements="*"/>

    <xsl:template match="/fileExport">
     <xsl:for-each select="rows/row">
         <xsl:variable name="date" select="columns/columnname='Date Time'/value"/>
         <xsl:for-each select="columns/column">
             <xsl:if test="name!='Date Time'">
                <xsl:text>A</xsl:text>
                <xsl:text>,</xsl:text>
                <xsl:value-of select="$date"/>
                <xsl:text>,</xsl:text>
                <xsl:value-of select="value"/>
                <xsl:text>,</xsl:text>
                 <xsl:choose>
                     <xsl:when test="name='Primary'">P</xsl:when>
                     <xsl:when test="name='Manual'">M</xsl:when>
                     ....
                </xsl:choose>
                <xsl:text>&#xA;</xsl:text>
                </xsl:if>
         </xsl:for-each>
     </xsl:for-each>
    <xsl:text>&lt;EOF&gt;</xsl:text>
    </xsl:template>
</xsl:stylesheet>

Leave a Comment