[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: using xsl to substitute synonyms or translate
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. ====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx 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
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|