Re: [xsl] using keys as cross-reference

Subject: Re: [xsl] using keys as cross-reference
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Sun, 20 Jan 2002 14:33:04 -0500
At 2002-01-20 07:38 -0800, Xiaocun Xu wrote:
  I am interested to implement cross-reference using
keys  (suggested by Jeni as well as documented in the
XSLT Programmer Reference 2nd Ed. on p.501-3).
  My situation is I have more readable headers in
Excel (i.e. *Organization Unique ID)that needs to be
translated into DTD attribute names (i.e.
OrganizationCode).  I intend to put the name-value
mapping in a separate XML, which I want to use keys to
retrieve the translation.

This is not a problem because the <xsl:key> instruction builds a key table for every source file, not just the principle source file.


Are there other more effective solutions?

All you need do is change your focus to the name-value file before calling the key() function, this will then give you the key() results from the name-value file instead of the principle source file. This can be done with <for-each>.


I've thrown together something below to illustrate how this, though I have no idea how you were planning to get at your attribute values. I've just created a translation of the first attribute in each element.

I hope this helps.

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


t:\ftemp>type xlate.xml <?xml version="1.0" encoding="utf-8"?> <categories> <category ref="OrganizationCode">Organization Unique ID</category> <category ref="OrganizationName">Organization Name</category> </categories> t:\ftemp>type xiaocun.xml <?xml version="1.0" encoding="utf-8"?> <items> <item OrganizationCode='123'/> <item OrganizationName='ABC Corporation'/> <item OrganizationCode='234'/> </items> t:\ftemp>type xiaocun.xsl <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">

<xsl:variable name="headings" select="document('xlate.xml')"/>

<xsl:key name="headings" match="category" use="@ref"/>

<xsl:output indent="yes"/>

<xsl:template match="/">
  <results>
    <xsl:for-each select="//item">
      <result>
        <xsl:variable name="attr" select="name(@*)"/>
        <xsl:for-each select="$headings">
          <xsl:value-of select="key('headings',$attr)"/>
        </xsl:for-each>
        <xsl:text>: </xsl:text>
        <xsl:value-of select="@*"/>
      </result>
    </xsl:for-each>
  </results>
</xsl:template>

</xsl:stylesheet>

t:\ftemp>saxon xiaocun.xml xiaocun.xsl
<?xml version="1.0" encoding="utf-8"?>
<results>
   <result>Organization Unique ID: 123</result>
   <result>Organization Name: ABC Corporation</result>
   <result>Organization Unique ID: 234</result>
</results>
t:\ftemp>rem Done!



--
Upcoming: 3-days XSLT/XPath and/or 2-days XSLFO - Feb 18-22, 2002

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 (Fax:-0995)
ISBN 0-13-065196-6                        Definitive XSLT & XPath
ISBN 1-894049-08-X  Practical Transformation Using XSLT and XPath
ISBN 1-894049-07-1               Practical Formatting Using XSLFO
XSL/XML/DSSSL/SGML/OmniMark services, books(electronic, printed),
articles, training(instructor-live,Internet-live,web/CD,licensed)
Next public training:  02-02-11,12,14,15,18,21,03-04,05,06,08,11,
-                                04-08,09,10,12,05-14,15,06-04,07


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



Current Thread