<x:set>标签为XPath表达式的值设置一个变量。
如果XPath表达式的值是boolean类型,则<x:set>将会设置一个java.lang.Boolean对象,若是字符串,则设置一个java.lang.String对象,若是数字,则设置一个java.lang.Number对象。
<x:set var="<string>" select="<string>" scope="<string>"/>
<x:set>标签有如下属性:
| 属性 | 描述 | 是否必要 | 默认值 | 
|---|---|---|---|
| var | 代表XPath表达式值得变量 | 是 | Body | 
| select | 需要计算的XPath表达式 | 否 | 无 | 
| scope | var属性的作用域 | 否 | Page | 
接下来的例子告诉我们如何使用<x:set>标签:
<%@ 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 标签</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>
运行结果如下:
BOOKS INFO: The price of the second book:[[book: null], [book: null]]