XSL 在变量中存储节点集 [英] XSL store node-set in variable

查看:22
本文介绍了XSL 在变量中存储节点集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(XSLT 1.0.)给定一个名为 Rows 的变量,其中包含以下内容(示例):

(XSLT 1.0.) Given a variable called Rows which contains the following (example):

输入

<AllResults>
    <Result>
      <subject>can be filtered by filter 1</subject>
      <type>can be filtered by filter 2</type>
      <date>can be filtered by filter 3</date>
    </Result>
    <Result> ...
    </Result>
</AllResults>

我有 3 个过滤器变量.对于每个过滤器,如果过滤器变量不为空,我想将过滤器应用于上面显示的输入.我想将过滤结果(与过滤器匹配的项目)存储到一个新变量中.我尝试了以下操作,但收到一条错误消息(filterResult)是结果树而不是节点集".Rows 变量是一个节点集,正如我使用调试器确定的那样.

I have 3 filter variables. For each filter, I'd like to apply the filter to the input shown above if the filter variable is not empty. I'd like to store the filtered result, the items that match the filters, into a new variable. I tried the following, but I got an error message about it (filterResult) being a "result tree instead of a node-set". The Rows variable is a node-set, as I have determined by using a debugger.

XSL 的一部分

<xsl:variable name="filterResult">
    <xsl:choose>
        <xsl:when test="$filter1 != '' and $filter2 != '' and $filter3 != ''">
            <xsl:copy-of select="$Rows[date=$filter1 and type=$filter2 and subject=$filter3]" />
        </xsl:when>
        <xsl:when test="$filter1 != '' and $filter2 != ''">
            <xsl:copy-of select="$Rows[date=$filter1 and type=$filter2]" />
        </xsl:when>
        <xsl:when test="$filter1 != '' and $filter3 != ''">
            <xsl:copy-of select="$Rows[date=$filter1 and subject=$filter3]" />
        </xsl:when>
        <xsl:when test="$filter3 != '' and $filter2 != ''">
            <xsl:copy-of select="$Rows[type=$filter2 and subject=$filter3]" />
        </xsl:when>
        <xsl:when test="$filter1 != ''">
            <xsl:copy-of select="$Rows[date=$filter1]" />
        </xsl:when>
        <xsl:when test="$filter3 != ''">
            <xsl:copy-of select="$Rows[subject=$filter3]" />
        </xsl:when>
        <xsl:when test="$filter2 != ''">
            <xsl:copy-of select="$Rows[type=$filter2]" />
        </xsl:when>
        <xsl:otherwise> 
            <xsl:copy-of select="$Rows" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:variable>

我意识到 copy-of 会生成一个结果树而不是一个节点集,但是我不确定如何根据我上面描述的 3 个过滤器要求生成一个节点集.

I realize that copy-of produces a result tree and not a node-set, but I am not sure HOW to produce a node set given my 3 filters requirement that I described above.

附加信息

我确实知道我可以做一些类似 <xsl:variable name="me" select="/set/node"/> 的事情,它会创建一个包含节点集的变量,但是我看不出这对我有什么帮助,因为我有很多可能的条件(考虑到三个过滤器).

I do know that I could do something like <xsl:variable name="me" select="/set/node"/> which would create a variable containing a node set but I don't see how that helps me, since I have a lot of possible conditions (given the three filters).

推荐答案

看起来 $Rows 变量是一个 Result Tree Fragment 类型的实例.

It looks like $Rows variable is an instance of type Result Tree Fragment.

除了字符串操作之外,您不能对 RTF 执行任何操作(例如 [] 过滤器表达式提供的操作):来自 http://www.w3.org/TR/xslt#section-Result-Tree-Fragments

You can't perform any operation (like the one provided by [] filter expression) on RTF other than string operations: from http://www.w3.org/TR/xslt#section-Result-Tree-Fragments

允许对结果进行操作树片段仅当该操作将允许在字符串上(对字符串的操作可能涉及首先将字符串转换为数字或布尔值).特别是,它不允许使用 /, //,和 [] 结果树上的运算符片段.

An operation is permitted on a result tree fragment only if that operation would be permitted on a string (the operation on the string may involve first converting the string to a number or boolean). In particular, it is not permitted to use the /, //, and [] operators on result tree fragments.

除此之外,如果 $Rows 是节点集数据类型的实例,则所有显示的代码都可以简化:

Besides that, all the showed code could be simplified if $Rows is an instance of node set data type like this:

<xsl:variable name="filterResult" 
              select="$Rows[(date=$filter1 or $filter1='')
                               and 
                            (type=$filter2 or $filter2='')
                               and 
                            (subject=$filter3 or $filter3='')]"/>

有 XSLT 处理器(大部分都是)提供 node-set() RTF 到节点集转换的扩展函数.

There are XSLT processor (mostly every one) that provide implementations of node-set() extension function for RTF to node set conversion.

这篇关于XSL 在变量中存储节点集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆