Previous | Next | Indexes
Zvon > Tutorials > XSLT 2.0 Tutorial > Sequences (10/10) >

Instruction xsl:sequence

Behavior of the instruction xsl:sequence is very similar to xsl:copy-of. The difference, which is mostly hidden from the user is that xsl:sequence may return existing nodes and so makes transformation more straightforward and therefore efficient.
XSLT

      <xsl:stylesheet
                  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                  version="2.0">
            <xsl:output  method="xml"
                        indent="yes"
                        omit-xml-declaration="yes"/>

            <xsl:template  match="/aaa">
                  <aaa>
                        <v1>
                              <i>
                                    <xsl:sequence  select="bbb/@x"/>
                              </i>
                              <ii>
                                    <xsl:sequence  select="bbb"/>
                              </ii>
                              <iii>
                                    <xsl:sequence  select="1 to 4"/>
                              </iii>
                        </v1>
                        <v2>
                              <i>
                                    <xsl:copy-of  select="bbb/@x"/>
                              </i>
                              <ii>
                                    <xsl:copy-of  select="bbb"/>
                              </ii>
                              <iii>
                                    <xsl:copy-of  select="1 to 4"/>
                              </iii>
                        </v2>
                        <v3>
                              <i>
                                    <xsl:value-of  select="bbb/@x"/>
                              </i>
                              <ii>
                                    <xsl:value-of  select="bbb"/>
                              </ii>
                              <iii>
                                    <xsl:copy-of  select="1 to 4"/>
                              </iii>
                        </v3>
                  </aaa>
            </xsl:template>

      </xsl:stylesheet>
XML

      <aaa>
            <bbb  x="1">A</bbb>
            <bbb  x="2">B</bbb>
      </aaa>
Output

      <aaa>
            <v1>
                  <i  x="2"/>
                  <ii>
                        <bbb  x="1">A</bbb>
                        <bbb  x="2">B</bbb>
                  </ii>
                  <iii>1 2 3 4</iii>
            </v1>
            <v2>
                  <i  x="2"/>
                  <ii>
                        <bbb  x="1">A</bbb>
                        <bbb  x="2">B</bbb>
                  </ii>
                  <iii>1 2 3 4</iii>
            </v2>
            <v3>
                  <i>1 2</i>
                  <ii>A B</ii>
                  <iii>1 2 3 4</iii>
            </v3>
      </aaa>


Previous chapter: Templates - basic usage
Next chapter: Sequence Combinations
Previous page: Several variables can be used is for ... in ... return instruction
Next page: - - -