'xsl:copy-of'和'xsl:sequence'之间的区别是什么?
xsl:copy-of select="expression"
评估 expression
, 并创建表达式评估的序列的深度副本,而 xsl:sequence select="expression"
评估 expression
并返回该表达式评估出的序列。
根据指令使用的位置不同,例如使用 <xsl:sequence select="doc('file.xml')//foo"/>
的函数将返回文件中的 foo
元素序列,而在函数中使用 <xsl:copy-of select="doc('file.xml')//foo"/>
将返回文件中 foo
元素的副本。
只要您输出到结果树,通常使用其中一个并不重要,但如果您选择输入节点并想要保留它们,并且还不想浪费内存,那么通常使用 xsl:sequence
代替 xsl:copy-of
可能更有效。
以下是一个例子,让你更清楚地了解区别。
这是样式表:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="/">
<xsl:variable name="vCopy" as="document-node()">
<xsl:copy-of select="."/>
</xsl:variable>
<xsl:variable name="vSequence" as="document-node()">
<xsl:sequence select="."/>
</xsl:variable>
<xsl:sequence select="generate-id(.),
generate-id($vCopy),
generate-id($vSequence)"/>
</xsl:template>
</xsl:stylesheet>
d2047 d2048d2047 d2047
xsl:copy-of
会将所选项目作为深度复制进行复制。
xsl:sequence
用于构建节点和/或原子值的有序列表。
因此,使用 xsl:copy-of
来深度复制输入文档中存在的节点;使用 xsl:sequence
来从可能不同的节点和基本类型组装有序列表。
xsl:copy-of
和xsl:sequence
有什么共同点?”并不容易。请提出一个具体的问题会更容易解答。 - michael.hor257k