9.3. The <xsl:for-each> Instruction Element

The <xsl:for-each> element provides the ability to recursively process a list of nodes. It is an instruction element that can contain zero or more <xsl:sort> elements (see Section 9.4), followed by a template, as shown in the following element model definition.

<!-- Category: instruction -->

<xsl:for-each

  select = node-set-expression>

  <!-- Content: (xsl:sort*, template) -->

</xsl:for-each>

The value for the required select attribute is a special kind of expression, a node-set-expression, which must always result in a node-set. It is an error to use an expression that returns any other kind of object. The instructions contained in the template will be applied to each node matched by the select attribute. The matched nodes are processed in document order, unless a revised order has been selected using <xsl:sort>.

Using Markup City, the stylesheet shown in Example 9-7 allows us to use <xsl:for-each> to process each <block> and use the name of its parent as an attribute named branch on each new <block> element in the output result tree.

Notice that the instruction elements in <xsl:for-each> apply to the current node as selected by the <xsl:for-each> element's select attribute, not the current node matched by the <xsl:template> element. The <xsl:for-each> element is one of the few instruction elements that can change the context of the current node list. It effectively suspends the processing of the node-set matched with <xsl:template>'s match attribute while it processes the new node-set.

If any <xsl:sort> elements are used in an <xsl:for-each>, they must come before any other elements or LREs in the template. The <xsl:sort> element sorts the nodes matched by the select attribute on <xsl:for-each> prior to instantiating the template for each node. This means that the nodes in the node-set are processed in the new sorted order, which may affect the output.

Example 9-7. Using <xsl:for-each> to add attributes.
					STYLESHEET:
<xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      version="1.0">

<xsl:template match="text()"/>
<xsl:output indent="yes"/>

<xsl:template match="boulevard">
<xsl:copy>
      <xsl:for-each select="//block">
            <xsl:copy>
            <xsl:attribute name="branch">
                  <xsl:value-of select="name(..)" />
            </xsl:attribute>
            <xsl:value-of select="text()"/>
            </xsl:copy>
      </xsl:for-each>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

RESULT:

<boulevard>
<block branch="thoroughfare">1st Street</block>
<block branch="thoroughfare">2nd Street</block>
<block branch="thoroughfare">3rd Street</block>
<block branch="thoroughfare">First Street</block>
<block branch="thoroughfare">Second Street</block>
<block branch="thoroughfare">Third Street</block>
<block branch="boulevard">Panorama Street</block>
<block branch="boulevard">Highland Plaza</block>
<block branch="boulevard">Hutchens Avenue</block>
<block branch="boulevard">Wildwood Drive</block>
<block branch="boulevard">Old Chimney Road</block>
<block branch="boulevard">Carrol Circle</block>
</boulevard>

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

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