Question

Je travaille sur une feuille de style Umbraco XSL et je suis assez coincé.

Fondamentalement, j'ai un paramètre que je teste et utilise sa valeur s'il est présent, sinon j'utilise le paramètre par défaut $currentPage.

Voici les paramètres

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

Voici la 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>

Et voici où je l'utilise

<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>


En un mot

Cela marche

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

Ce n'est pas

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

Alors, comment copiez-vous une variable sans utiliser le select="" attribut.

METTRE À JOUR: J'ai essayé d'utiliser une autre approche (voir ci-dessous) mais j'obtiens un variable hors de portée 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>
Était-ce utile?

La solution

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)]"/>

Autres conseils

Whenever you declare a variable in XSLT 1.0 without @select, but with some content template, the variable type will be of Result Tree Fragment. The variable holds the root node of this tree fragment.

So, with this:

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

You are declaring $source as the root of a RTF containing the copy of the nodes (self and descendants) in $currentPage node set.

You can't use / step operator with RTF. That's why you are ussing node-set extension function.

But, when you say:

node-set($source)/ancestor-or-self::*

This will be evaluate to an empty node set, because a root node hasn't ancestors an it's not an element.

EDIT: If you have two node sets, and you want to declare a variable with the content of one of the two node sets depending on some condition, you could use:

<xsl:variable name="current" 
              select="umbraco.library:GetXmlNodeById($source)[$source > 0]
                      |$currentPage[0 >= $source]" /> 
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top