Understanding How the Context Node Changes

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XPath Developer's Guide

Understanding How the Context Node Changes

The context node implicitly changes as the XPath-aware processor works its way through the location steps in a non-important location path.

There is no way to demonstrate this built-in changing of the context node by the processor, because the result of the XPath expression is returned to you as a whole, not as a series of steps. You can simulate the process, by decomposing a complete location path into its parts, and displaying where you are at each point.

Part of the data from the sample file, weather.xml, is shown here:

<weather>
    <today>
        <temperature scale="F">76</temperature>
        <humidity>67</humidity>
        <wind unit="mph">5</wind>
        <!-- No precipitation today -->
        <precip />
    </today>
    <forecast day="+1">
        <temperature scale="F">81</temperature>
        <humidity>30</humidity>
        <wind unit="mph">10</wind>
        <precip>Rain</precip>
    </forecast>
    <forecast day="+2">
        <temperature scale="F">72</temperature>
        <humidity>60</humidity>
        <wind unit="mph">2</wind>
        <precip>Fog</precip>
    </forecast>
</weather>

You can trace the steps through the following location path, using the style sheet weather.xsl:

/weather/forecast/wind/@unit

Steps in a location path are delimited by the slash character, /.

/
/weather
/weather/forecast
/weather/forecast/wind
/weather/forecast/wind/@unit

The only things you want to see are the results of evaluating each specific one of those steps, one at a time. Emphasizing tight XSLT code is not in this example. Here are the templates from the style sheet:

Example

The five template rules in this style sheet follow the same general method, aside from formatting the output for readability:

  • Match on the portion of the location path of interest.
  • Display the path itself, the name of the context node, and the value of the context node.
  • Process the template rule specifically established for the next portion of the location path.
Note   Using a simple <xsl:apply-templates />, without the select attribute, sometimes causes extraneous text to be displayed in the output. For example, omitting a select attribute in the template rule for the <forecast> element's <xsl:apply-templates/> would also cause the <temperature> and <precipitation> elements to be processed by the default template rules.

XML File (weather.xml)

Use the Sample XML Data File for XPath Context and Navigation and change the href attribute to reference weather.xsl.

XSLT File (weather.xsl)

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"    
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.w3.org/TR/REC-html40" >

<xsl:template match="/">
    <p>
        <b>Path: /</b><br />
        Name of context node: <b><xsl:value-of select="name()"/></b>
        Value: <b><xsl:value-of select="."/></b>
    </p>
    <xsl:apply-templates select="weather"/>
</xsl:template>
<xsl:template match="/weather">
    <p>
        <b>Path: /weather</b><br />
        Name of context node: <b><xsl:value-of select="name()"/></b>
        Value: <b><xsl:value-of select="."/></b>
    <xsl:apply-templates select="forecast"/>
    </p>
</xsl:template>
<xsl:template match="/weather/forecast">
    <p>
        <hr width="400" size="1" align="left" />
        <b>Path: /weather/forecast</b><br />
        Name of context node: <b><xsl:value-of select="name()"/></b>
        Value: <b><xsl:value-of select="."/></b>
    </p>
    <xsl:apply-templates select="wind"/>
</xsl:template>
<xsl:template match="/weather/forecast/wind">
    <p>
        <b>Path: /weather/forecast/wind</b><br />
        Name of context node: <b><xsl:value-of select="name()"/></b>
        Value: <b><xsl:value-of select="."/></b>
    </p>
    <xsl:apply-templates select="@unit"/>
</xsl:template>
<xsl:template match="/weather/forecast/wind/@unit">
    <p>
        <b>Path: /weather/forecast/wind/@unit</b><br />
        Name of context node: <b><xsl:value-of select="name()"/></b>
        Value: <b><xsl:value-of select="."/></b>
    </p>
</xsl:template>

</xsl:stylesheet>

Formatted Output

Processor Output

<?xml version="1.0" encoding="UTF-16"?><p xmlns="http://www.w3.org/TR/REC-html40"><b>Path: /</b><br />
        Name of context node: <b></b>
        Value: <b>  76 67 5     81 30 10 Rain   72 60 2 Fog  </b></p><p xmlns="http://www.w3.org/TR/REC-html40"><b>Path: /weather</b><br />
        Name of context node: <b>weather</b>
        Value: <b>  76 67 5     81 30 10 Rain   72 60 2 Fog  </b><p><hr width="400" size="1" align="left" /><b>Path: /weather/forecast</b><br />
        Name of context node: <b>forecast</b>
...</p></p>