[xsl] simulating multidimensional keys - way cool

Subject: [xsl] simulating multidimensional keys - way cool
From: Dan Diebolt <dandiebolt@xxxxxxxxx>
Date: Sun, 8 Apr 2001 15:15:36 -0700 (PDT)
I thought I would pass on this technique as I never saw it before
in the context of XSLT. I needed to create a key with *two* inputs
based on <item> definitions in the source XML:

  <item a="1" b="1" c="4" d="5"/>

This element represents the definition of one mapping from {@a,@b} to 
{@c,@d}. With the following key definition:

  <xsl:key name="Lookup" match="root/item" use="concat(@a,':',@b)"/>

You can recover {@c,@d} with the following:

  <xsl:for-each select="key('Lookup','$a:$b')">
   @c=<xsl:value-of select="@c"/>
   @d=<xsl:value-of select="@d"/><br/>
The use of concatenations in



is pretty much the same technique used to simulate multidimensional 
arrays in Perl/Tcl. You simply need to insure {@a,@b,@c,@d} don't
include the seperator character (here a colon) you employ.



<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="TwoDKey.xsl"?>

 <item a="1" b="1" c="4" d="5"/>
 <item a="1" b="2" c="5" d="5"/>
 <item a="2" b="1" c="4" d="6"/>
 <item a="2" b="2" c="5" d="6"/>

File: File:TwoDKey.xsl
<?xml version="1.0"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
 <xsl:key name="Lookup" match="root/item" use="concat(@a,':',@b)"/>
 <xsl:template match="/">
  <xsl:apply-templates select="root"/>
 <xsl:template match="root">
  <xsl:for-each select="key('Lookup','1:1')">
   <xsl:value-of select="@c"/>:<xsl:value-of select="@d"/><br/>

Do You Yahoo!?
Get email at your own domain with Yahoo! Mail. 

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

Current Thread