RE: [xsl] Xsl Transform to slow (I think)

Subject: RE: [xsl] Xsl Transform to slow (I think)
From: "Michael Kay" <mhk@xxxxxxxxx>
Date: Tue, 15 Jun 2004 19:49:38 +0100
The line that's causing the trouble is:

<xsl:value-of select="count(preceding::*[name(.)='detail'])+1"/>

This means that for each <value> element, the system has to count all the
preceding elements in the document, which gives O(n^2) performance.

Try using <xsl:number level="any"/>: there's a chance that your XSLT
processor might implement this more efficiently.

You could also rewrite the expression as count(preceding::detail)+1, but I
doubt that would make a big difference.

If all the <detail> elements are at the same level of the hierarchy, the
expression count(../preceding-sibling::detail) +
count(../../preceding-sibling::Registo/detail)+1 might be a bit faster,
though still O(n^2).

Michael Kay 

> -----Original Message-----
> From: Hélder Sousa [mailto:Helder.Sousa@xxxxxx] 
> Sent: 14 June 2004 16:18
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Xsl Transform to slow (I think)
> 
> Hi!
> 
> I have the next XML:
> <?xml version="1.0" encoding="ISO-8859-1"?>
> <Registos>
> 	<Registo>
> 		<a>aaa</a>
> 		<b>bbb</a>
> 		<value>2592.19</value>
> 		<detail>
> 			<date>2003-11-20</date>
> 			<value>13196.72</value>
> 		</detail>
> 		<detail>
> 			<date>2003-11-20</date>
> 			<value>13196.72</value>
> 		</detail>
> 	</Registo>
> 	<Registo>
> 		<a>aaa</a>
> 		<b>bbb</a>
> 		<value>0.00</value>
> 		<detail>
> 			<date>2003-11-20</date>
> 			<value>13196.72</value>
> 		</detail>
> 	</Registo>
> </Registos>
> 
> In the end I want the next XML.. The rules are:
> * if value of tag "value" in "Registo" element is equal to 
> zero then that tag (<value>) must disappear
> * all tag <detail> must have an atribute "num" that is an 
> unique incremental ID.
> 
> <?xml version="1.0" encoding="ISO-8859-1"?>
> <Registos>
> 	<Registo>
> 		<a>aaa</a>
> 		<b>bbb</a>
> 		<value>2592.19</value>
> 		<detail num="1">
> 			<date>2003-11-20</date>
> 			<value>13196.72</value>
> 		</detail>
> 		<detail num="2">
> 			<date>2003-11-20</date>
> 			<value>13196.72</value>
> 		</detail>
> 	</Registo>
> 	<Registo>
> 		<a>aaa</a>
> 		<b>bbb</a>
> 		<detail num="3">
> 			<date>2003-11-20</date>
> 			<value>13196.72</value>
> 		</detail>
> 	</Registo>
> </Registos>
> 
> I implements the follow 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" version="1.0" encoding="UTF-8" 
> indent="yes"/>
> 	<xsl:template match="/">
> 		<Registos>
> 		<xsl:for-each select=".//Registo">
> 			<Registo>
> 				<xsl:for-each 
> select="*[name(.)!='detail']">
> 					<xsl:if test="name(.)!='value'">
> 						<xsl:copy-of 
> select="."/>
> 					</xsl:if>
> 					<xsl:if 
> test="name(.)='value' and number(.)!=0">
> 						<xsl:copy-of 
> select="."/>
> 					</xsl:if>
> 				</xsl:for-each>
> 				<xsl:for-each 
> select="*[name(.)='detail']">
> 					<detail>
> 						<xsl:attribute 
> name="num">
> 							
> <xsl:value-of select="count(preceding::*[name(.)='detail'])+1"/>
> 						</xsl:attribute>
> 						<xsl:for-each 
> select="*">
> 							
> <xsl:copy-of select="."/>
> 						</xsl:for-each>
> 					</detail>
> 				</xsl:for-each>
> 			</Registo>
> 		</xsl:for-each>
> 		</Registos>
> 	</xsl:template>
> </xsl:stylesheet>
> 
> Although it's work, it's to slow! Can you help me to 
> performance this xsl?
> Tks :)
> 
> 
> Hélder Sousa
> 						
> 
> 
> Esta mensagem contém informação abrangida por sigilo ou 
> confidencial. Caso tenha recebido esta mensagem 
> indevidamente, queira informar de imediato o remetente e 
> proceder à destruição de todas as cópias da mesma.
>  
> This message contains information that may be privileged or 
> confidential. If you receive this message in error please 
> notify the sender immediately and delete all copies of this message.
> 
> 
> --+------------------------------------------------------------------
> XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
> or e-mail: <mailto:xsl-list-unsubscribe@xxxxxxxxxxxxxxxxxxxxxx>
> --+--
> 
> 


Current Thread