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

Matching processing instructions in source document

Processing instructions nodes are matched with node test processing-instruction().

XML parsers are required to pass processing instructions to applications and so your XSLT processor is guaranteed to receive them from any parser.

Please note, that the select attribute of xsl:apply-templates has value node(). 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="processing-instruction()">
                  <xxx>
                        <i>
                              <xsl:text>Node type: processing instruction</xsl:text>
                        </i>
                        <i>
                              <xsl:text>Name of processing instruction: </xsl:text>
                              <xsl:value-of  select="name()"/>
                        </i>
                        <i>
                              <xsl:text>Values of processing instruction: </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
<?procAAA a=3?>
      <aaa> <?procBBB a=66; b=12 ?> </aaa>
Output

      <xxx>
            <i>Node type: processing instruction</i>
            <i>Name of processing instruction: procAAA</i>
            <i>Values of processing instruction: a=3</i>
      </xxx>
      <xxx>
            <i>Node type: element</i>
            <i>Element name: aaa</i>
      </xxx>
      <xxx>
            <i>Node type: processing instruction</i>
            <i>Name of processing instruction: procBBB</i>
            <i>Values of processing instruction: a=66; b=12 </i>
      </xxx>


Previous chapter: Date and Time
Next chapter: Built in Types
Previous page: Name attribute is an attribute value template
Next page: - - -