如何在 JSP 中将 XPath 表达式结果的值存储到变量中?
<x:set> 标签将变量设置为 XPath 表达式的值。
如果 XPath 表达式结果为布尔值,则 <x:set> 标签设置一个 java.lang.Boolean 对象;对于字符串,则设置 java.lang.String 对象;对于数字,则设置 java.lang.Number 对象。
属性
<x:set> 标签具有以下属性:
| 属性 | 描述 | 必填 | 默认值 |
|---|---|---|---|
| var | 设置为 XPath 表达式值的变量 | 是 | 主体 |
| select | 要评估的 XPath 表达式 | 否 | 无 |
| scope | var 属性中指定的变量的作用域 | 否 | 页面 |
示例
以下示例将演示如何使用 <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 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>访问上述 JSP,将显示以下结果:
Books Info: The price of the second book:[[book: null], [book: null]]
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP