RE: [xsl] position

Subject: RE: [xsl] position
From: "Andrew Welch" <AWelch@xxxxxxxxxxxxxxx>
Date: Mon, 16 Feb 2004 09:15:39 -0000
> I'm having trouble using position() in the following 
> situation. The xml looks like this:
> 
> <root>
> 	<page>
> 		<line>
> 			<w>one</w> <w>two<note text="first note"/></w>
> <w>three</w>
> 		</line>
> 		<line>
> 			<w>four</w> <w>five</w> <w>six<note text="second
> note"/></w>
> 		</line>
> 		<line>
> 			<w>seven<note text="third note"/></w>
> <w>eight</w> <w>nine</w>
> 		</line>
> 	</page>
> 	<page>
> 		<line>
> 			<w>ten</w> <w>eleven<note text="4th 
> note"/></w> <w>twelve</w>
> 		</line>
> 		<line>
> 			<w>thirteen</w> <w>fourteen</w> <w>fifteen<note
> text="5th note"/></w>
> 		</line>
> 		<line>
> 			<w>sixteen<note text="6th note"/></w>
> <w>seventeen</w> <w>eighteen</w>
> 		</line>
> 	</page>
> </root>
> 
> And the xsl file like this:
> 
> <xsl:template match="/">
> 	<html>
> 		<body>
> 			<xsl:apply-templates select="root/page"/>
> 			<br/>
> 			<xsl:text>Notes:</xsl:text>
> 			<br/>
> 			<xsl:for-each select="//note">
> 				<xsl:value-of select="position()"/>
> 				<xsl:text> </xsl:text>
> 				<xsl:value-of select="@text"/>
> 				<br/>
> 			</xsl:for-each>
> 		</body>
> 	</html>
> </xsl:template>
> 
> <xsl:template match="page">
> 	<xsl:apply-templates select="line"/>
> 	<hr/>
> </xsl:template>
> 
> <xsl:template match="line">
> 	<xsl:apply-templates/>
> 	<br/>
> </xsl:template>
> 
> <xsl:template match="w">
> 	<xsl:text> </xsl:text>
> 	<xsl:apply-templates/>
> 	<xsl:if test="note">
> 		<span
> style="vertical-align:super;font-size:7pt">*</span>
> <!-- 
> --------------------------------------------------------^ 
> position should go here --> 
> 	</xsl:if>
> </xsl:template>
> 
> Instead of the asterisk in the <span>, I'd like to put the 
> value of the position of the <note> there, just like I did in 
> the root template (where it works as it should), but I can't 
> get the correct value there. 
> 
> Has anyone got any ideas on how to do this? 

The for-each each in the root template will select a list of <note>
nodes to process, so you can use position() here to return the position
of the node within that list.

A template match will process just a single node, so position() will
return 1.

You will need to use: <xsl:number level="any"/>

I would change the 'w' matching template to:

<xsl:template match="w">
  <xsl:text> </xsl:text>
  <xsl:apply-templates/>
</xsl:template>

And add the template:

<xsl:template match="note">
  <span style="vertical-align:super;font-size:7pt">
    <xsl:number level="any"/>
  </span>
</xsl:template>

You don't need the if test in the <w> matching template, as the <note>
matching template will only be fired if there is a child <note> of <w>
(this is the 'push' style of processing being mentioned).

cheers
andrew

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


Current Thread