[xsl] XPath problem with getting all ancestors

Subject: [xsl] XPath problem with getting all ancestors
From: Jostein Austvik Jacobsen <josteinaj@xxxxxxxxx>
Date: Fri, 23 Oct 2009 12:27:02 +0200
I've got a document like this:

<?xml version="1.0" encoding="utf-8"?>
<document>
	<article>
		<meta>
			<title>Title</title>
		</meta>
		<content>
			<headline>Headline</headline>
			<para>
				<note id="1">text</note>
			</para>
		</content>
	</article>
</document>

And in a template for the note tags I'm trying to use the ancestor
axis to get the list [ para , content , article ].
However, I only seem to get [ para ] with ancestor::* and [ para , ""
] with ancestor::node().
When I test it like this:

<xsl:template match="note">
		ancestors="<xsl:value-of select="count(ancestor::*)"/>"
		preceding="<xsl:value-of select="count(preceding::*)"/>"
		contentNum="<xsl:value-of select="count(preceding::content)"/>"
</xsl:template match>

...I get ancestors="1", preceding="2" and contentNum="0" as output.

What I actually need to do is to count the number of content nodes
preceding the current note (relevant for the input XML note numbering
scheme). I did so earlier in the document (for referencing the actual
notes) when matching document like this:

<xsl:template match="document">
	(...)
	<xsl:for-each select="//note">
		<note>
			<xsl:attribute name="id"><xsl:value-of
select="count(ancestor::content/preceding::content)"/></xsl:attribute>
			<p><xsl:apply-templates/></p>
		</note>
	</xsl:for-each>
	(...)
</xsl:template>

So here I get access to all of the notes ancestors. In retrospect I
could probably have used select="count(preceding::content)" instead,
but still...

How come that I cannot access the notes ancestors from my note template?

Regards
Jostein

Current Thread