xslt - exsl :node-set treat as the root node? 是什么

标签 xslt exslt node-set

我无法理解 exsl:node-set 函数的工作原理。

我有一些 XML,我正在扩展并使用它来动态填充 exsl:node-set。假设它目前采用这种格式:

<xsl:variable name="wrap">
    <nodes>
        <node/>
        <node/>
        <node/>
    </nodes>
</xsl:variable>
<xsl:variable name="wrapNodeSet" select="exsl:node-set($wrap)"/>

这按要求工作并且输出 $wrapNodeSet 显示上面的节点标记。根节点名称使用名称 ($wrapNodeSet/*) 显示为“节点”。

现在我需要将其扩展为具有 2 个节点并动态填充节点集。所以:

    <xsl:variable name="wrap">
    <nodes tier="a">
        <node/>
        <node/>
        <node/>
    </nodes>
    <nodes tier="b">
        <node/>
        <node/>
        <node/>
    </nodes>
</xsl:variable>
<xsl:variable name="wrapNodeSet" select="exsl:node-set($wrap)/nodes[@tier='b']"/>

输出节点集包括节点元素,但输出根节点的名称现在更改为“节点”。

有人可以解释为什么节点元素仍然输出吗?

最佳答案

<xsl:variable name="wrapNodeSet" 
              select="exsl:node-set($wrap)/nodes[@tier='b']"/>

这从使用 node-set() 扩展获得的树中选择任何 nodes 元素,(nodes 元素)有一个tier 属性值为 'b'

然后:

name($wrapNodeSet/*)

生成包含在 $wrapNodeSet 中的第一个节点的第一个子元素的名称。由于 $wrapNodeSet 只包含一个 nodes 元素,而 nodes 元素只有 node 子元素,第一个这样的子元素是一个 node 元素及其名称,因为任何 node 元素的名称都是 "node"

为什么您会在此结果中看到任何意外?

换句话说:

ext:node-set()$wrap 上的应用在这两种情况下都会产生相同的树,但在第二种情况下,您使用不同的、更长的 XPath ext:node-set($wrap) 结果的表达式——因此您从这两个不同的 XPath 表达式的计算中得到不同的结果。

关于xslt - exsl :node-set treat as the root node? 是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11291505/

相关文章:

xslt - 我可以使用 XSLT 忽略 namespace ,然后在同一个样式表中执行更多转换吗?

xslt - Sorted exsl :node-set. 按位置返回节点

xml - 如何在 Firefox 中进行多个 XSL 转换?

xml - 如何在遍历节点集时引用源 XML?

c# - 从坐标节点索引

javascript - 查询选择器全部 : manipulating nodes

xml - XPath child::* 与 child::node()

xml - 使 XSL 样式表与分页 XML 一起使用

xml - Xpath/XSLT : check if following sibling is a particular node

xslt-1.0 - 需要在 XSLT 1.0 中创建带参数的函数