RE: [xsl] Get specific elements

Subject: RE: [xsl] Get specific elements
From: TSchutzerWeissmann@xxxxxxxxxxxxxxxx
Date: Mon, 28 Apr 2003 12:34:57 +0100

> Thanks for the answer. I was think if there was a way of 
> using ./ancestor::*
> Something like this
> <xsl:variable name="path" select="./ancestor::*"/>
> <xsl:variable name="elements" select="document($doc)//*"/> 
> and then find THE element with the same ancestors.
> <xsl:variable name="element" ...

It sounds like you want to test nodes according to whether they share a
path. If the nodes are from a different document to the one that defines the
path, the two can't have the *same* ancestors, only the same sequence of

You could use a key if you know what depth of path you want to compare,
otherwise you might need a recursive function.

<xsl:key name="nodesByPath_5deep" match="*" 
	use="concat(local-name(ancestor::*[1]), '|'
			local-name(ancestor::*[2]), '|' ,
			local-name(ancestor::*[3]), '|' ,
			local-name(ancestor::*[4]), '|' ,
			local-name(ancestor::*[5]) )" />

then get $path by concatenating in the same way:
<xsl:variable name="path" select="concat(local-name(ancestor::*[1]), '|'
			local-name(ancestor::*[2]), '|' ,
			local-name(ancestor::*[3]), '|' ,
			local-name(ancestor::*[4]), '|' ,
			local-name(ancestor::*[5]) )" />

The good thing about this is that using document() will change the context
of the key, so you can apply-templates to all the elements in the $doc that
match $path just by doing this:

<xsl:for-each select="document($doc)">  -- for-each changes the context
	<xsl:apply-templates select="key('nodesByPath_5deep', $path"/>

Come to think of it, it doesn't matter if you do this to compare things with
only 2 ancestors (as in both cases the nonexistent anscestors will return ""
for name().

it might work...

 "They put us through the pain of learning to walk and talk, and when
we manage that, they tell us to be quiet and sit still."
Eric Chevillard - Préhistoires

 XSL-List info and archive:

Current Thread