<x:set> 标签


<x:set>标签为XPath表达式的值设置一个变量。

如果XPath表达式的值是boolean类型,则<x:set>将会设置一个java.lang.Boolean对象,若是字符串,则设置一个java.lang.String对象,若是数字,则设置一个java.lang.Number对象。

属性

<x:set>标签有如下属性:

属性 描述 是否必要 默认值
var 代表XPath表达式值得变量 Body
select 需要计算的XPath表达式
scope var属性的作用域 Page

实例演示

接下来的例子告诉我们如何使用<x:set>标签:

  1. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  2. <%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
  3.  
  4. <html>
  5. <head>
  6. <title>JSTL x:set 标签</title>
  7. </head>
  8. <body>
  9. <h3>Books Info:</h3>
  10.  
  11. <c:set var="xmltext">
  12. <books>
  13. <book>
  14. <name>Padam History</name>
  15. <author>ZARA</author>
  16. <price>100</price>
  17. </book>
  18. <book>
  19. <name>Great Mistry</name>
  20. <author>NUHA</author>
  21. <price>2000</price>
  22. </book>
  23. </books>
  24. </c:set>
  25.  
  26. <x:parse xml="${xmltext}" var="output"/>
  27. <x:set var="fragment" select="$output//book"/>
  28. <b>The price of the second book</b>:
  29. <c:out value="${fragment}" />
  30. </body>
  31. </html>

运行结果如下:

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