RE: [xsl] XPath problem with getting all ancestors

Subject: RE: [xsl] XPath problem with getting all ancestors
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Fri, 23 Oct 2009 11:58:40 +0100
My guess (and it's a completely wild one) is that you aren't applying
templates to the note element in the source tree as you have shown it, but
to a copy of the note element in some smaller tree, probably a tree rooted
at the para element.

To confirm that we would need to see more of the stylesheet. Or, within the
match="note" template, do

<xsl:message><xsl:copy-of select="/"/></xsl:message>

to see what tree you are processing at the time.

Regards,

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

> -----Original Message-----
> From: Jostein Austvik Jacobsen [mailto:josteinaj@xxxxxxxxx] 
> Sent: 23 October 2009 11:27
> To: xsl-list
> Subject: [xsl] XPath problem with getting all ancestors
> 
> 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