Using the self Axis

Using the self Axis

The self axis contains just the context node. In fact, one of the XPath abbreviations that youll see later is that you can abbreviate self::node() as ..

This is a useful axis to know about, because as you recall from Chapter 4, if you omit the axis, the default is child:: , and sometimes you want to refer to the current node instead. For example, [self::PLANET] is true only if the context node is a <PLANET> element.

In the following example, I combine the templates for < NAME > and <MASS> elements into one template. Because those elements are formatted differently, however, I need to tell them apart inside the same template, and I can do that by checking self::NAME , which returns a non-empty node set if the context node is a <NAME> element, and self::MASS , which returns a non-empty node set if the context node is a <MASS> element:

 <xsl:template match="PLANET">         <TR>            <TD><xsl:apply-templates select="NAME"/></TD>            <TD><xsl:apply-templates select="MASS"/></TD>            <TD><xsl:apply-templates select="RADIUS"/></TD>            <TD><xsl:apply-templates select="DAY"/></TD>         </TR>     </xsl:template>      <xsl:template match="NAME  MASS">          <xsl:if test="self::NAME">              <xsl:value-of select="."/>          </xsl:if>          <xsl:if test="self::MASS">              <xsl:value-of select="."/>              <xsl:text> </xsl:text>              <xsl:value-of select="@UNITS"/>          </xsl:if>      </xsl:template>          .          .          . 

That completes our look at the new XPath axes. Its time to get to some examples.

Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
Year: 2005
Pages: 196

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: