xsl-list
[Top] [All Lists]

Re: xsl:key on variable containing result of apply-templates

2002-11-24 16:36:13
Hello Grainne,

your approach can't work, because via

<xsl:variable name="alphabet-NodeSet"
select="document('')/xsl:stylesheet/xsl:variable[(_at_)name='alphabet']"

you access only the uninterpreted content of the variable, so only <xsl:apply-templates/>, not the result of the applied templates.

<xsl:variable name="alphabet">
  <xsl:apply-templates select="/test/alphabetList/alphabet"
                       mode="loadList"/>
</xsl:variable>

The same applies for the key declaration. The variable contains

<alphabet col="1">a</alphabet>
<alphabet col="2">b</alphabet>
<alphabet col="3">c</alphabet>
<alphabet col="4">d</alphabet>
<alphabet col="5">e</alphabet>

what you already get as result. The key declaration must be then:

<xsl:key name="alphabets" match="alphabet" use="@col"/>

And later the usage of key():

<xsl:template match="numbers">
  <xsl:for-each select="number">
    <xsl:variable name="curPosition" select="position()"/>
    <xsl:for-each select="$alphabet-exslNodeSet">
      <!-- context changed to converted node set -->
      <xsl:copy-of select="key('myKey', $curPosition)"/>
    </xsl:for-each>
  </xsl:for-each>
</xsl:template>

Shortening the template matching on root node / to

<xsl:template match="/">
  <xsl:apply-templates select="test/numbers"/>
</xsl:template>

this should again result in

<alphabet col="1">a</alphabet>
<alphabet col="2">b</alphabet>
<alphabet col="3">c</alphabet>
<alphabet col="4">d</alphabet>
<alphabet col="5">e</alphabet>

So there is no difference, but everything is processed. I don't know how your output should look like, maybe then I can help you a bit more.

Furthermore this way seems to be extremely complicated. Why the way using alphabet variable and the resulting need for an extension function. You said your original code is much more complex, so I showed you this way, but maybe it's easier to set the key directly on the input XML? This shell result in better processor times. The only thing you have to change is the template for numbers:

<xsl:template match="numbers">
  <xsl:for-each select="number">
    <xsl:copy-of select="key('myKey', position())"/>
  </xsl:for-each>
</xsl:template>

But of course you must know your requirements.

I hope I made no mistakes or forgot anything. Try it out and tell me whether it helps or not.

Regards,

Joerg


Grainne Reilly wrote:
Hello,
This is my first post to this list and I apologize for it's length. My question basically is whether it is possible to create a key on a variable which contains the result of applying a template? In my real stylesheet and xml the template which creates the node-set I would like to create the key on is much more complicated than the example I have given below - but the concept is the same. I apply-templates to the source document and the result tree fragment is kept in a variable ($alphabet). I convert this to a node-set ($alphabet-exslNodeSet) and then use that node-set in another template. It works but is pretty slow - I think it would be much faster if I were able to create a key on the node-set (each item in the node set is used multiple times). I can't see how to do that - it looks like my attempt below gives me the code in the variable, not the result. I know I can't use a variable in the key's match attribute, so I'm at a loss how to do this.
Thanks for any suggestions,
Grainne.

The Stylesheet:
============
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:saxon="http://icl.com/saxon"; xmlns:exsl="http://exslt.org/common";
exclude-result-prefixes="ss"
extension-element-prefixes="exsl saxon">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    <xsl:variable name="alphabet">
<xsl:apply-templates select="/test/alphabetList/alphabet" mode="loadList"/>
    </xsl:variable>

<xsl:variable name="alphabet-NodeSet" select="document('')/xsl:stylesheet/xsl:variable[(_at_)name='alphabet']" /> <xsl:variable name="alphabet-exslNodeSet" select="exsl:node-set($alphabet)"/> <xsl:key name="myKey" match="/*/xsl:variable[(_at_)name='alphabet']" use="@col"/>

    <xsl:template match="alphabet" mode="loadList">
        <xsl:copy-of select="."/>
    </xsl:template>

    <xsl:template match="*"/>
<xsl:template match="/">
        Value of alphabet variable is:<xsl:text>
        </xsl:text> <xsl:copy-of select="$alphabet"/><xsl:text>
        </xsl:text>
Value of alphabet-NodeSet varaible is (I think this is what I get in the key!!)<xsl:text>
        </xsl:text><xsl:copy-of select="$alphabet-NodeSet"/><xsl:text>
        </xsl:text>
        Value of alphabet-exslNodeSet variable is <xsl:text>
</xsl:text><xsl:copy-of select="$alphabet-exslNodeSet"/> <xsl:text>
        </xsl:text>
<xsl:apply-templates select="test/numbers"/> </xsl:template> <xsl:template match="numbers">
        <xsl:for-each select="number">
            <xsl:variable name="curPosition" select="position()"/>
Using the key to get the alphabet element (THIS is what I'd like to do since each node in the set is used multiple times):
            <number value="{.}">
            <xsl:for-each select="document('')">
                <xsl:copy-of select="key('myKey',$curPosition)"/>
            </xsl:for-each>
            </number>
Using the exslNodeSet variable to get the alphabet element (what I am doing but which is very slow in my real code):
            <number value="{.}">
<xsl:copy-of select="$alphabet-exslNodeSet/alphabet[(_at_)col=$curPosition]"/>
            </number>
            <xsl:text>
</xsl:text> </xsl:for-each> </xsl:template> </xsl:stylesheet>



Example xml:
==========
<?xml version="1.0" encoding="UTF-8"?>
<test>
    <alphabetList>
        <alphabet col="1">a</alphabet>
        <alphabet col="2">b</alphabet>
        <alphabet col="3">c</alphabet>
        <alphabet col="4">d</alphabet>
        <alphabet col="5">e</alphabet>
    </alphabetList>
    <numbers>
        <number>one</number>
        <number>two</number>
        <number>three</number>
        <number>four</number>
        <number>five</number>
    </numbers>
</test>

Result (tidied up a little):
====================
<?xml version="1.0" encoding="UTF-8"?>
Value of alphabet variable is:
<alphabet col="1">a</alphabet>
<alphabet col="2">b</alphabet>
<alphabet col="3">c</alphabet>
<alphabet col="4">d</alphabet>
<alphabet col="5">e</alphabet>
Value of alphabet-NodeSet varaible is (I think this is what I get in the key!!) <xsl:variable xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:saxon="http://icl.com/saxon"; xmlns:exsl="http://exslt.org/common"; name="alphabet"> <xsl:apply-templates select="/test/alphabetList/alphabet" mode="loadList"/>
</xsl:variable>
Value of alphabet-exslNodeSet variable is
<alphabet col="1">a</alphabet>
<alphabet col="2">b</alphabet>
<alphabet col="3">c</alphabet>
<alphabet col="4">d</alphabet>
<alphabet col="5">e</alphabet>
Using the key to get the alphabet element (THIS is what I'd like to do since each node in the set is used multiple times):
            <number value="one"/>
Using the exslNodeSet variable to get the alphabet element (what I am doing but which is very slow in my real code):
            <number value="one">
               <alphabet col="1">a</alphabet>
            </number>
...<snip>... Using the key to get the alphabet element (THIS is what I'd like to do since each node in the set is used multiple times):
            <number value="five"/>
Using the exslNodeSet variable to get the alphabet element (what I am doing but which is very slow in my real code):
            <number value="five">
               <alphabet col="5">e</alphabet>
            </number>



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