This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

simulating multidimensional keys - way cool


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/>
  </xsl:for-each>
 
The use of concatenations in

   use="concat(@a,':',@b)" 

   select="key('Lookup','$c:$d')"

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.

Regards,

Dan
------------------------------


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

<root>
 <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"/>
</root>

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>
 
 <xsl:template match="root">
  <xsl:for-each select="key('Lookup','1:1')">
   <xsl:value-of select="@c"/>:<xsl:value-of select="@d"/><br/>
  </xsl:for-each>
 </xsl:template>
   
</xsl:stylesheet>

__________________________________________________
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail. 
http://personal.mail.yahoo.com/

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]