RE: [xsl] Optimizing a XSLT

Subject: RE: [xsl] Optimizing a XSLT
From: "Michael Kay" <mhk@xxxxxxxxx>
Date: Tue, 15 Apr 2003 00:05:13 +0100
Everyone looking at your code is immediately struck by the horrible use
of disable-output-escaping to write tags. But that's distracting people
from the real problem, which is:

<xsl:when test="unit=preceding::unit">
>      <xsl:choose>
>       <xsl:when test="schedule=preceding::schedule">

The preceding axis is very expensive, it has O(n^2) performance because
it searches the entire document up to the context node. I don't have
time to study your problem and suggest anything better, but the answer
is usually one or more of:

* use preceding-sibling
* use preceding::x[1]
* use keys

Michael Kay
Software AG
home: Michael.H.Kay@xxxxxxxxxxxx
work: Michael.Kay@xxxxxxxxxxxxxx 

> -----Original Message-----
> From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx 
> [mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of Eric Barre
> Sent: 14 April 2003 19:02
> To: XSL-List@xxxxxxxxxxxxxxxxxxxxxx
> Cc: Eric Barre
> Subject: [xsl] Optimizing a XSLT
> 
> 
> Hi,
>  
> I wrote a XSL file to transform a XML file to another XML 
> file, everything works fine on small file but eventually I 
> will be using it against large XML file (over 1mg). When I 
> run it against such a large file it takes for ever to return, 
> I mean I have to kill it after running for an hour. Is there 
> a way to optimize the XSL that I have to make it perform 
> better? Here is the 'input' XML: 
> <CSVFile>
>  <ROW>
>   <unit>DOUG_1</unit>
>   <schedule>101</schedule>
>   <textdate>2003-04-09</textdate>
>   <texthour>18</texthour>
>   <mw>999999</mw>
>   <price>16.09</price>
>   <ScheduleType>Cost</ScheduleType>
>   <MarketAvailability>RealTime</MarketAvailability>
>  </ROW>
>  <ROW>
>   <unit>DOUG_1</unit>
>   <schedule>199</schedule>
>   <textdate>2003-04-09</textdate>
>   <texthour>18</texthour>
>   <mw>999999</mw>
>   <price>0.00</price>
>   <ScheduleType>Cost</ScheduleType>
>   <MarketAvailability>RealTime</MarketAvailability>
>  </ROW>
>  <ROW>
>   <unit>ERIC_1</unit>
>   <schedule>201</schedule>
>   <textdate>2003-04-09</textdate>
>   <texthour>18</texthour>
>   <mw>0</mw>
>   <price>10.79</price>
>   <ScheduleType>Cost</ScheduleType>
>   <MarketAvailability>RealTime</MarketAvailability>
>  </ROW>
>  <ROW>
>   <unit>ERIC_1</unit>
>   <schedule>201</schedule>
>   <textdate>2003-04-09</textdate>
>   <texthour>18</texthour>
>   <mw>18</mw>
>   <price>21.59</price>
>   <ScheduleType>Cost</ScheduleType>
>   <MarketAvailability>RealTime</MarketAvailability>
>  </ROW>
> </CSVFile> 
>  
> The XSL:
>  
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="1.0" 
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
>  <xsl:output method="xml" indent="no"/>
>  <xsl:template match="/">
>   <SubmitRequest>
>    <xsl:call-template name="RowProcess"/>
>    <xsl:text 
> disable-output-escaping="yes">&lt;/PriceCurvehourly&gt;</xsl:text>
>    <xsl:text 
> disable-output-escaping="yes">&lt;/OfferPriceCurve&gt;</xsl:text>
>    <xsl:text 
> disable-output-escaping="yes">&lt;ScheduleType&gt;</xsl:text>
>     <xsl:value-of select="CSVFile/ROW/ScheduleType"/>
>    <xsl:text 
> disable-output-escaping="yes">&lt;/ScheduleType&gt;</xsl:text>
>    <xsl:text 
> disable-output-escaping="yes">&lt;MarketAvailability&gt;</xsl:text>
>     <xsl:value-of select="CSVFile/ROW/MarketAvailability"/>
>    <xsl:text 
> disable-output-escaping="yes">&lt;/MarketAvailability&gt;</xsl:text>
> 
>    <xsl:text
> disable-output-escaping="yes">&lt;/UnitScheduleOffer&gt;</xsl:text>   
>   </SubmitRequest>
>  </xsl:template>
>  <!--Processing-->
>  <xsl:template name="RowProcess">
>   <xsl:for-each select="/CSVFile/ROW">
>    <xsl:call-template name="UnitProcess"/>
>   </xsl:for-each>  
>  </xsl:template>
>  <xsl:template name="UnitProcess">
>   <xsl:call-template name="unit"/>
>  </xsl:template>
>  <xsl:template name="unit">
>    <xsl:choose>
>     <xsl:when test="unit=preceding::unit">
>      <xsl:choose>
>       <xsl:when test="schedule=preceding::schedule">
>        <xsl:call-template name="PricePointProcess"/>
>       </xsl:when>
>       <xsl:otherwise>
>        <xsl:call-template name="NewUnit"/> 
>        <xsl:call-template name="PricePointProcess"/>       
>       </xsl:otherwise>
>      </xsl:choose>
>     </xsl:when>
>     <xsl:otherwise>
>      <xsl:call-template name="NewUnit"/>     
>      <xsl:call-template name="PricePointProcess"/>
>     </xsl:otherwise>
>    </xsl:choose>   
>  </xsl:template>
>  <xsl:template name="NewUnit">
>   <xsl:choose>
>    <xsl:when test="position()!=1">    
>     <xsl:text 
> disable-output-escaping="yes">&lt;/PriceCurvehourly&gt;</xsl:text>
>     <xsl:text 
> disable-output-escaping="yes">&lt;/OfferPriceCurve&gt;</xsl:text>
>     <xsl:text 
> disable-output-escaping="yes">&lt;ScheduleType&gt;</xsl:text>
>      <xsl:value-of select="ScheduleType"/>
>     <xsl:text 
> disable-output-escaping="yes">&lt;/ScheduleType&gt;</xsl:text>
>     <xsl:text 
> disable-output-escaping="yes">&lt;MarketAvailability&gt;</xsl:text>
>      <xsl:value-of select="MarketAvailability"/>
>     <xsl:text 
> disable-output-escaping="yes">&lt;/MarketAvailability&gt;</xsl:text>
> 
>     <xsl:text
> disable-output-escaping="yes">&lt;/UnitScheduleOffer&gt;</xsl:
> text>     
>    </xsl:when>
>   </xsl:choose>     
>    <xsl:text disable-output-escaping="yes">&lt;UnitScheduleOffer
> unit="</xsl:text>
>    <xsl:value-of select="unit"/>
>    <xsl:text disable-output-escaping="yes">" schedule="</xsl:text>
>    <xsl:value-of select="schedule"/>
>    <xsl:text disable-output-escaping="yes">" day="</xsl:text>
>    <xsl:value-of select="textdate"/>
>    <xsl:text disable-output-escaping="yes">"&gt;</xsl:text>
>    <xsl:text
> disable-output-escaping="yes">&lt;OfferPriceCurve&gt;</xsl:text>   
>    <xsl:text disable-output-escaping="yes">&lt;PriceCurvehourly
> hour="</xsl:text>
>     <xsl:value-of select="texthour"/>
>    <xsl:text disable-output-escaping="yes">"&gt;</xsl:text>
>  </xsl:template>
>  <xsl:template name="PricePointProcess">
>   <xsl:text disable-output-escaping="yes">&lt;PricePoint 
> MW="</xsl:text>
>    <xsl:value-of select="mw"/>
>   <xsl:text disable-output-escaping="yes">" price="</xsl:text>
>    <xsl:value-of select="price"/>
>   <xsl:text disable-output-escaping="yes">"/&gt;</xsl:text>
>  </xsl:template>
> </xsl:stylesheet>
> 
>  
> and the expected output:
>  
> <?xml version="1.0" encoding="UTF-8"?>
> <SubmitRequest>
>  <UnitScheduleOffer unit="DOUG_1" schedule="101" day="2003-04-09">
>   <OfferPriceCurve>
>    <PriceCurvehourly hour="18">
>     <PricePoint MW="0" price="8.04"/>
>     <PricePoint MW="68" price="16.09"/>
>     <PricePoint MW="999999" price="16.09"/>
>    </PriceCurvehourly>
>   </OfferPriceCurve>
>   <ScheduleType>Cost</ScheduleType>
>   <MarketAvailability>RealTime</MarketAvailability>
>  </UnitScheduleOffer>
>  <UnitScheduleOffer unit="DOUG_1" schedule="199" day="2003-04-09">
>   <OfferPriceCurve>
>    <PriceCurvehourly hour="18">
>     <PricePoint MW="999999" price="0.00"/>
>    </PriceCurvehourly>
>   </OfferPriceCurve>
>   <ScheduleType>Cost</ScheduleType>
>   <MarketAvailability>RealTime</MarketAvailability>
>  </UnitScheduleOffer>
>  <UnitScheduleOffer unit="ERIC_1" schedule="201" day="2003-04-09">
>   <OfferPriceCurve>
>    <PriceCurvehourly hour="18">
>     <PricePoint MW="0" price="10.79"/>
>     <PricePoint MW="18" price="21.59"/>
>     <PricePoint MW="999999" price="21.59"/>
>    </PriceCurvehourly>
>   </OfferPriceCurve>
>   <ScheduleType>Cost</ScheduleType>
>   <MarketAvailability>RealTime</MarketAvailability>
>  </UnitScheduleOffer>
> </SubmitRequest> 
>  
> After reading some posting I tried to stay away from the 
> <xsl:for-each> as much as possible as well as using variables 
> and parameters. If anyone could let me know a way to optimize 
> this I would appreciate it. 
>  
>  
> Thanks,
> Eric Barre
>  
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 


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


Current Thread