Saturday, September 23, 2023

xsl-if Example scenario

 The syntax for the <xsl:if> element is:

<xsl:if test="condition"> <!-- Content to be processed if the condition is true --> </xsl:if>
test: This attribute specifies the condition to be evaluated. If the condition evaluates to true, the content inside the <xsl:if> element is processed. If it evaluates to false, the content is ignored.
Ex:<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">  <xsl:template match="person">    <xsl:if test="age &lt; 18">      <xsl:text>Child: </xsl:text>      <xsl:value-of select="name"/>    </xsl:if>  </xsl:template></xsl:stylesheet>
In this example, the XSLT stylesheet processes XML data containing <person> elements and checks if the <age> element within each <person> is less than 18. If the condition is true (i.e., the person is a child), it outputs "Child: " followed by the person's name.

If these fields are optional in the source and target, it is a good practice to insert an xsl:if statement around these mappings to test for the existence of the source node.

Yes, it is a good practice to use xsl:if statements or similar conditional constructs when dealing with optional fields in your source and target XML data during XSLT transformations.This helps ensure that your XSLT stylesheet gracefully handles cases where certain nodes may or may not be present in the source XML.
Here's a simplified example of how you might use xsl:if to handle optional fields in an XSLT transformation:


<xsl:template match="person">  <xsl:if test="address">    <!-- Process the address node -->    <address>      <xsl:value-of select="address"/>    </address>  </xsl:if></xsl:template>

In this example, the <address> element is processed only if the <address> node exists in the source XML. If it doesn't exist, the <address> element is not generated in the output.

Ex:Suppose you have the following source XML:
<person>  <name>John</name>  <!-- Address node is optional and not present --></person>
And you want to transform it into a target XML:
<target>  <person>    <name>John</name>    <!-- Target address node should be optional -->  </person></target>
In this case, if you don't use an xsl:if or similar conditional check, you might end up with undesired empty <address> nodes in the target document:
<target>  <person>    <name>John</name>    <address></address> <!-- Undesired empty node -->  </person></target>
To avoid this, you should use xsl:if to conditionally create the <address> node only when the source <address> node exists:

<xsl:template match="person">  <person>    <name><xsl:value-of select="name" /></name>    <xsl:if test="address">      <address><xsl:value-of select="address" /></address>    </xsl:if>  </person></xsl:template>
With this approach, the <address> node is only created in the target when the source <address> node is present, resulting in the desired output:
<target>  <person>    <name>John</name>  </person></target>

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.

BillAmount.xsl Stylesheet 

<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 &lt;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

The <xsl:if> element checks for the existence of a node in an XML file.

Ex If the customer node exists, you can display the corresponding node information.
If the customer node does not exist, you can display the message,

CustAcc.xsl stylesheet, which uses the <xsl:if> element to list the account node information and check the existence of the customer node in the CustAcc.xml file:

CustAcc.xml File


<?xml version="1.0"?> <Bank> <Account type="FD"> <AccountNo>FD-1254</AccountNo> <CustName>Mr.Stewart</CustName> <Minimum currency="US$">4500</Minimum> </Account> <Account type="CA"> <AccountNo>CA-1255</AccountNo> <CustName>Mr. John David</CustName> <Minimum currency="US$">8000</Minimum> </Account> <Account type="CA"> <AccountNo >CA-1256</AccountNo> <CustName>Mr. Tom Wilkins</CustName> <Minimum currency="US$">6000</Minimum> </Account> </Bank>
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

SOA Overview Part-1

  Middleware It provides a mechanism for the process to interact with other processes running on multiple network machines. Advantages...