Re: [xsl] xsl:variable and key

Subject: Re: [xsl] xsl:variable and key
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Thu, 10 Jun 2004 23:52:32 -0400
At 2004-06-10 19:34 -0700, Sameer N wrote:
I want to assign a node-set returned by key() to a variable.

This works fine : <xsl:variable name="foo" select="key('XXX', YYY)"/>

Sure.


But now I have a condition and I have to use <xsl:choose>.

Actually, for your example you are not obliged to use <xsl:choose> and there is a trick to getting what you want.


And in such case I am not able to set the variable.
The pseudo for what I want is something like this:
<xsl:variable name="foo">
 <xsl:choose>
    <xsl:when test="SOME-TEST">
         SELECT node-set returned by key('XXX', YYY)
    </xsl:when>
    <xsl:otherwise>
          SELECT node-set ancestor::*[1]
    <xsl:otherwise>
 </xsl:choose>
</xsl:variable>

Any suggesstions?

In XSLT 1.0 I would do the following:


<xsl:variable name="foo" select="key('XXX',YYY)[SOME-TEST] |
                                 ancestor::*[1][not(SOME-TEST)]"/>

Essentially I'm assigning the union of two node set expressions where only one of them will be non-empty because of the application of the predicate to each operand and one of the predicates will be true and the other will be false.

Better still you could distill out the test as:

<xsl:variable name="test" select="SOME-TEST"/>
<xsl:variable name="foo" select="key('XXX',YYY)[$test] |
                                 ancestor::*[1][not($test)]"/>

Not elegant, but it will do what you asked for ... are you sure that is what you want and there isn't a better way?

I hope this helps.

.................. Ken


-- Public training 3 days XSLT & 2 days XSL-FO: Phoenix,AZ 2004-08-23 World-wide on-site corporate, govt. & user group XML/XSL training. G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (F:-0995) Male Breast Cancer Awareness http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal


Current Thread