Altova Mailing List Archives

Re: [xsl] using xsl to substitute synonyms or translate

From: Wendell Piez <wapiez@---------------->
Date: 1/7/2003 11:41:00 AM

This is not hard to implement in XSLT, assuming of course your names/teams 
relations are known and fixed (no fancy logic like a team being in one city 
in-season, another city off-season ;-).

The way I would do it uses a couple of techniques that aren't "first day" 
material, but are still pretty easy to understand. For one, the document 
function when given the empty string as argument -- document('') -- 
retrieves the stylesheet itself as an XML document. For another, keys can 
serve as a way of optimizing retrieval of nodes based on values associated 
with those nodes. (This particular problem is tractable without keys but I 
like their clarity and conciseness; and they help the processor optimize.) 
So, in your stylesheet,

<my:nflmap xmlns:my="http://my.namespace">
  <!-- this element, in a non-xsl namespace, will serve you as a
       kind of lookup table for your teams -->
  <team name="49ers" city="San Francisco"/>
  <team name="Cowboys" city="Dallas"/>
  <!-- etc. etc. for all teams -->

<xsl:variable name="nflmap" select="document('')/*/my:nflmap"/>
<!-- this variable saves having to find and load the table every time
     we want it, which would be expensive  -->

<xsl:key name="teamsbyname" match="team" use="@name"/>
<!-- sets up a key so we can retrieve team nodes by name -->

<xsl:template match="nflTeamNames">

<xsl:template match="name">
  <xsl:variable name="thisname" select="."/>
  <!-- binds our context node to a variable so we can get to it below -->
  <xsl:for-each select="$teamsbyname">
  <!-- the for-each changes our context node, which we need to do
       so we can use our key -->
      <xsl:value-of select="key('teamsbyname',$thisname)/@city"/>
      <!-- reports the string value of the @city attribute on the
           'team' (lookup) element node retrieved by the key -->

This approach scales pretty well (your list of 300 shouldn't be a problem), 
and with a bit of further work can be applied to many-to-many lookups or 
lookups in either direction ... also, there's no reason why your lookup 
table has to be in the stylesheet (as long as the stylesheet can find it 
using document()) -- that's just a convenience here.

I hope this helps!


At 03:36 PM 1/7/2003, you wrote:
I have a problem that I think is similar to translating words in one 
language to words in another language and I was wondering if I could use 
xsl to do it.

I know what my source input will be (example below) and I know what I want 
to output to be, but I can't fathom how to build an xsl to get the output.

I thought I could use the translate function, but there would be a long 
list of things to translate.  There are 32 teams and I want to use this 
process again (on something unrelated) for a list of over 300 items.

Here is what my source document will look like


Here is what I want the output to look like:

  <city>San Francisco</city>

In case you don't know American football the 49ers are the San Francisco 
team, the Cowboys are the Dallas team and so on. :-)



Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.      
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
  Mulberry Technologies: A Consultancy Specializing in SGML and XML

XSL-List info and archive:


These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.