如何在 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