Repetition

Repetition is natural in the processing of XML documents. The nature of the tree involves stepping through the document node-by-node and applying the transformations laid out in the templates. There are many occasions when you will want to apply the same formatting to a number of the same type of elements. For example, if you had an XML document that described the employees in your company, and you were transforming it into an HTML document for the Web, you might want to create a table with each employee's name in it. For this operation, you would basically want to repeat the operation for the number of times an element occurred.

Fortunately, there is a mechanism built into XSLT, which makes this kind of repetitious processing very easy—the xsl:for-each element.

xsl:for-each

The xsl:for-each element can contain a template, or other structures, for creating elements, and has one attribute, select. The select attribute allows you to choose what condition the repetition will be based on.

For example, let's say that we wanted to create an element for our phone numbers in our address XML document. We could use the for-each structure to create a new element for each of the number children for each phone:

<xsl:template match="contact"> 
 <xsl:for-each select="phone/number">
 <xsl:element name="{@type}">
  <xsl:value-of select="."/>
 </xsl:element>
 </xsl:for-each>
</xsl:template>

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

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