Tom,
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 -->
</my:nflmap>
<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">
<nflCities>
<xsl:apply-templates/>
</nflCities>
</xsl:templates>
<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 -->
<city>
<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 -->
</city>
</xsl:for-each>
</xsl:template>
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!
Cheers,
Wendell
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
<nflTeamNames>
<name>49ers</name>
<name>Cowboys</name>
.
.
.
<name>Seahawks</name>
<nflTeamNames>
Here is what I want the output to look like:
<nflCities>
<city>San Francisco</city>
<city>Dallas</city>
.
.
.
<city>Seattle</city>
</nflCities>
In case you don't know American football the 49ers are the San Francisco
team, the Cowboys are the Dallas team and so on. :-)
Regards,
Tom
======================================================================
Wendell Piez
mailto:wapiez(_at_)mulberrytech(_dot_)com
Mulberry Technologies, Inc. http://www.mulberrytech.com
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: http://www.mulberrytech.com/xsl/xsl-list