xsl-list
[Top] [All Lists]

Re: Looking up keys in a separate xml file

2004-01-06 07:17:19




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(_at_)us(_dot_)ibm(_dot_)com



                                                                                
                                                             
                      Scott Anguish                                             
                                                             
                      <sanguish(_at_)digifix(_dot_)com>            To:       
xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com                                
            
                      Sent by:                          cc:                     
                                                             
                      owner-xsl-list(_at_)lists(_dot_)mulbe        Subject:  
Re: [xsl] Looking up keys in a separate xml file                           
                      rrytech.com                                               
                                                             
                                                                                
                                                             
                                                                                
                                                             
                      01/06/2004 02:55 AM                                       
                                                             
                      Please respond to xsl-list                                
                                                             
                                                                                
                                                             
                                                                                
                                                             




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

<Prev in Thread] Current Thread [Next in Thread>