JSTL - Balise XML <x: set>

le <x:set> La balise définit une variable sur la valeur d'une expression XPath.

Si l'expression XPath donne un booléen, <x:set>tag définit un objet java.lang.Boolean; pour une chaîne, java.lang.String; et pour un nombre, java.lang.Number.

Attribut

le <x:set> tag a les attributs suivants -

Attribut La description Obligatoire Défaut
var Une variable définie sur la valeur de l'expression XPath Oui Corps
sélectionner L'expression XPath à évaluer Non Aucun
portée Portée de la variable spécifiée dans l'attribut var Non Page

Exemple

L'exemple suivant montre comment utiliser le <x:set> étiquette -

<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix = "x" uri = "http://java.sun.com/jsp/jstl/xml" %>

<html>
   <head>
      <title>JSTL Tags</title>
   </head>

   <body>
      <h3>Books Info:</h3>

      <c:set var = "xmltext">
         <books>
            <book>
               <name>Padam History</name>
               <author>ZARA</author>
               <price>100</price>
            </book>
            
            <book>
               <name>Great Mistry</name>
               <author>NUHA</author>
               <price>2000</price>
            </book>
         </books>
      </c:set>

      <x:parse xml = "${xmltext}" var = "output"/>
      <x:set var = "fragment" select = "$output//book"/>
      <b>The price of the second book</b>: 
      <c:out value = "${fragment}" />
   </body>
</html>

Accédez au JSP ci-dessus, le résultat suivant sera affiché -

Books Info:

The price of the second book:[[book: null], [book: null]]