Re: [xsl] key with restricting the scope of use attribute

Subject: Re: [xsl] key with restricting the scope of use attribute
From: Dan Diebolt <dandiebolt@xxxxxxxxx>
Date: Thu, 10 May 2001 16:35:47 -0700 (PDT)
Perhaps I need to clarify my own question with some example XML.
It is a gross simplification, but assume this is my XML:

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

<root>
   <a>       <b id="1"  x="1" y="1"/> 
             <b id="2"  x="1" y="2"/> 
             <b id="3"  x="2" y="2"/> 
             <b id="4"  x="1" y="1"/> </a>
   <a><a>    <b id="5"  x="1" y="2"/> 
             <b id="6"  x="1" y="1"/> </a></a>
   <a><a><a> <b id="7"  x="1" y="1"/> </a></a></a>

   <c>       <b id="8"  x="1" y="1"/> </c>
   <c><a>    <b id="9"  x="1" y="1"/> 
             <b id="10" x="2" y="1"/> 
             <b id="11" x="2" y="1"/> 
             <b id="12" x="1" y="2"/> </a></c>
   <c><a><a> <b id="13" x="2" y="1"/> </a></a></c>

   <d>       <b id="14" x="1" y="1"/> 
             <b id="15" x="1" y="1"/> </d>
   <d><a>    <b id="16" x="1" y="2"/> 
             <b id="17" x="1" y="1"/> </a></d>
   <d><a><a> <b id="18" x="1" y="1"/> 
             <b id="19" x="1" y="2"/>
             <b id="20" x="1" y="2"/>
             <b id="21" x="1" y="1"/></a></a></d>
</root>

I need to 'visit' every "//a/b" node and evaluate a key in the
context of this node. At each node visited, I need to look for
a complex value which for this problem I will represent as
use="concat(@x,':',@y)". However, I want to restrict the nodes
returned by key() to those matching the current node, namely
what is currently matched by "//a/b". What I am asking the list
is how you would approach this problem and what alternatives
there might be to implement it. What I came up with is shown
below and relies upon concatenating a generate-id() into the
use attribute as follows:

  <xsl:key name="MyKey" match="a/b" 
    use="concat(generate-id(),':',@x,':',@y)"/>
 
Is there another way to accomplish the same result?

Regards,

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

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
 <xsl:key name="MyKey" match="a/b" 
  use="concat(generate-id(),':',@x,':',@y)"/>
 <xsl:template match="/">
  <xsl:apply-templates select="//a/b"/>
 </xsl:template>
 <xsl:template match="a/b">
  <!-- complex code that results in $xyCombo -->
  <xsl:variable name="xyCombo" select="'1:1'"/>
  <xsl:value-of select="@id"/>:
  <xsl:for-each select="key('MyKey',concat(generate-id(),':',$xyCombo))">
   @xyCombo=<xsl:value-of select="$xyCombo"/>
  </xsl:for-each>   
  <br/>
 </xsl:template>
</xsl:stylesheet>

Output correctly identifying "1:1" b nodes 
1: @xyCombo=1:1
2: 
3: 
4: @xyCombo=1:1
5: 
6: @xyCombo=1:1
7: @xyCombo=1:1
9: @xyCombo=1:1
10: 
11: 
12: 
13: 
16: 
17: @xyCombo=1:1
18: @xyCombo=1:1
19: 
20: 
21: @xyCombo=1:1


__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/

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


Current Thread