XSLT choose

XSLT <xsl:choose> Element

To express multiple conditional tests, we can use the <xsl:choose> element in conjunction with <xsl:when> and <xsl:otherwise>.

Syntax:


  
   ... Code ...
  
  
   ... Code ....
  

Where to use the Choose Condition:

In the XSL file, we can add the <xsl:choose>, <xsl:when>, and <xsl:otherwise> elements, to insert a multiple conditional test against the XML file.

Example:

Books.xml:




  
    ABC
    Author Name
    2020
    100.00
  

  
    XQuery Book
    Author 1
    Author 2
    2005
    300.00
  

  
    Sociology 1
    Author Name
    2010
    250.00
   

  
    Current Affairs
    Author Name
    2004
    500.00
  

  
    Science Book
    Author 1
    Author 2
    Author 3
    2011
    150.00
  


XSLT Code:






  

List of Books

Title Price

Output:

Explanation:

In the above example, we are adding a pink background-colour to the “Price” column if the year of the book has a value higher than 2005.

Example 2:

Books.xml:




  
    ABC
    Author Name
    2020
    100.00
  

  
    XQuery Book
    Author 1
    Author 2
    2005
    300.00
  

  
    Sociology 1
    Author Name
    2010
    250.00
  

  
    Current Affairs
    Author Name
    2004
    500.00
  

  
    Science Book
    Author 1
    Author 2
    Author 3
    2011
    150.00
  


XSLT Code:






  

List of Books

Title Artist

Output:

Explanation:

In the above example, we have placed two <xsl:when> elements. Here, we are adding a pink background-colour to the “Price” column if the year of the book has a value higher than 2005, and if the year of the book has a value higher than 2004 and lower or equal to 2005, then we are adding a grey background-colour.

Please follow and like us:
Content Protection by DMCA.com