Conditionally assigning nodesets to a variable

  • Thread starter Hans-Michael Rupp
  • Start date
H

Hans-Michael Rupp

Hello!

I need to a assign a node(-set) conditionally to a varialbe like:

<xsl:variable name="foo">
<xsl:if test="$bar=''">
<!-- how can I access a node here ?-->
</xsl:if>
<xsl:if test="not($bar='')">
<!-- how can I access a node here ?-->
</xsl:if>
</xsl:variable>

but how can I do it? I would need a <xsl:select> tag, which does not exist.

I would be gratefull for any suggestions.

Greetings,

Hans
 
C

Colin Mackenzie

you use any xslt that returns a value for example
<xsl:varibale name-"foo">
<xsl:if test="$bar=''">
<xsl:value-of select="/foo/bar">
</xsl:if>

or

<xsl:if test="$bar=''">
<xsl:apply-templates select="/foo/bar/x">
</xsl:if>

etc

if you only need to use the result of the apply templates as a string/number
etc then you access it using <xsl:value-of select="$foo"/>

but if it returns a node-set (e.g. foo/bar/x) then you must use a processor
specifc extension e.g.

<xsl:for-each select="msxsl:node-set($foo/bar/x)">

and declare a namespace dfor the extension

Colin
 
M

Marrow

Hi Hans,

If you need the variable to contain references to the original nodes (rather
than copies of the original nodes placed into a result tree fragment) then
you can usually accomplish this using XPath predicates. For example, taking
your original code and modifying it a little...

<xsl:variable name="foo">
<xsl:if test="$bar=''">
<xsl:copy-of select="//a"/>
</xsl:if>
<xsl:if test="not($bar='')">
<xsl:copy-of select="//b"/>
</xsl:if>
</xsl:variable>

then using XPath predicates you could do...
<xsl:variable name="foo" select="//a[$bar=''] | //b[not($bar='')]"/>

Hope this helps
Marrow
http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.topxml.com/Xselerator
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top