This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
RE: xsl:key and document()
- To: "'Jeni Tennison'" <mail at jenitennison dot com>
- Subject: RE: [xsl] xsl:key and document()
- From: Robert Goheen <RobertG at avenuea dot com>
- Date: Mon, 8 Jan 2001 09:08:10 -0800
- Cc: "'xsl-list at lists dot mulberrytech dot com'" <xsl-list at lists dot mulberrytech dot com>
- Reply-To: xsl-list at lists dot mulberrytech dot com
Thanks for the great response. It works great! I would never have thought
placing a new document context in a xsl:for-each block....
Here's a side question as a result of the answer. If I didn't want to use a
variable in the key, how could I refer back to the document I'm currently
parsing when I'm in the context of the external doc? Specifically, what if
I wanted to do something like:
<xsl:for-each select="document('LUType.xml')">
<xsl:value-of select="key('LUType', ParentDocReference/Tag/@ID)" />
</xsl:for-each>
Even more specifically, if I'm either in a loop or template processing a
specific tag within my first document, how to I refer to the current() node?
Thanks.....
> -----Original Message-----
> From: Jeni Tennison [mailto:mail@jenitennison.com]
> Sent: Saturday, January 06, 2001 3:29 AM
> To: Robert Goheen
> Cc: xsl-list@lists.mulberrytech.com
> Subject: Re: [xsl] xsl:key and document()
>
>
> Hi Robert,
>
> > I'm trying to build a XSL doc that uses a couple of keys (using
> > MSXML 3.0). The values I want for these keys, however, are located
> > in XML documents other than the main document. However, I haven't
> > been able to use xsl:key to reference external documents.
>
> Yes, annoying isn't it? ;) The thing you have to realise is that the
> key definition uses a *match pattern*, not a *select expression*, to
> identify the nodes that it indexes. When key() is called, it looks for
> nodes that match the match pattern for the key *in the document
> holding the current node*, and indexes them.
>
> So, to create a key that will match nodes in your separate document,
> you need an xsl:key element that matches those nodes and don't need to
> worry about pointing out they're in a different document at all:
>
> <xsl:key name="LUType" match="record" use="@ID" />
>
> It's when you *use* the key that you determine which document is
> searched. You need to change the current node to be in the document
> you want to search in; usually you'd do this with an xsl:for-each:
>
> <xsl:for-each select="document('LUType.xml')">
> <xsl:value-of select="key('LUType', $keyValue)" />
> </xsl:for-each>
>
> Remember that within the xsl:for-each, the current node is the
> document root node, and therefore if you use an expression in the key
> value then it will be resolved relative to that document's root node:
> usually you'll want to use a variable. Remember also that it's an
> xsl:for-each, but actually you're just using it to change the current
> node - don't have a select expression that contains more than one node
> or it will repeat output left, right and centre.
>
> There are occasions where this limitation on key() causes real
> problems, particularly when you're using key() within a sort select
> expression, but usually the above method fits in with what you're
> trying to achieve. Let us know if not.
>
> I hope that helps,
>
> Jeni
>
> ---
> Jeni Tennison
> http://www.jenitennison.com/
>
>
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list