XSLT xsl: 序列.到底有什么好处呢..? [英] XSLT xsl:sequence. What is it good for..?

查看:27
本文介绍了XSLT xsl: 序列.到底有什么好处呢..?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道下面的问题有点初学者,但我需要你的帮助来理解一个基本概念.

I know the following question is a little bit of beginners but I need your help to understand a basic concept.

首先我想说的是,我是一名 3 年的 XSLT 程序员,但我在这里学到了一些我从未知道的新的和非常基础的东西(在我的工作中,任何人都学会了如何单独编程,有不涉及课程).

I would like to say first that I'm a XSLT programmer for 3 years and yet there are some new and quite basics things I've been learning here I never knew (In my job anyone learns how to program alone, there is no course involved).

我的问题是:xsl:sequence 的作用是什么?

My question is: What is the usage of xsl:sequence?

我一直在使用 xsl:copy-of 来按原样复制节点,xsl:apply-templates 来修改我选择的节点和 简单文本的值.

I have been using xsl:copy-of in order to copy node as is, xsl:apply-templates in order to modifiy nodes I selected and value-of for simple text.

我从来没有必要使用 xsl:sequence.如果有人可以向我展示 xsl:sequence 用法示例,如果没有我上面提到的那些,这是首选的或无法实现的,我将不胜感激.

I never had the necessity using xsl:sequence. I would appreciate if someone can show me an example of xsl:sequence usage which is preferred or cannot be achieved without the ones I noted above.

还有一件事,我当然已经阅读了 xsl:sequence 定义,但我无法推断它有什么用处.

One more thing, I have read about the xsl:sequence definition of course, but I couldn't infer how it is useful.

推荐答案

在原子值(或原子值序列)上与 相同xsl:copy-of> 都只返回其输入的副本.当您考虑节点时,差异就会出现.

<xsl:sequence> on an atomic value (or sequence of atomic values) is the same as <xsl:copy-of> both just return a copy of their input. The difference comes when you consider nodes.

如果 $n 是一个单元素节点,例如由类似的东西定义

If $n is a single element node, eg as defined by something like

<xsl:variable name="n" select="/html"/>

然后

<xsl:copy-of select="$n"/>

返回节点的副本,它具有相同的名称和子结构,但它是一个具有新标识(并且没有父节点)的节点.>

Returns a copy of the node, it has the same name and child structure but it is a new node with a new identity (and no parent).

<xsl:sequence select="$n"/>

返回节点 $n,返回的节点与 $n 具有相同的父节点,并且通过 is Xpath 运算符等于它.

Returns the node $n, The node returned has the same parent as $n and is equal to it by the is Xpath operator.

在传统(XSLT 1 样式)模板使用中,差异几乎完全被掩盖了,因为您永远无法访问任一操作的结果,构造函数的结果被隐式复制到输出树中,因此xsl:sequence 不制作副本的事实被掩盖了.

The difference is almost entirely masked in traditional (XSLT 1 style) template usage as you never get access to the result of either operation the result of the constructor is implicitly copied to the output tree so the fact that xsl:sequence doesn't make a copy is masked.

<xsl:template match="a">
   <x>
   <xsl:sequence select="$n"/>
   </x>
</xsl:template>

<xsl:template match="a">
    <x>
    <xsl:copy-of select="$n"/>
    </x>
</xsl:template>

两者都创建一个新的元素节点,并将内容的结果复制作为新节点 x 的子节点.

Both make a new element node and copy the result of the content as children of the new node x.

但是,如果您使用函数,很快就会发现差异.

However the difference is quickly seen if you use functions.

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:f="data:,f">

    <xsl:variable name="s">
        <x>hello</x>
    </xsl:variable>

    <xsl:template name="main">
        ::
        :: <xsl:value-of select="$s/x is f:s($s/x)"/>
        :: <xsl:value-of select="$s/x is f:c($s/x)"/>
        ::
        :: <xsl:value-of select="count(f:s($s/x)/..)"/>
        :: <xsl:value-of select="count(f:c($s/x)/..)"/>
        ::
    </xsl:template>

    <xsl:function name="f:s">
        <xsl:param name="x"/>
        <xsl:sequence select="$x"/>
    </xsl:function>

    <xsl:function name="f:c">
        <xsl:param name="x"/>
        <xsl:copy-of select="$x"/>
    </xsl:function>

</xsl:stylesheet>

生产

$ saxon9 -it main seq.xsl
<?xml version="1.0" encoding="UTF-8"?>
::
:: true
:: false
::
:: 1
:: 0
::

这里 xsl:sequencexsl:copy-of 的结果完全不同.

Here the results of xsl:sequence and xsl:copy-of are radically different.

这篇关于XSLT xsl: 序列.到底有什么好处呢..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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