MCS-043 IGNOU STUDY MATERIAL

Page 201

Enhanced Database Models

For example, perhaps your XML source document has the following data: <OrderNo> <ProductNo> 100 </ProductNo> <ProductNo> 200 </ProductNo> <ProductNo> 300 </ProductNo> </OrderNo> However, you want to display only the ProductNo data and do so as an HTML list: Products in this order: <UL> <LI> 100 </LI> <LI> 200 </LI> <LI> 300 </LI> </UL> The template will need to contain the <UL> and <LI> HTML codes, but what is needed is the means to select the nodes value you desire to insert between each <LI> and </LI>. The Elements - Templates The most basic tools at your disposal in XSLT are the <xsl:template> and <xsl:applytemplates/>. The former is used to define a rule and the later to apply it. <xsl:template/> The <template> tag is used to select a node or node sub-trees. By using the tag’s match attribute you can set a pattern to identify the node upon which you wish to apply a rule – or transformation. The pattern value can be a node path, such as Customer/Order/OrderNo, or it can be a predefined pattern value, such as * to select all nodes. In the previous example, you wanted to select all ProductNo values from a particular Customer node. For the sake of simplicity, we will omit how to iterate over all Customers or Orders for a particular Customer. Let us assume, you have managed to select a particular Customer/Orders/OrderNo and now you wish to display the ProductNo values in that OrderNo. <template match = “Customer/Orders/OrderNo” > <! -- apply some rules --> </template> But this only shows how to select the node or node set on which you wish to work. Now you need to apply some rules – or changes. <xsl:apply-templates /> To apply rules you use the <apply-templates> rule. You can apply a template rule to all child nodes of the currently selected node (selected using <template match = “…” />) by using <apply-template /> without a select attribute value pair. Alternatively, you can apply the rule to all child nodes in the set that meet a specific selection criteria by using <apply-template select = “…” /> where the ellipses are some pattern such as ProductNo. The following fragment should help things come together: <xsl:stylesheet ...> <html> <body> 40


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.