复制XSLT变量

时间:2010-09-30 21:02:01

标签: xml xslt variables umbraco

我正在制作Umbraco XSL样式表,而且我很困难。

基本上,我有一个参数,我测试并使用它的值,如果它存在,否则我使用默认参数$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=""属性的情况下复制变量。

  

更新:我尝试过使用其他方法(见下文),但我从范围异常中获取变量。

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

2 个答案:

答案 0 :(得分:6)

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

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

在您的情况下,这会转换为

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

完整的<xsl:variable>定义

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

答案 1 :(得分:2)

每当您在没有@select的情况下在XSLT 1.0中声明变量,但是使用某个内容模板时,变量类型将是Result Tree Fragment。该变量保存此树片段的根节点。

所以,有了这个:

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

您声明$source是RTF的根,其中包含$currentPage节点集中节点(自身和后代)的副本

您不能将/步骤运算符与RTF一起使用。这就是你使用node-set扩展功能的原因。

但是,当你说:

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

这将被评估为空节点集,因为根节点没有祖先,它不是元素。

编辑:如果您有两个节点集,并且您希望根据某些条件声明一个具有两个节点集之一的内容的变量,则可以使用:

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