tags:

views:

24

answers:

1

Hi!

I wonder how to store xml data from one variable in another.

This works ($oldvariable contains xml data):

<xsl:variable name="newvariable" select="$oldvariable"/>

But this does not work (probably because of some obvious reason for an experienced XSLT-coder):

<xsl:variable name="newvariable">
 <xsl:copy-of select="$oldvariable"/>
</xsl:variable>

How can I make the latter store the exact variable data?

I need that construct since I'm really using a :

<xsl:variable name="newvariable">
<xsl:choose>
 <xsl:when test="some-test">
  <xsl:copy-of select="$oldvariable"/>
...

Thanks alot!

+3  A: 

This is FAQ: In XSLT 1.0, whenever you declare a variable/parameter with content template (without @select), the result type is Result Tree Fragment.

Then, you can't use RTF as left hand for / step operator.

So, how do you declare a variable to be one of two node-sets based on a condition?

<xsl:variable name="newvariable" select="$oldvariable[$condition]|
                                         $othernodeset[not($condition)]"/> 
Alejandro
+1 for a best possible answer.
Dimitre Novatchev
Wow, man, thats awesome XSLT magic! Thanks!
joeriks
@joeriks: You are wellcome! Please, do not that the context for predicate evaluation will be each node in node-set at a time. You might need a "context free" condition, mainly with some variable/parameter reference.
Alejandro
like such <xsl:variable name="condition" select="$foo='test'"/> ?
joeriks
And | is a Union operator, nothing else. Right?
joeriks
with my original way, I should be able to convert the RTF (thanks for pointing out that's what I've got) to a useful node-set with <xsl:variable name="newvariable-node-set" select="exsl:node-set($newvariable)"/> yes?
joeriks
@joeriks: About condition: `$foo='test'` is already a "context free" condition. So you could use it like `$oldvariable[$foo='test']`. You are right about `|` union set operator. At last, RTF was a bad design choice of the XSLT WG. `node-set()` is an *extension function*: mostly every processor have an implementation but under differents namespaces.
Alejandro