Re: [xsl] xsl:key, predicates and documents.

Subject: Re: [xsl] xsl:key, predicates and documents.
From: Nic Gibson <nicg@xxxxxxxxxx>
Date: Fri, 14 May 2010 14:10:18 +0000
On Fri, May 14, 2010 at 04:03:28PM +0200, Martin Honnen wrote:
> Nic Gibson wrote:
> >On Fri, May 14, 2010 at 02:36:18PM +0100, Andrew Welch wrote:
> >>>So, I created keys that look like this:
> >>>
> >>>? ?<xsl:key name="term-key" select="e[@PER='n']" use="@HW"/>
> >>>? ?<xsl:key name="person-key" select="e[@PER='y']" use="@SUR"/>
> >>...
> >>><xsl:template match="eData[@PERS='n']">
> >>>? ?<xsl:variable name="exact" select="key('term-key', @HW)"/>
> >>>? ?...
> >>>
> >>></xsl:template>
> >>>
> >>>We know that there are matches yet this doesn't work unless
> >>>we remove the predicate from the key definition. Are predicates
> >>>usable in key definitions?
> >>It's much harder to help without seeing any input, but I can see a PER
> >>vs PERS difference in the code there... is that it?
> >>
> >
> >Good point but that's just my retyping error. The attribute is @PER.
> >I'm just trying to get some data from its owner
> 
> The key function call (with two arguments) finds the indexed nodes
> in the document the context node belongs to so within your
> 
>   <xsl:template match="eData[@PERS='n']">
> 
> the context node is an "eData" element in your $in-doc document and
> the key function call key('term-key', @HW) finds nodes in that
> document. You seem to have two documents and might want to find
> nodes in the other document so you either need to use the key
> function call with the third argument (possible in XSLT 2.0) or you
> need to use an xsl:for-each to change the context to the other
> document.
> In both cases, the way you have set up your stylesheet I think you need
>   <xsl:variable name="main-root" select="/"/>
> as a global variable and then you can use
>   key('term-key', @HW, $main-root)
> in XSLT 2.0 or with XSLT 1.0
>   <xsl:variable name="hwatt" select="@HW"/>
>   <xsl:variable name="exact>
>     <xsl:for-each select="$main-root">
>       <xsl:value-of select="key('term-key', $hwatt)"/>
>     </xsl:for-each>
>   </xsl:variable>

That'll be it. I had completely forgotten that. 

thank you

nic


> 
> Does that help? If not then I am afraid you need to show your input
> documents.
> 
> -- 
> 
> 	Martin Honnen
> 	http://msmvps.com/blogs/martin_honnen/

Current Thread