[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: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Tue, 06 Jan 2004 09:44:55 -0500

At 2004-01-06 02:55 -0500, Scott Anguish wrote:
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...

But you missed what I said later in my message you quoted about setting the context for use of the key function. Note that the processor typically caches documents opened using the document() function so you can repeatedly call it without a performance penalty.


Below is a one-pass solution to your problem ... I hope this time it helps.

................... Ken

p.s. for those interested in US east coast XSL training, we still don't have enough preregistered students for a decision tomorrow to go with the course or cancel it and save the venue deposit fees ... if you have been thinking about registering, please do so today or the course might not happen next month. Thanks!


T:\ftemp>type thedocument.xml
<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>


T:\ftemp>type scott.xml
<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>

T:\ftemp>type scott.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

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

<xsl:output indent="yes"/>

<xsl:template match="Items|Item|Name">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="InsertDescription">
  <xsl:variable name="lookup" select="@lookup"/>
  <xsl:for-each select="document('thedocument.xml')">
    <xsl:copy-of select="key('descriptionlookup',$lookup)"/>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

T:\ftemp>saxon -o scott.out scott.xml scott.xsl

T:\ftemp>type scott.out
<?xml version="1.0" encoding="utf-8"?>
<Items>

   <Item>
      <Name>Item A</Name>
      <Description name="a">
         <Para>Description for <subname/>With Lookup Name A</Para>
      </Description>
   </Item>

   <Item>
      <Name>Item B</Name>
      <Description name="b">
         <Para>Description for <subname/>With Lookup Name B</Para>
      </Description>
   </Item>

   <Item>
      <Name>Item C</Name>
      <Description name="a">
         <Para>Description for <subname/>With Lookup Name A</Para>
      </Description>
   </Item>

   <Item>
      <Name>Item D</Name>
      <Description name="c">
         <Para>Description for <subname/>With Lookup Name C</Para>
      </Description>
   </Item>

   <Item>
      <Name>Item E</Name>
      <Description name="a">
         <Para>Description for <subname/>With Lookup Name A</Para>
      </Description>
   </Item>

</Items>
T:\ftemp>rem Done!


-- North America (Washington, DC): 3-day XSLT/2-day XSL-FO 2004-02-09 Instructor-led on-site corporate, government & user group training for XSLT and XSL-FO world-wide: please contact us for the details

G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
ISBN 0-13-065196-6                       Definitive XSLT and XPath
ISBN 0-13-140374-5                               Definitive XSL-FO
ISBN 1-894049-08-X   Practical Transformation Using XSLT and XPath
ISBN 1-894049-11-X               Practical Formatting Using XSL-FO
Member of the XML Guild of Practitioners:     http://XMLGuild.info
Male Breast Cancer Awareness  http://www.CraneSoftwrights.com/s/bc


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




Current Thread
Keywords