Set Variable by Selecting XML Path : XML Path « JSTL « Java Tutorial






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

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

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

    <x:set var="a" select="$doc/students/student/name/first" />

    <x:set var="b"
    select="$doc/students/student[@id=2]/name/first" />

    <x:set var="c"
    select="$doc/students/student[@id=1]/name/first" />

    <table border="1">
      <tr>
        <td>$doc/students/student/name/first</td>

        <td>
          <x:out select="$a" />
        </td>
      </tr>

      <tr>
        <td>$doc/students/student[@id=2]/name/first</td>

        <td>
          <x:out select="$b" />
        </td>
      </tr>

      <tr>
        <td>$doc/students/student[@id=1]/name/first</td>

        <td>
          <x:out select="$c" />
        </td>
      </tr>
    </table>
  </body>
</html>
<?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:  JSTLSetVariableBySelectingXMLPath.zip( 1,582 k)








24.33.XML Path
24.33.1.Output XML with Select Statement
24.33.2.Use ForEach Loop to Select XML Path
24.33.3.Set Variable by Selecting XML Path
24.33.4.Parse XML With XPath Expression
24.33.5.Parse XML and Path Select
24.33.6.Use JSTL to output and extract XML element with XPATH