题
我正在研究Umbraco XSL样式表,我很困惑。
基本上,我有一个参数,我可以测试并使用它的值,否则我使用默认参数 $currentPage
.
这是参数
<xsl:param name="source" select="/macro/sourceId" />
<xsl:param name="currentPage" />
这是变量
<xsl:variable name="current">
<xsl:choose>
<xsl:when test="$source > 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 > 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()
:
$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)]"/>
其他提示
每当您在没有@Select的XSLT 1.0中声明一个变量,但是使用某些内容模板,变量类型将是结果树片段。该变量保留了该树片段的根节点。
因此,这样做:
<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]" />
不隶属于 StackOverflow