The <xsl:choose>, <xsl:when>, and <xsl:otherwise> elements of XSLT process elements in a document depending on the success or failure of a condition. This method of processing documents is known as conditional processing.
The <xsl:choose> Element
The syntax for the <xsl:choose> element is:
<xsl:choose>
<xsl:when>+
<xsl:otherwise> ?
</xsl:choose>
Note
Multiple <xsl:when> conditions can be used to process conditions in a similar manner as a switch-case statement.
<xsl:when> Element
The <xsl:when> element is a conditional testing element that enables you to test conditions. This element always appears within the <xsl:choose> element.
All <xsl:choose> elements contain at least one <xsl:when> element.
The syntax for the <xsl:when> element is:
<xsl:when test = Expression>
template body
</xsl:when>
the test attribute within the <xsl:when> element tests the condition specified through the expression.
This attribute returns a Boolean value depending on the specified expression.
If the condition returns true, the <xsl:when> element processes all instructions within the template body. The <xsl:otherwise> element specifies the elements to be processed if the <xsl:when> element returns false.
Ex: XML document called TVProgram that stores information about television programs:
TVProgram.xml File
<?xml version="1.0"?>
<Television>
<TVName>Sports TV</TVName>
<Channel ch="3">
<Progname>Football</Progname>
<Time>8.30 AM</Time>
<Description>Argentina Vs France</Description>
</Channel>
<Channel ch="4">
<Progname>Basketball</Progname>
<Time>22.30 PM</Time>
<Description>India Vs Australia</Description>
</Channel>
</Television>
the TVProgram.xml file stores information about Channel 3 and Channel 4 programs identified by the ch attribute of the <channel> element.
how the <xsl:choose> and <xsl:when > elements extract Channel 3 information from the TVProgram.xml file:
Channel.xsl Stylesheet
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<TV>
<xsl:apply-templates/>
</TV>
</xsl:template>
<xsl:template match="Channel">
<xsl:choose>
<xsl:when test="@ch=1">
<xsl:copy-of select="."/>
</xsl:when>
<xsl:when test="@ch=3">
<xsl:copy-of select="."/>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
It uses two <xsl:when> elements to check the values for the ch attribute. In the first <xsl:when> element, the condition is specified as ch =1. In the source TVProgram.xml file, there are no values with a ch attribute 1. As a result, the first <xsl:when> element returns false. In the second <xsl:when> element, the condition is specified as ch=3. When this condition is satisfied, all corresponding nodes are copied to the resultant document.The <xsl:otherwise> Element
The <xsl:otherwise> element always appears within the <xsl:when> element.If the conditions specified in the <xsl:when> element return false, the elements within the <xsl:otherwise> element are processed.
syntax for the <xsl:otherwise> element is:
<xsl:otherwise>
template body
</xsl:otherwise>
Note
The <xsl:otherwise> element does not contain any attributes
Ex: Product.xml File
<?xml version="1.0"?>
<SYSTEM>
<PRODUCT type="PROCESSOR">
<NAME>ABC Processor</NAME>
<SPEED>500 Mhz</SPEED>
<PRICE>14250</PRICE>
</PRODUCT>
<PRODUCT type="PROCESSOR">
<NAME>XYZ Processor</NAME>
<SPEED>450 Mhz</SPEED>
<PRICE>11500</PRICE>
</PRODUCT>
<PRODUCT type="PROCESSOR">
<NAME>PQR Processor</NAME>
<SPEED>330 Mhz</SPEED>
<PRICE>9850</PRICE>
</PRODUCT>
<PRODUCT type="HARD-DISK">
<SIZE>80 GB</SIZE>
<PRICE>6000</PRICE>
</PRODUCT>
<PRODUCT type="HARD-DISK">
<SIZE>40 GB</SIZE>
<PRICE>4800</PRICE>
</PRODUCT>
<PRODUCT type="HARD-DISK">
<SIZE>20 GB</SIZE>
<PRICE>3500</PRICE>
</PRODUCT>
</SYSTEM>
the Product.xml file stores information about various products, such as processors and hard disks.
The stylesheet that compares the value passed to the prodtype parameter and the value of the type attribute of the <Product> element in the Product.xml file:
PassValue.xsl Stylesheet
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:param name="prodtype"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:choose>
<xsl:when test="$prodtype='PROCESSOR'">
<PRODUCT-LIST>
<xsl:for-each select="//PRODUCT">
<xsl:if test="@type=$prodtype">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:for-each>
</PRODUCT-LIST>
</xsl:when>
<xsl:when test="$prodtype='HARD-DISK'">
<PRODUCT-LIST>
<xsl:for-each select="//PRODUCT">
<xsl:if test="@type=$prodtype">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:for-each>
</PRODUCT-LIST>
</xsl:when>
<xsl:otherwise>
<html>
<body>
<center><table border="1">
<tr><Td><xsl:value-of select="$prodtype"/> does not exist in the Source Document</Td></tr>
</table></center>
</body>
</html>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
he PassValue.xsl stylesheet displays the information about the products in the resultant document.
When you specify a value that does not exist in the XML document, the stylesheet executes the statements within the <xsl:otherwise> element, and displays the message stating that the product is not available.
No comments:
Post a Comment