转换中的节点集Node Sets in Transformations

节点集是从 XML 路径语言 (XPath) 表达式返回的四种基本数据类型之一。Node sets are one of four basic data types that are returned from XML Path Language (XPath) expressions. 节点集是按文档顺序创建的无重复节点的无序集合,可将其分配给样式表中的某个变量。A node set, which is an unordered collection of nodes without duplicates, created in document order, can be assigned to a variable in a style sheet.

备注

XslTransform 类在 .NET Framework 2.0 中已过时。The XslTransform class is obsolete in the .NET Framework 2.0. 可以使用 XslCompiledTransform 类执行可扩展样式表语言转换 (XSLT) 转换。You can perform Extensible Stylesheet Language for Transformations (XSLT) transformations using the XslCompiledTransform class. 请参阅使用 XslCompiledTransform 类从 XslTransform 类迁移,以获取详细信息。See Using the XslCompiledTransform Class and Migrating From the XslTransform Class for more information.

节点集是从 XPath 表达式返回的四种基本数据类型之一。Node sets are one of four basic data types that are returned from XPath expressions. 节点集是按文档顺序创建的无重复节点的无序集合,可将其分配给样式表中的某个变量。A node set, which is an unordered collection of nodes without duplicates, created in document order, can be assigned to a variable in a style sheet. 此节点集是在转换中的 select 属性中使用的 XPath 表达式的结果,它与 XML 文档对象模型 (DOM) 中的节点集有相同的行为。This node set, which is a result of an XPath expression used in a select attribute in a transformation, has the same behavior as a node set from the XML Document Object Model (DOM). 可以使用使用 XPathNavigator 的节点集定位中介绍的一组方法浏览节点集,与结果树片断不同,后者使用 XPathNodeIterator 进行浏览。You can navigate a node set using a set of methods shown in Node Set Navigation Using XPathNavigator, unlike a result tree fragment or result tree fragment, which uses the XPathNodeIterator for navigation.

下面的代码示例显示了当样式表中的某个 variableparameter 元素计算结果为节点集时如何循环访问节点集。The following code sample shows how to iterate over a node set when a variable or parameter element in a style sheet evaluates to a node set.

样式表Style Sheet

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">  
  
    <xsl:variable name="x" select="bookstore/book/title"></xsl:variable>  
  
    <xsl:template match="/">  
        <xsl:for-each select="$x">  
            ******  
            <xsl:value-of select="."></xsl:value-of>  
            ******  
        </xsl:for-each>  
    </xsl:template>  
  
</xsl:stylesheet>  

输入Input

<bookstore>  
   <book style="autobiography">  
      <title>Seven Years in Trenton</title>  
   </book>  
  
   <book style="autobiography">  
      <title>Seven Years in Trenton2</title>  
   </book>  
  
   <book style="textbook">  
      <title>History of Trenton Vol 3</title>  
   </book>  
</bookstore>  

OutputOutput

******  
Seven Years in Trenton  
******  
  
******  
Seven Years in Trenton2  
******  
  
******  
History of Trenton Vol 3  
******  

请参阅See also