[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Looking up keys in a separate xml file

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

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

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.