Choose XML Data with Comparision : Choose « JSTL « Java Tutorial






JSTL code

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %><%@ taglib uri="http://java.sun.com/jstl/xml" prefix="x" %>
<html>
  <head>
    <title>For Each Examples</title>
  </head>

  <body>
    <c:import var="students" url="students.xml" />

    <x:parse var="doc" xml="${students}" />

    <table border="1">
      <tr>
        <TH>First</th>

        <TH>Last</th>

        <TH>Points</th>

        <TH>Letter</th>

        <TH>Note</th>
      </tr>

      <x:forEach var="student" select="$doc/students/student">
        <tr>
          <td>
            <x:out select="name/first" />
          </td>

          <td>
            <x:out select="name/last" />
          </td>

          <td>
            <x:out select="grade/points" />
          </td>

          <td>
            <x:out select="grade/letter" />
          </td>

          <td>
            <x:choose>
              <x:when select="grade/points>90">You did
              great!</x:when>

              <x:when select="grade/points>80">You did
              good!</x:when>

              <x:when select="grade/points>75">You did
              ok.</x:when>

              <x:when select="grade/points>70">Well, you
              passed.</x:when>

              <x:otherwise>You failed</x:otherwise>
            </x:choose>
          </td>
        </tr>
      </x:forEach>
    </table>
  </body>
</html>

Demo XML document data

<?xml version="1.0" encoding="ISO-8859-1"?>
<students>
   <student id="1">
      <name>
         <first>A</first>
         <last>B</last>
         <middle>T</middle>
      </name>
      <grade>
         <points>88</points>
         <letter>B</letter>
      </grade>
   </student>
   <student id="2">
      <name>
         <first>C</first>
         <last>D</last>
         <middle>K</middle>
      </name>
      <grade>
         <points>92</points>
         <letter>A</letter>
      </grade>
   </student>
   <student id="3">
      <name>
         <first>E</first>
         <last>F</last>
         <middle>A</middle>
      </name>
      <grade>
         <points>72</points>
         <letter>C</letter>
      </grade>
   </student>
   
</students>
  Download:  JSTLChooseXMLDataWithComparision.zip( 1,561 k)








24.5.Choose
24.5.1.Tag Examples - choose
24.5.2.Combine If Chooser to Report Exception
24.5.3.Choose XML Data with Comparision
24.5.4.Choose When Statement
24.5.5.Choose When and Otherwise