Previous | Next | Indexes
Zvon > Tutorials > XSLT 2.0 Tutorial > Comments and processing instructions (6/9) >

Selecting comments with "node"

The select attribute of xsl:apply-templates in this example has value "node()". The "node()" is the most general node test, it matches a node of any kind. If xsl:apply-templates without select was used there would be no match as the default behavior of xsl:apply-templates does not apply instructions to comments and processing instructions.

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="/">
                  <xsl:apply-templates  select="node()"/>
            </xsl:template>

            <xsl:template  match="comment()">
                  <xxx>
                        <i>
                              <xsl:text>Node type: comment</xsl:text>
                        </i>
                        <i>
                              <xsl:text>Comment contents: </xsl:text>
                              <xsl:value-of  select="."/>
                        </i>
                  </xxx>
            </xsl:template>

            <xsl:template  match="*">
                  <xxx>
                        <i>
                              <xsl:text>Node type: element</xsl:text>
                        </i>
                        <i>
                              <xsl:text>Element name: </xsl:text>
                              <xsl:value-of  select="name()"/>
                        </i>
                  </xxx>
                  <xsl:apply-templates  select="node()"/>
            </xsl:template>

      </xsl:stylesheet>
XML
<!-- comment A -->
      <aaa> <!-- comment B --> </aaa>
Output

      <xxx>
            <i>Node type: comment</i>
            <i>Comment contents: comment A </i>
      </xxx>
      <xxx>
            <i>Node type: element</i>
            <i>Element name: aaa</i>
      </xxx>
      <xxx>
            <i>Node type: comment</i>
            <i>Comment contents: comment B </i>
      </xxx>


Previous chapter: Date and Time
Next chapter: Built in Types
Previous page: Matching comments in the source document
Next page: Processing instructions