Mar*_*rko 7 xml xslt variables umbraco
我正在研究Umbraco XSL样式表,我很困惑.
基本上,我有一个参数,我测试并使用它的值,如果它存在,否则我使用默认参数$currentPage.
这是参数
<xsl:param name="source" select="/macro/sourceId" />
<xsl:param name="currentPage" />
Run Code Online (Sandbox Code Playgroud)
这是变量
<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>
Run Code Online (Sandbox Code Playgroud)
这就是我使用它的地方
<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>
Run Code Online (Sandbox Code Playgroud)
这有效
<xsl:variable name="source" select="$currentPage" />
Run Code Online (Sandbox Code Playgroud)
事实并非如此
<xsl:variable name="source">
<xsl:copy-of select="$currentPage" /> <!-- Have tried using <xsl:value-of /> as well -->
</xsl:variable>
Run Code Online (Sandbox Code Playgroud)
那么如何在不使用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>
Run Code Online (Sandbox Code Playgroud)
通常,此表达式根据给定条件是否为true()或选择两个节点集中的一个false():
$ns1[$cond] | $ns2[not($cond)]
Run Code Online (Sandbox Code Playgroud)
在您的情况下,这转换为:
umbraco.library:GetXmlNodeById($source)
|
$currentPage[not(umbraco.library:GetXmlNodeById($source))]
Run Code Online (Sandbox Code Playgroud)
完整的<xsl:variable>定义是:
<xsl:variable name="vCurrent" select=
" umbraco.library:GetXmlNodeById($source)
|
$currentPage[not(umbraco.library:GetXmlNodeById($source))]
"/>
Run Code Online (Sandbox Code Playgroud)
这可以用更紧凑的方式编写:
<xsl:variable name="vRealSource" select="umbraco.library:GetXmlNodeById($source)"/>
<xsl:variable name="vCurrent" select=
"$vRealSource| $currentPage[not($vRealSource)]"/>
Run Code Online (Sandbox Code Playgroud)