English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

<x:set> tag

JSP Standard Tag Library

The <x:set> tag sets a variable for the value of the XPath expression.

If the value of the XPath expression is of boolean type, <x:set> will set a java.lang.Boolean object; if it is a string, it will set a java.lang.String object; if it is a number, it will set a java.lang.Number object.

Syntax format

<x:set var="<string>" select="<string>" scope="<string>"/>

Attribute

The <x:set> tag has the following attributes:

Attribute Description Is it necessary Default value
var Represents a variable for the value of the XPath expression Yes Body
select The XPath expression to be calculated No None
scope The scope of the var attribute No Page

Example demonstration

The following examples show us how to use the <x:set> tag:

<%@ 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 x:set Tag</title>
</head>
<body>
<h2>Books Info:</h2>
<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>

The running result is as follows:

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

JSP Standard Tag Library