views:

43

answers:

2

Hi all,

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"&gt;

  <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/column[name='Date Time']/value"/>
      <xsl:text>,</xsl:text>
      <xsl:value-of select="columns/column[name='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/column[name='Date Time']/value"/>  
      <xsl:text>,</xsl:text>
      <xsl:value-of select="columns/column[name='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

+1  A: 

Could this help ?

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

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

    <xsl:template match="/fileExport">
     <xsl:for-each select="rows/row">
         <xsl:variable name="date" select="columns/column[name='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>
dvhh
I guess that looks like it will work also.
Andez
what is the logic with the last field data the primary assumption is the first letter or the name, but I was worried of more bizarre variants
dvhh
+3  A: 

This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <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="column[name='Primary' or name='Manual']">
        <xsl:apply-templates/>
        <xsl:value-of select="concat(',',substring(name,1,1))"/>
    </xsl:template>
    <xsl:template match="row[position() mod 2]/*/column[name='Manual']|
                         row[position() mod 2 = 0]/*/column[name='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
Alejandro
+1 nice implementation, use of template match patterns for conditional processing.
LarsH
Looks better than what I've come up with. Think I'll check out templates and do a little more reading first.
Andez
@Andez: In order to understand this you also have to know the [built-in rules](http://www.w3.org/TR/xslt#built-in-rule). The meaning of all this is: *`row` to 'A'-childs result-NL; `value` to ','-string value; 'Primary' or 'Manual' column to childs result-','-first letter of `name`; don't process a 'Manual' column for odd `row`, nor 'Primary' column for even `row`, nor text nodes*.
Alejandro
+1 for a good answer.
Dimitre Novatchev