8.2. Result Tree Fragments

When a variable declaration element contains a template, the content of the template is instantiated and the resulting object is an RTF. This creates a new kind of object, different from the four standard object types (node-set, string, Boolean, and number). An RTF is a node-set that contains one node, whose children are the nodes that are produced by the instruction elements in the template.

Although an RTF is technically a node-set, there are restrictions on how this new type of node-set can be accessed or used. The way in which the variable is referenced in subsequent template rules determines how that RTF will be written to the actual output result tree. If the RTF is output using <xsl:copy-of>, the nodes in the RTF are sent to the output, including the tags and text content. Otherwise, the output will be the text content of the RTF, without any markup.

When a variable is bound to an RTF, a portion of the actual output result tree is being bound. Additional processing on RTFs resulting from variables is allowed, but not all functions are available. Only functions that can be applied to strings are allowed on RTFs. For example, using count() on an RTF will return an error. Operations that involve patterns and predicates are not allowed. Also, an RTF cannot be further processed using any /, //, or [] tokens in it—in other words, no absolute location path tokens, no descendant axis abbreviations, and no predicates. Otherwise, operations performed of the type permitted on strings, when performed on RTFs, behave just as they would on any other equivalent node-set.

Example 8-1 shows a variable that contains a template with three LREs, each containing an instruction element.

Example 8-1. Variable declaration with template content.
<xsl:variable name="numvar">
      <num><xsl:value-of select="1"/></num>
      <num><xsl:value-of select="2"/></num>
      <num><xsl:value-of select="3"/></num>
</xsl:variable>

The result of instantiating the variable is a node-set containing the three <num> nodes. Using <xsl:copy-of select="$numvar"> returns the nodes and their content:

<num>1</num>
<num>2</num>
<num>3</num>

The value of the node-set, when extracted using <xsl:value-of select="$numvar">, is the string value of a concatenation of its children; in this case, the string is "123." However, using a node-set function like <xsl:value-of select="count($numvar)"> is an error because node-set operations on RTFs are not allowed.

In an example using Markup City, it is possible to store nodes from the input in a variable as follows:

<xsl:variable name="blocks">
<xsl:copy-of select="//block"/>
</xsl:variable>

This example stores all the <block> elements and their text children in the variable as a single node-set. The value of the new node-set can then be extracted using <xsl:value-of select="$blocks">, which returns the concatenation of the text in each block.

Using <xsl:copy-of select="$blocks"> retrieves the entire set of <block> nodes, including their children text nodes, as shown in Example 8-2.

Example 8-2. Extracting a node-set in a variable using <xsl:copy-of>.
					INPUT:

<?xml version="1.0"?>
<main>
      <parkway>
            <thoroughfare>Governor Drive</thoroughfare>
            <thoroughfare name="Whitesburg Drive">
                  <sidestreet>Bob Wallace Avenue</sidestreet>
                  <block>1st Street</block>
                  <block>2nd Street</block>
<block>3rd Street</block>
                  <sidestreet>Woodridge Street</sidestreet>
            </thoroughfare>
            <thoroughfare name="Bankhead">
                  <sidestreet>Tollgate Road</sidestreet>
                  <block>First Street</block>
                  <block>Second Street</block>
                  <block>Third Street</block>
                  <sidestreet>Oak Drive</sidestreet>
            </thoroughfare>
      </parkway>
      <boulevard>
                  <block>Panorama Street</block>
                  <block>Highland Plaza</block>
      <block>Hutchens Avenue</block>
                  <block>Wildwood Drive</block>
                  <block>Old Chimney Road</block>
                  <block>Carrol Circle</block>
     </boulevard>
</main>

STYLESHEET:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
             version="1.0">
<xsl:variable name="blocks">
<xsl:copy-of select="//block"/>
</xsl:variable>
<xsl:template match="/">
<xsl:copy-of select="$blocks"/>
</xsl:template>
</xsl:stylesheet>
RESULT:

<?xml version="1.0" encoding="utf-8"?>
<block>1st Street</block>
<block>2nd Street</block>
<block>3rd Street</block>
<block>First Street</block>
<block>Second Street</block>
<block>Third Street</block>
<block>Panorama Street</block>
<block>Highland Plaza</block>
<block>Hutchens Avenue</block>
<block>Wildwood Drive</block>
<block>Old Chimney Road</block>
<block>Carrol Circle</block>

The template inside the <xsl:variable> element shown in this example is retrieved when the variable is referenced, or called, using the variable reference, $blocks.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.13.173