Microsoft XML 2.5 SDK


 

context Method

[This is preliminary documentation and subject to change.]

Changes the context to the node from which this query started.

Syntax

context(index)

Parameters

index

Negative numbers indicate relative positions upward in the tree from the current context position. Natural numbers (zero and the positive integers) represent absolute positions working down from the root of the data tree. For example, context(0) is the root of the subtree upon which transformNode was called. context(1) is the context of the first context-switching XSL element in the style sheet (specified with the select attribute).

Remarks

In XSL, previous queries initiated by <xsl:for-each> can be obtained as well by using a numerical parameter.

The context method offers a way to create the most common type of joins. In particular, it provides a way to retrieve the context from which a query started. Thus, one can easily select nodes down a chain (or through links) where a property matches an attribute or other feature of the context from which the query started.

You can obtain elements returned by a previous query. You can also obtain indexes to nodes that might not otherwise be available.

The context method acts as a proxy for the context node. For example, the following will return the "a" attribute of the context node for the query:

context()/@a

The following will return all author nodes where the name is the same as the search attribute of the context:

//author[. = context()/@search]

The context method takes an optional integer argument. Negative numbers specify the number of context levels to walk up in the set of containing expressions relative to the current context: context(-1) represents the current context, context(-2) represents the containing context, and so on. The most commonly used argument is -1, which is the default value. context() is equivalent to context(-1).

In the following query, the inner for loop selects "b" nodes where the 'x' attribute is equal to the 'x' attribute of the context for the outer for loop. Given the sample data shown below, it returns the node x="1" corresponding to a="1" and the node x="2" when a="2". context(-2) specifies the containing context, here the selected "a" nodes.

<a x="1">
 <b>
  <c x="1"/>
  <c x="2"/>
 </b>
</a>
<a x="2">
 <b>
  <c x="1"/>
  <c x="2"/>
 </b>
</a>
<xsl:for-each select="a">
  <xsl:for-each select="b">
    <xsl:for-each select="c[@x = context(-2)/@x]">
    ...
    </xsl:for-each>
  </xsl:for-each>
</xsl:for-each>

The following query will select all "a" elements from the root of the subtree the style sheet is transforming:

<xsl:for-each select="context(0)/a">
  ...
</xsl:for-each>

Contexts cannot be used within a union. For more information, see the reference documentation for the | operator in Set Operations.

Note the following limitations on the context method:

context can appear only in the leftmost part of a query.

Only context() and context(-1) can be used in conjunction with the | operator. All other context values are prohibited.

Example

The following example applies the XSL style sheet "style" to the XML data, "data". The style sheet includes the following fragment:

<xsl:for-each select="data/classes">
  <xsl:for-each select="class">
    <xsl:for-each select="teacher">
      <xsl:value-of
        select="context(0)/data/teachers/teacher[@tid = context(-1)/@tid]"/>
    </xsl:for-each>
  </xsl:for-each>
</xsl:for-each>

context indicates the root of the data tree, so that query is fairly straightforward, walking down from the root to the teacher elements. context(-1) indicates the current context, so the subquery [@tid = context(-1)/@tid] selects only those teacher elements that match the teacher id (tid) values specified in the current context (as can be seen in the nested <xsl:for-each> elements) data/classes/class/teacher.

<HTML>
<HEAD>
<STYLE>
.  {font-size:20pt}
</STYLE>
<SCRIPT>
function init() {
  results.innerHTML = "";
  res = data.transformNode(style.selectSingleNode("*"));
  results.innerHTML = res;
 
}
</SCRIPT> 
</HEAD>
<BODY onload="init()">
<XML id="data">
  <data>
    <classes mingrade="B">
      <class nid="1">
        <teacher tid="1"/>
        <student sid="1"/>
        <student sid="2"/>
        <student sid="3"/>
      </class>
      <class nid="2">
        <teacher tid="1"/>
        <teacher tid="2"/>
        <student sid="4"/>
        <student sid="2"/>
        <student sid="3"/>
      </class>
    </classes>
       
    <teachers>
      <teacher tid="1">Teresa Atkinson</teacher>
      <teacher tid="2">Steve DeBroux</teacher>
    </teachers>
   
    <students>
      <student sid="1"><name>Kathie Flood</name><grade>A</grade></student>
      <student sid="2"><name>William Herlan</name><grade>B</grade></student>
      <student sid="3"><name>Paul West</name><grade>D</grade></student>
      <student sid="4"><name>Wendy Wheeler</name><grade>C</grade></student>
    </students>
  </data>
</XML>
<!--The XSL style sheet-->
<XML id="style">
  <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
    <xsl:template match="/">
      <xsl:for-each select="data/classes">
        <xsl:for-each select="class">
          <DIV>Class <xsl:value-of select="@nid"/></DIV>
          <HR/>
          <P>Teacher(s)
            <xsl:for-each select="teacher">
              <DIV>
                <xsl:value-of select="context(0)/data/teachers/teacher[(@tid = context(-1)/@tid)]"/>
              </DIV>
            </xsl:for-each>
          </P>
          <BR/>
          <P>Student(s)
            <xsl:for-each select="student">
              <DIV>
                <xsl:value-of select="context(0)/data/students/student[(@sid =
                  context(-1)/@sid) and (grade $le$ context(-3)/@mingrade)]/name"/>
              </DIV>
            </xsl:for-each>
          </P>
          <HR noshade="true" color="red"/>
        </xsl:for-each>
      </xsl:for-each>
    </xsl:template>
  </xsl:stylesheet>
</XML>
<DIV id="results"></DIV>
</BODY>
</HTML>

See Also

Set Operations