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

Re: [xsl] extract xpath locator


Subject: Re: [xsl] extract xpath locator
From: Nicolas Mazziotta <nicolas.mazziotta@xxxxxxxxx>
Date: Thu, 12 Aug 2004 13:13:58 +0200

Hello,

I have written a recursive template to extract the XPath pointing to an element

		<xsl:template name="getPath">
			<xsl:param name="node"></xsl:param>
			<xsl:if test="$node">
				<xsl:call-template name="getPath">
					<xsl:with-param name="node" select="$node/parent::*"></xsl:with-param>
				</xsl:call-template>	
				<xsl:text>/</xsl:text>
				<xsl:variable name="name">
					<xsl:value-of select="name($node)"></xsl:value-of>
				</xsl:variable>
				<xsl:value-of select="$name"></xsl:value-of>
			</xsl:if>
		</xsl:template>

This gives the hierarchy. Eg,

applied on <b> in:

	<doc>This is <i><b>a</b></i> test</doc>

it outputs "/doc/i"

But I cannot figure out how to compute the positions of nodes; applied on <b> in:

	<doc>This <i>i</i>s <i><b>a</b></i> test</doc>

of course, it still outputs "/doc/i", but I want to improve it to output "/doc/i[2]".

Since the context node is the <b> element, using 'position()' in the template returns its position(). I cannot tell the template that $node becomes the context node --maybe it's possible, but I don't know how. I have tried to "count(preceding-sibling::$name)", but saxon does not like it. I keep trying harder.

Thanks in advance for your help.

N. Mazziotta


Current Thread
Keywords