Previous | Next | Indexes
Zvon > Tutorials > XSLT 2.0 Tutorial > Axes (7/8) >

Self axis

The "self::" axis of an node always contains only one item, the context node itself. Nevertheless, this axis may be very useful when namespaces are involved. Study carefully the example. If the comparison was based on name() function then the processing would be difficult as namespace prefixes would cause severe problems.

XSLT

      <xsl:stylesheet
                  xmlns:c="111"
                  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="/">
                  <xxx>
                        <xsl:apply-templates  select="/*"/>
                  </xxx>
                  <yyy>
                        <xsl:apply-templates  select="/*/*"/>
                  </yyy>
            </xsl:template>

            <xsl:template  match="*">
                  <zzz  name="{name()}"
                              position="{position()}">
                        <xsl:choose>
                              <xsl:when  test="self::c:aaa">
                                    <MATCH/>
                              </xsl:when>
                              <xsl:otherwise>
                                    <NOTHING/>
                              </xsl:otherwise>
                        </xsl:choose>
                  </zzz>
            </xsl:template>

      </xsl:stylesheet>
XML

      <aaa
                  xmlns:a="111"
                  xmlns:b="111">
            <a:aaa/>
            <b:aaa/>
            <a:aaa
                        xmlns:a="222"/>
      </aaa>
Output

      <xxx
                  xmlns:c="111">
            <zzz  name="aaa"
                        position="1">
                  <NOTHING/>
            </zzz>
      </xxx>
      <yyy
                  xmlns:c="111">
            <zzz  name="a:aaa"
                        position="1">
                  <MATCH/>
            </zzz>
            <zzz  name="b:aaa"
                        position="2">
                  <MATCH/>
            </zzz>
            <zzz  name="a:aaa"
                        position="3">
                  <NOTHING/>
            </zzz>
      </yyy>


Previous chapter: Sorting
Next chapter: Named-Templates
Previous page: Attribute axis
Next page: Descendant-or-self and ancestor-or-self axes