Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 18th, 2005, 12:38 PM
Authorized User
 
Join Date: Oct 2004
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to return nodeset in template in xsl:variable

I have a xsl:variable which is to return one node or another depending on which is found

<xsl:variable name="wantednode">
  <xsl:choose>
    <xsl:when test="/xmlpage/somesection/wantednode">

    </xsl:when>
    <xsl:otherwise>

    </xsl:otherwise>
  </xsl:choose>
<xsl:variable>

The problem is that I want to then select nodes within $wantednode, so the template body needs to return a nodeset. therefore I can't use <xsl:value-of ...> as i could if it was just a string i needed. I thought <xsl:copy-of... would work, but alas no.

Any ideas?
 
Old March 18th, 2005, 12:43 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Unfortunately in XSLT version one you can't, not without an extension. If you are using MSXML then you need the node-set function, see here for examples:

http://msdn.microsoft.com/library/de...asp?frame=true

You will need to declare the appropriate namespace in your stylesheet.

--

Joe (Microsoft MVP - XML)
 
Old March 18th, 2005, 01:30 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If you know that only one of somesection and defaultsection will be present, you can write

<xsl:variable name="wantednode"
select="/xmlpage/somesection/wantednode | /xmlpage/defaultsection/wantednode"/>

(incidentally, you shouldn't use names beginning "xml" - they are reserved for future standardization)

If you know that if both are present, somesection will come first, then you can write

<xsl:variable name="wantednode"
select="(/xmlpage/somesection/wantednode | /xmlpage/defaultsection/wantednode)[1]"/>

If you don't know the order, you can write:

<xsl:variable name="wantednode"
select="/xmlpage/somesection/wantednode | /xmlpage/defaultsection/wantednode[
   not(/xmlpage/somesection/wantednode)]"/>



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 18th, 2005, 01:36 PM
Authorized User
 
Join Date: Oct 2004
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Micheal, excellent lateral thinking! No need to use the template bit after all.

Incidentally those were not my real node names, they were just made up for the sake of the example.

Cheers





Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript Function return save in XSL Variable Vrokad XSLT 2 June 2nd, 2007 02:54 AM
ASSIGNING A JAVA SCRIPT VARIABLE TO A XSL VARIABLE SOMANATHAN10 XSLT 1 February 21st, 2007 04:26 AM
The difference between xsl:variable and xsl:param NEO1976 XML 2 July 24th, 2006 06:05 AM
xsl:call-template name="$variable" problem chemi XSLT 1 October 6th, 2005 07:28 AM
How to create and return a nodeset in a javascript jerry.gadd XSLT 8 October 17th, 2003 05:48 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.