The syntax for the <xsl:if> element is:
Testing Multiple Conditions
You can test multiple conditions using logical and
relational operators with the <xsl-if> element.
Ex: the XML file that stores information about the telephone usage of customers:
TeleInformation.xml File
?xml version="1.0"?>
<Bill-Information>
<Customer-Info>
<Tel-No>01-456-789-58621</Tel-No>
<CustName>Mr.Micheal</CustName>
<Address>457,Texas,USA.</Address>
<Chargable-Units>450</Chargable-Units>
</Customer-Info>
<Customer-Info>
<Tel-No>01-427-654-57945</Tel-No>
<CustName>Mr.Stewart</CustName>
<Address>789,California,USA</Address>
<Chargable-Units>800</Chargable-Units>
</Customer-Info>
</Bill-Information>
if the chargeable unit is less than 500, $2.50 is
charged for each unit. If the number of units is more than 500, $3.00 per unit
is charged. You can use an XSLT stylesheet to calculate the bill amount and
display the amount for each customer.
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:template name="cust-info">
<xsl:param name="slab1"/>
<xsl:param name="slab2"/>
<xsl:for-each
select="//Customer-Info">
<xsl:if test="Chargable-Units
<500" >
<xsl:variable name="unit"
select="Chargable-Units"/>
<tr><td>PhoneNo:
</td><td> <xsl:value-of
select="Tel-No"/></td></tr>
<tr><td>CustomerName:
</td><td> <xsl:value-of
select="CustName"/></td></tr>
<tr><td>Address:
</td><td><xsl:value-of
select="Address"/></td></tr>
<tr><td>No.of Units:
</td><td><xsl:value-of
select="$unit"/></td></tr>
<tr><td>Amount:
</td><td><xsl:value-of
select="$unit*$slab1"/></td></tr>
</xsl:if>
<xsl:if
test="Chargable-Units>=500">
<xsl:variable name="unit"
select="Chargable-Units"/>
<tr><td colspan="2"
height="20"></td></tr>
<tr><td>PhoneNo:
</td><td><xsl:value-of
select="Tel-No"/></td></tr>
<tr><td>CustomerName:
</td><td><xsl:value-of
select="CustName"/></td></tr>
<tr><td>Address:
</td><td><xsl:value-of
select="Address"/></td></tr>
<tr><td>No. of Units:
</td><td><xsl:value-of select="$unit"/></td></tr>
<tr><td>Amount:
</td><td><xsl:value-of
select="$unit*$slab2"/></td></tr>
</xsl:if>
</xsl:for-each>
</xsl:template>
<xsl:template match="/">
<html>
<body>
<center><table border="1" bgcolor="#ffeedd">
<th colspan="2" align="center">Telephone Bill
Information</th>
<xsl:call-template name="cust-info"> <xsl:with-param
name="slab1" select="2.50"/>
<xsl:with-param name="slab2" select="3.00"/>
</xsl:call-template>
</table></center>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Checking the existence of a node
CustAcc.xml File
CheckAcc.xsl Stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"version="1.0"> <xsl:output method="xml" indent="yes"/> <xsl:template match="/"> <xsl:if test="//Account"> <xsl:copy-of select="//Account"/> </xsl:if> <xsl:if test="not(//customer)"> <Customer><xsl:text>The customer node does not
Exist</xsl:text></Customer> </xsl:if> </xsl:template> </xsl:stylesheet>
No comments:
Post a Comment