RE: [xsl] xsl:variable and key

Subject: RE: [xsl] xsl:variable and key
From: "M. David Peterson" <m.david@xxxxxxxxxx>
Date: Thu, 10 Jun 2004 21:02:04 -0600
Hey Sameer,

If you use <xsl:copy-of select="key('keyname', selector)"/> you should
get the results you are looking for.

Alternatively you can build a nodeset using apply-templates like so
(remember to use your processors node-set() function to convert it to a
true node-set if you plan to use the variable in a select attribute
later in the stylesheet):

<xsl:element name="dynamic_or_hard_coded_element_name"> 
	<xsl:apply-templates select="key('keyname', selector)"
mode="buildFromKey"/>
</xsl:element>

And then build the node set in the template:

<xsl:template match="*" mode=" buildFromKey">
	<xsl:element name="{local-name()}">
		<!-- use copy of to make a copy of the desired
attributes from the element, @* if you want them all -->
		 <xsl:copy-of select="@*"/>
		<!-- or use xsl:attribute to create attributes with
different names than the original or from child elements of this
element, etc... -->
		<xsl:attribute name="foo"><xsl:value-of
select="childElement"/></xsl:attribute>
	</xsl:element>
</xsl:template>

The first method is much easier obviously and is appropriate when
creating an exact copy (or selective copy using XPath).

The second is appropriate when you plan to change the names of elements
or attributes and/or create brand new elements or attributes. Its also
handy when you want to use the value of a different element or attribute
for the value of an existing or new element or attribute.

Hope this helps!

Best regards,

<M:D/>



-----Original Message-----
From: Sameer N [mailto:samooo_vc@xxxxxxxxxxx] 
Sent: Thursday, June 10, 2004 8:35 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] xsl:variable and key

I want to assign a node-set returned by key() to a variable.

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

But now I have a condition and I have to use <xsl:choose>. 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? Thanks.

Sameer.

_________________________________________________________________
Is your PC infected? Get a FREE online computer virus scan from McAfee® 
Security. http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963


--+------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe@xxxxxxxxxxxxxxxxxxxxxx>
--+--


Current Thread