RE: [xsl] position matching

Subject: RE: [xsl] position matching
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 27 Nov 2008 09:20:18 -0000
Firstly, never do this:

<xsl:variable name="pos">
  <xsl:value-of select="X"/>
</xsl:variable>

when you mean this

<xsl:variable name="pos" select="X"/>

Usually, the only damage is that it's three times as many lines of code and
increases the execution time by a factor of about 5. In this case the damage
is worse, it means that $pos is a document node rather than a number, which
means that if used in a predicate A[$pos] the meaning is completely
different - it tests whether the node exists, which it does.

Also, you wrote select="target[position()]" when you meant
select="position()". Writing select="X[position()]" means
select="X[position()=position()]" which means the same as select="X".

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

> -----Original Message-----
> From: Ganesh Babu N [mailto:nbabuganesh@xxxxxxxxx] 
> Sent: 27 November 2008 05:18
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: Re: [xsl] position matching
> 
> Hai
> 
> Based on Michael points i have updated the XSL but I am not 
> getting any output. Please help me.
> <xsl:template match="map">
> <xsl:if test="count(child::source) = count(child::target) and
> count(child::*) != 2">
> 				<xsl:variable 
> name="pos"><xsl:value-of select="target[position()]"/></xsl:variable>
> 				<mac:template match="{source[$pos]}">
> 					<mac:element 
> name="{target[$pos]}">
> 						<xsl:for-each 
> select="target[$pos > 1]">
> 						<mac:attribute 
> name="substring-after({target[$pos]},'@')">
> 							<mac:value-of
> select="(preceding-sibling::source)[$pos]"/>
> 							</mac:attribute>
> 						</xsl:for-each>
> 					<mac:apply-templates 
> select="@*|node()"/>
> 					</mac:element>
> 				</mac:template>
> 			</xsl:if>
> </xsl:template>
> 
> Regards,
> Ganesh
> 
> 
> On Wed, Nov 26, 2008 at 4:31 PM, Michael Kay 
> <mike@xxxxxxxxxxxx> wrote:
> > Two obvious errors, excluding typos:
> >
> > (1) target[position() > 2] should be target[position() > 1]
> >
> > (2) preceding-sibling::source[position()] - you've 
> forgotten that the 
> > context inside a predicate changes. You need to bind 
> position() to a 
> > variable p outside the expression and then use 
> > preceding-sibling::source[$p]. Or in fact I suspect it should be 
> > (preceding-sibling::source)[$p] because you want them numbered in 
> > forwards rather than reverse order.
> >
> > Michael Kay
> > http://www.saxonica.com/
> >
> >
> >
> >> -----Original Message-----
> >> From: Ganesh Babu N [mailto:nbabuganesh@xxxxxxxxx]
> >> Sent: 26 November 2008 10:52
> >> To: XSL
> >> Subject: [xsl] position matching
> >>
> >> Hai All,
> >>
> >> I am working on automatic XSLT generation project and here is the 
> >> mapping table.
> >>
> >> Here is my XML file:
> >>  <map>
> >>       <source>article</source>
> >>       <source>@docsubtype</source>
> >>       <source>@version</source>
> >>       <target>article</target>
> >>       <target>@article-type</target>
> >>       <target>@dtd-version</target>
> >>    </map>
> >>
> >> My stylesheet:
> >>
> >> <xsl:template match="map">
> >> <xsl:if test="count(child::source) = count(child::target) and
> >> count(child::*) != 2">
> >> <mac:template match="{source[1]}">
> >> <mac:element name="{target[1]}">
> >> <xsl:for-each select="target[position() > 2"> <mac:attribute 
> >> name="substring-after({target},'@')"><mac:value-of
> >> select="preceding-sibling::source[position()]"/></mac:attribute>
> >> <xsl:for-each>
> >> <mac:apply-templates select="@*|node()"/> </mac:element> 
> >> </mac:template> </xsl:if> </xsl:template>
> >>
> >> Expected Output:
> >>
> >> <xsl:template match="article>
> >> <xsl:element name="article">
> >> <xsl:attribute name="article-type"><xsl:value-of 
> >> select="@docsubtype"></xsl:attribute>
> >> <xsl:attribute name="@dtd-version"><xsl:value-of 
> >> select="@version"></xsl:attribute>
> >> </xsl:element>
> >> <xsl:template>
> >>
> >>
> >> I am not getting the desired result. Please let me know where I am 
> >> going wrong.
> >>
> >> Regards,
> >> Ganesh

Current Thread