复制 XSLT 变量

发布于 2024-09-26 06:41:32 字数 1841 浏览 0 评论 0原文

我正在开发 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>

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-10-03 06:41:32

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

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

In您的情况这将转换为

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

完整的定义是

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

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

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

In your case this translates to:

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

This can be written in a more compact way:

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

<xsl:variable name="vCurrent" select=
    "$vRealSource| $currentPage[not($vRealSource)]"/>
醉生梦死 2024-10-03 06:41:32

每当您在 XSLT 1.0 中声明一个不带 @select 的变量,但使用某些内容模板时,该变量类型将为结果树片段。该变量保存该树片段的根节点。

因此,这样:

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

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:

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

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 ussing node-set extension function.

But, when you say:

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

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:

<xsl:variable name="current" 
              select="umbraco.library:GetXmlNodeById($source)[$source > 0]
                      |$currentPage[0 >= $source]" /> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文