复制 XSLT 变量 [英] Copying an XSLT variable

查看:29
本文介绍了复制 XSLT 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理 Umbraco XSL 样式表,但遇到了困难.

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

基本上,我有一个我测试的参数,如果它存在就使用它的值,否则我使用默认参数 $currentPage.

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

这里是参数

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

这是变量

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

这是我使用它的地方

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


简而言之

这有效

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

这不会

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

那么如何在不使用 select="" 属性的情况下复制变量.

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>

推荐答案

通常,此表达式会根据给定条件是 true() 还是 选择两个节点集之一>false():

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

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

在你的情况下,这转化为:

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

这可以写成更紧凑的方式:

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

<xsl:variable name="vCurrent" select=
    "$vRealSource| $currentPage[not($vRealSource)]"/>

这篇关于复制 XSLT 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆