[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

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

Subject: Re: [xsl] Looking up keys in a separate xml file
From: Scott Anguish <sanguish@xxxxxxxxxxx>
Date: Tue, 6 Jan 2004 02:55:28 -0500

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

<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>

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

<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>

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 match="InsertDescription">
   <xsl:value-of select="key('descriptionlookup',@lookup)"/>
    <xsl:value-of select="@lookup" />


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

Current Thread