JSTL - Etiqueta XML <x: set>
los <x:set> etiqueta establece una variable en el valor de una expresión XPath.
Si la expresión XPath resulta en un booleano, <x:set>etiqueta establece un objeto java.lang.Boolean; para una cadena, java.lang.String; y para un número, java.lang.Number.
Atributo
los <x:set> La etiqueta tiene los siguientes atributos:
Atributo | Descripción | Necesario | Defecto |
---|---|---|---|
var | Una variable que se establece en el valor de la expresión XPath | si | Cuerpo |
Seleccione | La expresión XPath a evaluar | No | Ninguna |
alcance | Alcance de la variable especificada en el atributo var | No | Página |
Ejemplo
El siguiente ejemplo le mostrará cómo utilizar el <x:set> etiqueta -
<%@ 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>
Acceda a la JSP anterior, se mostrará el siguiente resultado:
Books Info:
The price of the second book:[[book: null], [book: null]]