Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying an XSLT variable

I'm working on an Umbraco XSL Stylesheet and I am pretty stuck.

Basically, I have a parameter that I test and use it's value if it's present, otherwise I use the default parameter $currentPage.

Here are the parameters

<xsl:param name="source" select="/macro/sourceId" />
<xsl:param name="currentPage" />

Here's the variable

<xsl:variable name="current">
    <xsl:choose>
        <xsl:when test="$source &gt; 0">
            <xsl:copy-of select="umbraco.library:GetXmlNodeById($source)" />
        </xsl:when>
        <xsl:otherwise>
            <xsl:copy-of select="$currentPage" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:variable>

And here's where I use it

<xsl:for-each select="msxml:node-set($source)/ancestor-or-self::* [@isDoc and @level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']">
... code here ...
</xsl:for-each>


In a nutshell

This works

<xsl:variable name="source" select="$currentPage" />

This doesn't

<xsl:variable name="source">
  <xsl:copy-of select="$currentPage" /> <!-- Have tried using <xsl:value-of /> as well -->
</xsl:variable>

So how do you copy a variable without using the select="" attribute.

UPDATE: I've tried using another approach (see below) but I get a variable out of scope exception.

<xsl:choose>
    <xsl:when test="$source &gt; 0">
        <xsl:variable name="current" select="umbraco.library:GetXmlNodeById($source)" />
    </xsl:when>
    <xsl:otherwise>
        <xsl:variable name="current" select="$currentPage" />
    </xsl:otherwise>
</xsl:choose>
like image 568
Marko Avatar asked Sep 30 '10 21:09

Marko


People also ask

How do I copy nodes in XSLT?

XSLT <xsl:copy-of> The <xsl:copy-of> element creates a copy of the current node. Note: Namespace nodes, child nodes, and attributes of the current node are automatically copied as well! Tip: This element can be used to insert multiple copies of the same node into different places in the output.

What does xsl copy do?

Causes the current XML node in the source document to be copied to the output. The actual effect depends on whether the node is an element, an attribute, or a text node.

How do you assign a value to XSLT variable?

XSLT <xsl:variable> The <xsl:variable> element is used to declare a local or global variable. Note: The variable is global if it's declared as a top-level element, and local if it's declared within a template. Note: Once you have set a variable's value, you cannot change or modify that value!

Can we reassign a value to variable in XSLT?

Variables in XSLT are not really variables, as their values cannot be changed. They resemble constants from conventional programming languages.


1 Answers

Generally, this expression selects one of two nodesets, based on whether a given condition is true() or false():

$ns1[$cond] | $ns2[not($cond)]

In your case this translates to:

    umbraco.library:GetXmlNodeById($source) 
|
    $currentPage[not(umbraco.library:GetXmlNodeById($source))]

The complete <xsl:variable> definition is:

<xsl:variable name="vCurrent" select=
"        umbraco.library:GetXmlNodeById($source) 
    |
        $currentPage[not(umbraco.library:GetXmlNodeById($source))]
"/>

This can be written in a more compact way:

<xsl:variable name="vRealSource" select="umbraco.library:GetXmlNodeById($source)"/>

<xsl:variable name="vCurrent" select=
    "$vRealSource| $currentPage[not($vRealSource)]"/>
like image 71
Dimitre Novatchev Avatar answered Sep 25 '22 02:09

Dimitre Novatchev