John
John

Reputation: 111

More efficient XSLT for transforming an XML doc to CSV flatfile

I have an XSLT that works and translates my XML doc to CSV. But its hard to read (I don't like all the "../../" references) and I'm wondering if those also hurt performance - the file I'm transforming is big. I've looked at several different examples, but I've only been able to make the one below work.

So my questions are: 1. Can this XSLT be rewritten to be more concise and to not use the "../.." references. 2. Are "../.." type references less efficient than storing a value somehow.

Samples below. Thanks, John


Heres the XSLT:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
   <xsl:apply-templates select="Document"/>
</xsl:template>
<xsl:template match="Document">
   <xsl:apply-templates select="DocBody"/>
</xsl:template>
<xsl:template match="DocBody">
   <xsl:apply-templates select="Values"/>
</xsl:template>
<xsl:template match="Values">
   <xsl:apply-templates select="IntervalValues"/>
</xsl:template>
<xsl:template match="IntervalValues">
   <xsl:apply-templates select="Quantity"/>
</xsl:template>
<xsl:template match="Quantity">
   <xsl:value-of select="concat(../../../../DocHeader/DocTitle,',',../../../../DocHeader/CreatedAt,',',../../../DeliveryDate,',',../../../DeliveryHour,',',../Interval,',',Type,',',Value)"/>
   <xsl:text>&#xA;</xsl:text>
</xsl:template>
</xsl:stylesheet>

Heres a small XML sample:

<?xml version="1.0" encoding="UTF-8"?>
<Document>
<DocHeader>
<DocTitle>Totals Report</DocTitle>
<DocRevision>1</DocRevision>
<CreatedAt>2011-02-10T21:25:00</CreatedAt>
</DocHeader>
<DocBody>
<DeliveryDate>2011-02-10</DeliveryDate>
<DeliveryHour>22</DeliveryHour>
<Values>
<IntervalValues>
<Interval>1</Interval>
<Quantity>
<Type>Energy</Type>
<Value>18053.5</Value>
</Quantity>
<Quantity>
<Type>Loss</Type>
<Value>438.7</Value>
</Quantity>
<Quantity>
<Type>Load</Type>
<Value>17614.8</Value>
</Quantity>
</IntervalValues>
<IntervalValues>
<Interval>2</Interval>
<Quantity>
<Type>Energy</Type>
<Value>17940.7</Value>
</Quantity>
<Quantity>
<Type>Loss</Type>
<Value>437.7</Value>
</Quantity>
<Quantity>
<Type>Load</Type>
<Value>17503</Value>
</Quantity>
</IntervalValues>
<IntervalValues>
<Interval>3</Interval>
<Quantity>
<Type>Energy</Type>
<Value>17871.7</Value>
</Quantity>
<Quantity>
<Type>Loss</Type>
<Value>437.4</Value>
</Quantity>
<Quantity>
<Type>Load</Type>
<Value>17434.3</Value>
</Quantity>
</IntervalValues>
</Values>
</DocBody>
</Document>

Heres a sample output

Totals Report,2011-02-10T21:25:00,2011-02-10,22,1,Energy,18053.5
Totals Report,2011-02-10T21:25:00,2011-02-10,22,1,Loss,438.7
Totals Report,2011-02-10T21:25:00,2011-02-10,22,1,Load,17614.8
Totals Report,2011-02-10T21:25:00,2011-02-10,22,2,Energy,17940.7
Totals Report,2011-02-10T21:25:00,2011-02-10,22,2,Loss,437.7
Totals Report,2011-02-10T21:25:00,2011-02-10,22,2,Load,17503
Totals Report,2011-02-10T21:25:00,2011-02-10,22,3,Energy,17871.7
Totals Report,2011-02-10T21:25:00,2011-02-10,22,3,Loss,437.4
Totals Report,2011-02-10T21:25:00,2011-02-10,22,3,Load,17434.3

Upvotes: 0

Views: 1019

Answers (2)

Bob Vale
Bob Vale

Reputation: 18474

Is this any easier to read for you?

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:template match="/Document">
    <xsl:variable name="header" select="DocHeader"/>
    <xsl:variable name="body" select="DocBody" />
    <xsl:for-each select="DocBody/Values">
      <xsl:for-each select="IntervalValues">
        <xsl:variable name="interval" select="Interval"/>
        <xsl:for-each select="Quantity">
        <xsl:value-of select="$header/DocTitle"/>
        <xsl:text>,</xsl:text>
        <xsl:value-of select="$header/CreatedAt"/>
        <xsl:text>,</xsl:text>
        <xsl:value-of select="$body/DeliveryDate"/>
        <xsl:text>,</xsl:text>
        <xsl:value-of select="$body/DeliveryHour"/>
        <xsl:text>,</xsl:text>
        <xsl:value-of select="$interval"/>
        <xsl:text>,</xsl:text>
        <xsl:value-of select="Type"/>
        <xsl:text>,</xsl:text>
        <xsl:value-of select="Value"/>
        <xsl:text>&#xA;</xsl:text>
        </xsl:for-each>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

Upvotes: 0

David Carlisle
David Carlisle

Reputation: 5652

You could pull the common parts out a bit earlier, something like this:

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

<xsl:variable name="head">
 <xsl:for-each select="Document/DocHeader">
  <xsl:value-of  select="concat(DocTitle,',',CreatedAt,',')"/>
 </xsl:for-each>
 <xsl:for-each select="Document/DocBody">
  <xsl:value-of  select="concat(DeliveryDate,',',DeliveryHour,',')"/>
 </xsl:for-each>
</xsl:variable>

<xsl:template match="/">
 <xsl:apply-templates select="Document/DocBody/Values/IntervalValues/Quantity"/>
</xsl:template>

<xsl:template match="Quantity">
 <xsl:value-of select="concat($head,../Interval,',',Type,',',Value)"/>
 <xsl:text>&#xA;</xsl:text>
</xsl:template>

</xsl:stylesheet>

Upvotes: 1

Related Questions