Home > Articles > Web Services > XML

📄 Contents

  1. The XSL Elements
  2. The xsl:if Element
  3. The xsl:choose, xsl:when, and xsl:otherwise Elements
  4. The xsl:eval Element
  5. Summary
  6. Q&A
  • Print
  • + Share This

The xsl:choose, xsl:when, and xsl:otherwise Elements

The previous example showed you how to use multiple <xsl:if> elements in situations where there are alternatives. Although this works, it's ungainly when there are several possible values. In that case, it's better to use a combination of the <xsl:choose>, <xsl:when>, and <xsl:otherwise> elements. The following two sections illustrate how these elements are used.

An xsl:choose and xsl:when Example

Reopen listing14-4.xml and change these lines:

<Book>
<Title>Teach Yourself Uses of Silicon in 24 Hours</Title>
<Author>
<Name>Missy Fixit</Name>

to these lines:

<Book BestSeller="impossible">
<Title>Teach Yourself Uses of Silicon in 24 Hours</Title>
<Author>
<Name>Missy Fixit</Name>

Add the following two additional records that appear in Listing 15.3 to the end of the file. Save the updated file as listing15-3.xml.

Listing 15.3  Two Records with Additional Values for the BestSeller Attribute

 1: <Book BestSeller="guaranteed">
 2: <Title>101 Unusual Uses for Hairspray</Title>
 3: <Author>
 4: <Name>Harpo Hair</Name>
 5: <Address>101 Furrball</Address>
 6: <City>Clipper City</City>
 7: <State>California</State>
 8: <PostalCode>10102</PostalCode>
 9: <Country>United States</Country>
10: <Phone>(586) 212-3498</Phone>
11: <Email>HairX@HairClip.com</Email>
12: <URL>http://www.HairClip.com</URL>
13: </Author>
14: <Publisher>
15: <Name>Sams Publishing</Name>
16: <Address>201 West 103rd Street</Address>
17: <City>Indianapolis</City>
18: <State>Indiana</State>
19: <PostalCode>46290</PostalCode>
20: <Country>United States</Country>
21: <Phone>(111) 123-4567</Phone>
22: <Editor>Dummy Name</Editor>
23: <EmailContact>Dummy.Name@samspublishing.com</EmailContact>
24: <URL>http://www.samspublishing.com</URL>
25: </Publisher>
26: <ISBN>0-578-3411-0</ISBN>
27: <PublisherBookID>1234576</PublisherBookID>
28: <PublicationDate>2000-03-01</PublicationDate>
29: <Price>36.95</Price>
30: </Book>
31: <Book BestSeller="dud">
32: <Title>Celebrity Toenail Clippings</Title>
33: <Author>
34: <Name>Foot Lover</Name>
35: <Address>101 Podiatrist Center</Address>
36: <City>Clipper City</City>
37: <State>California</State>
38: <PostalCode>10102</PostalCode>
39: <Country>United States</Country>
40: <Phone>(586) 212-2112</Phone>
41: <Email>Exotic@CToesClip.com</Email>
42: <URL>http://www.CToesClip.com</URL>
43: </Author>
44: <Publisher>
45: <Name>Sams Publishing</Name>
46: <Address>201 West 103rd Street</Address>
47: <City>Indianapolis</City>
48: <State>Indiana</State>
49: <PostalCode>46290</PostalCode>
50: <Country>United States</Country>
51: <Phone>(111) 123-4567</Phone>
52: <Editor>Dummy Name</Editor>
53: <EmailContact>Dummy.Name@samspublishing.com</EmailContact>
54: <URL>http://www.samspublishing.com</URL>
55: </Publisher>
56: <ISBN>0-578-3001-0</ISBN>
57: <PublisherBookID>1234476</PublisherBookID>
58: <PublicationDate>2000-06-01</PublicationDate>
59: <Price>46. 95</Price>
60: </Book>

NOTE

Before you continue, understand that there are now five different values assigned to the BestSeller option: yes, no, impossible, guaranteed, and dud.

Reopen listing15-2.xsl and modify the <xsl:for-each> loop to match Listing 15.4. Save the updated file as listing15-4.xsl.

Listing 15.4  Using the XSL Elements to Create a Multiple Alternative Construct

 1: <xsl:for-each select="Catalog/Book"
    order-by="+Author/City">
 2:    <xsl:choose>
 3:     <xsl:when match=".[@BestSeller='yes']">
 4:     <tr>
 5:      <xsl:apply-templates/>
 6:     </tr>
 7:     </xsl:when>
 8:     <xsl:when match=".[@BestSeller='no']">
 9:     <tr style="font-weight:bold;color:red">
10:     <td>Not a bestseller</td>
11:     </tr>
12:    </xsl:when>
13:     <xsl:when match=".[@BestSeller='impossible']">
14:     <tr style="font-weight:bold;color:red">
15:     <td>It is impossible for this to be a
        bestseller</td>
16:     </tr>
17:    </xsl:when>
18:     <xsl:when match=".[@BestSeller='guaranteed']">
19:     <tr style="font-weight:bold;color:red">
20:     <td>Guaranteed to be a bestseller</td>
21:     </tr>
22:    </xsl:when>
23:    <xsl:when match=".[@BestSeller='dud']">
24:     <tr style="font-weight:bold;color:red">
25:     <td>Definitely a dud</td>
26:     </tr>
27:    </xsl:when>
28:     </xsl:choose>
29:    </xsl: for-each>

Note that there's an xsl:when element for each of the possible values that the BestSeller attribute can have.

Reopen listing14-2.html and modify the lines that load the files so that listing15-3.xml is the XML file and listing15-4.xsl is the XSL file. The output when it's viewed in IE is shown in Figure 15.4.

Figure 15.4 A different output for each possible value of the BestSeller attribute.

The following XSL element pair delimits a set of multiple alternatives written using the <xsl:when> and <xsl:otherwise> elements:

<xsl:choose>
</xsl:choose>

There are five <xsl:when> elements in the construct, one for each of the possible values of the attribute:

<xsl:when match=".[@BestSeller='yes']">
    <tr>
     <xsl:apply-templates/>
    </tr>
    </xsl:when>
    <xsl:when match=".[@BestSeller='no']">
    <tr style="font-weight:bold;color:red">
    <td>Not a bestseller</td>
    </tr>
   </xsl:when>
    <xsl:when match=".[@BestSeller='impossible']">
    <tr style="font-weight:bold;color:red">
    <td>It is impossible for this to be a
    bestseller</td>
    </tr>
   </xsl:when>
    <xsl:when match=".[@BestSeller='guaranteed']">
    <tr style="font-weight:bold;color:red">
    <td>Guaranteed to be a bestseller</td>
    </tr>
   </xsl:when>
   <xsl:when match=".[@BestSeller='dud']">
    <tr style="font-weight:bold;color:red">
    <td>Definitely a dud</td>
    </tr>
   </xsl: when>

In this case, the data is extracted only when the value of BestSeller is yes. For all other cases, a simple message is written to that row of the table.

An xsl:otherwise Example

As has already been mentioned, the other element used in association with <xsl:choose> is the <xsl:otherwise> element. It can be considered as the "none of the above" option.

Reopen listing15-4.xsl and modify it to match Listing 15.5. Save it as listing15-5.xsl.

Listing 15.5  Using the xsl:otherwise Element to Catch All Remaining Options

 1: <xsl:for-each select="Catalog/Book"
    order-by="+Author/City">
 2:    <xsl:choose>
 3:     <xsl:when match=".[@BestSeller='yes']">
 4:     <tr>
 5:      <xsl:apply-templates/>
 6:     </tr>
 7:     </xsl:when>
 8:     <xsl:when match=".[@BestSeller='no']">
 9:     <tr style="font-weight:bold;color:red">
10:     <td>Not a bestseller</td>
11:     </tr>
12:    </xsl:when>
13:    <xsl:when match=".[@BestSeller='impossible']">
14:     <tr style="font-weight:bold;color:red">
15:     <td>It is impossible for this to be a bestseller</td>
16:     </tr>
17:    </xsl:when>
18:     <xsl:otherwise>
19:     <tr style="font-weight:bold;color:red">
20:     <td>Guaranteed to be a bestseller or a dud</td>
21:     </tr>
22:    </xsl:otherwise>
23:   </xsl:choose>
24:  </xsl: for-each>

Reopen listing14-2.html and modify the lines that load the files so that listing15-3.xml is the XML file and listing15-5.xsl is the XSL file. The output when it's viewed in IE is shown in Figure 15.5.

Figure 15.5 Illustrating the xsl:otherwise element.

For the three values of yes, no, and impossible, there's an xsl:if element that will lead to specific actions. If the value of the BestSeller attribute is dud or guaranteed, the message in the xsl:otherwise option is displayed.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.