Re: [xsl] Looking up keys in a separate xml file

Subject: Re: [xsl] Looking up keys in a separate xml file
From: Anthony Zawacki <zwacki@xxxxxxxxxx>
Date: Tue, 6 Jan 2004 09:17:19 -0500

This works under Xalan, but I don't know how efficient this is:

<?xml version='1.0' encoding='iso-8859-1'?>

<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output method='xml' version='1.0' encoding='iso-8859-1' omit-xml-declaration="yes" indent="yes"/>

<xsl:key name="descriptionlookup" match="Descriptions/Description" use="@name"/>
<xsl:variable name="desc" select="document('desc.xml')"/>

<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="InsertDescription">
<xsl:variable name="key_value" select="@lookup"/>
<xsl:for-each select="$desc">
<xsl:copy-of select="key('descriptionlookup',$key_value)/Para"/>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

There may be a better way than saving the @lookup as $key_value, but I'm no where near an expert in XSL

Thanks,
Anthony Zawacki

410-571-7161
zwacki@xxxxxxxxxx

Inactive hide details for Scott Anguish <sanguish@xxxxxxxxxxx>Scott Anguish <sanguish@xxxxxxxxxxx>




          Scott Anguish <sanguish@xxxxxxxxxxx>
          Sent by: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx

          01/06/2004 02:55 AM
          Please respond to xsl-list



To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
cc:
Subject: Re: [xsl] Looking up keys in a separate xml file


> Your one XSLT stylesheet can match nodes from different documents just
> by saying:
>
> <xsl:template match="descriptions">......
>
> Where elsewhere in your document you do the push:
>
> <xsl:apply-templates
> select="document('thedocument.xml')/descriptions"/>
>
> If you have a <descriptions> element in both input files, then you
> could either use modes or you could qualify an ancestral element that
> is unique in each file.
>
>

I tried that a number of different ways, but still had to fall back to
the multiple pass methods... So I'm clearly not understanding...

I have a description file

<Descriptions>
<Description name="a"><Para>Description for <subname />With Lookup Name
A</Para></Description>
<Description name="b"><Para>Description for <subname />With Lookup Name
B</Para></Description>
<Description name="c"><Para>Description for <subname />With Lookup Name
C</Para></Description>
</Descriptions>

and I have my file that I need to insert those elements into

<Items>
<Item><Name>Item A</Name><InsertDescription lookup="a" /></Item>
<Item><Name>Item B</Name><InsertDescription lookup="b" /></Item>
<Item><Name>Item C</Name><InsertDescription lookup="a" /></Item>
<Item><Name>Item D</Name><InsertDescription lookup="c" /></Item>
<Item><Name>Item E</Name><InsertDescription lookup="a" /></Item>
</Items>


Each InsertDescription needs to lookup the corresponding
Description/@name and insert the full contents (so, for a lookup="a", I
get '<Description name="a"><Para>Description for <subname />With Lookup
Name A</Para></Description>')

So, it sounds like I'd do this

<?xml version='1.0' encoding='iso-8859-1'?>

<xsl:stylesheet version='1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output method='xml' version='1.0' encoding='iso-8859-1'
omit-xml-declaration="yes" indent="yes"/>

<xsl:key name="descriptionlookup" match="Descriptions/Description"
use="@name"/>

<xsl:template match="/">
<xsl:apply-templates
select="document('descriptions.xml')/descriptions"/>
<xsl:apply-templates />
</xsl:template>

<xsl:template match="InsertDescription">
<xsl:value-of select="key('descriptionlookup',@lookup)"/>
<xsl:value-of select="@lookup" />
</xsl:template>

</xsl:stylesheet>


but I'm not getting any results aside from the Item name's being
output...


XSL-List info and archive:
http://www.mulberrytech.com/xsl/xsl-list

GIF image

GIF image

GIF image

Current Thread