复制 XSLT 变量
我正在开发 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>
I'm working on an Umbraco XSL Stylesheet and I am pretty stuck.
Basically, I have a parameter that I test and use it's value if it's present, otherwise I use the default parameter $currentPage
.
Here are the parameters
<xsl:param name="source" select="/macro/sourceId" />
<xsl:param name="currentPage" />
Here's the variable
<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>
And here's where I use it
<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>
In a nutshell
This works
<xsl:variable name="source" select="$currentPage" />
This doesn't
<xsl:variable name="source">
<xsl:copy-of select="$currentPage" /> <!-- Have tried using <xsl:value-of /> as well -->
</xsl:variable>
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 > 0">
<xsl:variable name="current" select="umbraco.library:GetXmlNodeById($source)" />
</xsl:when>
<xsl:otherwise>
<xsl:variable name="current" select="$currentPage" />
</xsl:otherwise>
</xsl:choose>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,此表达式根据给定条件是
true()
还是false()
选择两个节点集之一:In您的情况这将转换为:
完整的
定义是:这可以用更紧凑的方式编写:
Generally, this expression selects one of two nodesets, based on whether a given condition is
true()
orfalse()
:In your case this translates to:
The complete
<xsl:variable>
definition is:This can be written in a more compact way:
每当您在 XSLT 1.0 中声明一个不带 @select 的变量,但使用某些内容模板时,该变量类型将为结果树片段。该变量保存该树片段的根节点。
因此,这样:
您将
$source
声明为 RTF 的根,其中包含$currentPage
副本 > 节点集。您不能将
/
步进运算符与 RTF 一起使用。这就是您使用node-set
扩展函数的原因。但是,当你说:
这将被评估为一个空节点集,因为根节点没有祖先并且它不是一个元素。
编辑:如果您有两个节点集,并且您想根据某些条件声明一个包含两个节点集之一的内容的变量,您可以使用:
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:
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 ussingnode-set
extension function.But, when you say:
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: