[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] Optimization using keys


Subject: RE: [xsl] Optimization using keys
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Wed, 16 Feb 2005 19:47:59 -0000

Anything that processes each Menu element by counting how many preceding
Menu elements there are is going to be slow, in fact it's going to vary as
the square of the input document size.

If your requirement is:
Given a XML with nested Menu structures,
i want to copy all the nodes to the output tree *except* the one that
corresponds to the $pos position, wich i want to replace with the
result from document.

then what you should do is first identify the node at the Nth position, and
then when you process each node, test whether it is that one.

The node at the n'th position is (//Menu)[$pos]

You can test whether node $A is the same node as $B in XPath 2.0 using ($A
is $B). In 1.0 you can use generate-id($A)=generate-id($B), or
count($A|$B)=1.

Michael Kay
http://www.saxonica.com/



> -----Original Message-----
> From: Antsnio Mota [mailto:amsmota@xxxxxxxxx]
> Sent: 16 February 2005 19:40
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: Re: [xsl] Optimization using keys
>
> I'm also trying to optimize this template
>
> (indentity template)
>
>        <xsl:template match="Menu">
>                 <xsl:choose>
>                         <xsl:when
> test="count(./ancestor-or-self::Menu)+count(./preceding::Menu)
> =number($pos)">
>                                 <xsl:variable name="pai"
> select="Menu_K"/>
>                                 <xsl:copy>
>                                         <xsl:apply-templates
> select="@*|node()[not(name()='MenuTipo' or name()='Filhos' )]"
> mode="menu"/>
>                                         <MenuTipo>EXP</MenuTipo>
>                                         <Filhos>
>                                                 <xsl:for-each
> select="Filhos/Menu">
>                                                         <xsl:variable
> name="xmldoc"
> select="document(concat($xmluri,'?Menu_K=',Menu_K,'&amp;Pai_K=
> ',$pai))"/>
>                                                         <xsl:variable
> name="filhos" select="Filhos"/>
>                                                         <xsl:variable
> name="orelhas" select="Orelhas"/>
>
> <xsl:apply-templates select="$xmldoc/Menus/Menu" mode="menu">
>
> <xsl:with-param name="filhos" select="$filhos"/>
>
> <xsl:with-param name="orelhas" select="$orelhas"/>
>
> </xsl:apply-templates>
>                                                 </xsl:for-each>
>                                         </Filhos>
>                                 </xsl:copy>
>                         </xsl:when>
>                         <xsl:otherwise>
>                                 <xsl:copy>
>                                         <xsl:apply-templates
> select="*"/>
>                                 </xsl:copy>
>                         </xsl:otherwise>
>                 </xsl:choose>
>         </xsl:template>
>
>       <xsl:template match="Menu" mode="menu">
>                 <xsl:param name="filhos"/>
>                 <xsl:param name="orelhas"/>
>                 <xsl:copy>
>                         <xsl:apply-templates mode="menu"/>
>                         <xsl:if test="$filhos">
>                                 <xsl:copy-of select="$filhos"/>
>                         </xsl:if>
>                         <xsl:if test="$orelhas">
>                                 <xsl:copy-of select="$orelhas"/>
>                         </xsl:if>
>                 </xsl:copy>
>         </xsl:template>
>
> but it is too slow and i don't know why. I thought it was the document
> ($xmluri is pointing to a ASP that returns a chunk of XML), but i've
> tested with a small xml input tree and it's fast enough, so it seems
> the problem is with the XML size. However i don't want to do much
> things with it.
>
> I'll fraze it (poor) english. Given a XML with nested Menu structures,
> i want to copy all the nodes to the output tree *except* the one that
> corresponds to the $pos position, wich i want to replace with the
> result from document.
>
> For each of the resulting document() nodes i want to append the
> original <Filhos> and <Orelhas>.
>
> So is there a optimized way to do this? Including the "simple copy" of
> the nodes?
>
>
> Thanks again.


Current Thread
Keywords