jsp:xpath

.1 关于“/ ”和“// ”的使用
“/”是表示当前文档的节点,类似 DOS 目录分割符,“//”则表示当前文档所有的节点。类似查看
整个目录。
(1)/authors/author:表示选择根目录下、父节点为 authors 的元素“author”。

(2)/authors/author/name:表示查找到所有名称为“name”的元素,但是它的父节点为“author”,
而“author”的父节点又为“authors”。
(3)//name:表示查找 XML 文件中的所有“name”元素,而无论这个元素在什么层次。

.2 关于“* ”的使用
“*”标记表示某个层次上的所有元素。
(1)/authors/author/*:表示在 author 元素(它的父节点为 authors)下的所有子元素(本例为 name
和 nationality 两个元素)。
(2)/authors/*/name:表示查找所有名为 name 的元素,而不管它的父节点是谁,或者是 author,
也或者是 super-author 元素,但是再上一个父节点确必须是 authors 元素。
(3)//*:表示查找到所有元素。

3 路径分支
方括号表示路径分支。
(1)/authors/author[nationality]/name:表示只查找那些在 authors 元素下包含 nationality 子元素的
author 元素的 name 节点。
(2)authors/author[nationality=’Russian’]/name:表示查找那些nationality子元素值为German的author
元素下的 name 节点,而且 author 元素的父节点为 authors。

XML核心动作标签
包括的核心动作标签有如下几个:
<x.parse>:用于解析 XML 文件。
<x.out>:通过 XPath 来读取 XML 文件中的某元素。
<x:set>:该标签用来计算 XPath 表达式,并且把结果保存在指定的变量当中。

<x:parse> 标签
该标签的使用格式如下:
<x:parse doc=”XMLDocument”
[var=”varName”] [scope=”scope”] | [varDom=”varName”] [scopeDom=”scope”]
[systemId=”systemId”] [filter=”filter”] />
标签中的各属性描述如下:
doc:指定要解析的 XML 文件。一般使用<c:import>检索到相应的 XML 文件。
var:把解析之后的 XML 文件存储在 var 属性指定的变量中。
scope:设置 var 属性指定的变量有效范围。
varDom:存储解析后的 XML 文件。
scopeDom:设置 varDom 属性指定变量的有效范围。
systemId:指定 XML 文件的 URI。
filter:对应的是 org.xml.sax.XMLFilter 类。

例子:

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>

<h4>解析xml</h4>
<c:set var="xmlText">
	<a>
		<b>
			<c>good</c>
		</b>
		<d>
			<c>body</c>
		</d>
	</a>
</c:set>
<hr>
<x:parse var="xmlDoc" doc="${xmlText}" />
<x:out select="$xmlDoc//c"/><br>
<x:out select="$xmlDoc/a/d"/><br>
<x:out select="$xmlDoc/a/*"/><br>

</body>
</html>

  

<x:out> 标签
该标签是用于计算 XPath 表达式,然后把查找到的元素进行输出。
其一般的使用格式如下:
<x:out select=”XPathExpression” [escapeXml=”true|false”] />
属性说明如下:
select:将要计算的 XPath 表达式。
escapeXml:确定<、>、&、’、”这些字符是否被转换成字符实体代码,默认为 true。

<?xml version="1.0" encoding="UTF-8"?>
<games>
<country id="Luxembourg">
<athlete>
<name>Lux 1</name>
<sport>swimming</sport>
<age>23</age>
<gender>M</gender>
</athlete>
<athlete>
<name>Lux 2</name>
<sport>wrestling</sport>
<age>31</age>
<gender>M</gender>
</athlete>
</country>
<country id="Denmark">
<athlete>
<name>Den 1</name>
<sport>cycling</sport>
<age>18</age>
<gender>F</gender>
</athlete>
<athlete>
<name>Den 2</name>
<sport>sailing</sport>
<age>27</age>
<gender>M</gender>
</athlete>
</country>
</games>

  

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>games</title>
</head>
<body>
<h4>parse和out输出</h4>
<!-- 引入文件 -->
<c:import var="doString" url="games.xml"/>
<!-- 开始解析xml -->
<x:parse var="doc" doc="${doString}"/>
<table border="1">
<tr>
<td valign="top"><pre><c:out value="${doString}"/></pre></td>
<td valign="top">
	<!-- 内嵌table -->
	<table border="1">
	<tr>
		<td>XPath元素</td>
		<td>查找到的元素</td>
	</tr>	
	<tr>
		<td>$doc//sport[只取第一个]</td>
		<td><x:out select="$doc//sport"/></td>
	</tr>
	<tr>
		<td>$doc/games/country/*</td>
		<td><x:out select="$doc/games/country/*"/></td>
	</tr>
	<tr>
		<td>$doc//*</td>
		<td><x:out select="$doc//*"/></td>
	</tr>
	<tr>
		<td>$doc/games/country</td>
		<td><x:out select="$doc/games/country"/></td>
	</tr>
	<tr>
		<td>$doc/games/country/[Last()][取最后一个元素]</td>
		<td><x:out select="$doc/games/country[last()]"/></td>
	</tr>
	<tr>
		<td>$doc//@id[取第一个id]</td>
		<td><pre><x:out select="$doc//@id"/></pre></td>
	</tr>
	<tr>
		<td>$doc//country/[@id="Denmark""]</td>
		<td><pre><x:out select="$doc//country[@id='Denmark']"/></pre></td>
	</tr>
	</table>
	<!-- 内嵌table -->
</td>
</tr>	
</table>
</body>
</html>

  

<x:set> 标签
该标签首先计算 XPath 表达式,然后把计算的结果保存在指定的变量中,而不是输出。
<x:set>标签的一般使用格式如下:
<x:set select=”XPathExpression”
var=”varName” [scope=”page|request|session|application”] />
标签中各属性描述如下:
select:将要被计算的 XPath 表达式。
var:把 XPath 表达式计算之后的值保存在这个属性所指定的变量中,以后可以通过指定的变量
来引用这个值。
scope:设置 var 属性所指定变量的有效范围。

<c:set var="xmlText">
<a>
	<d>
		<c>baby</c>
		<k>good</k>
	</d>
</a>
</c:set>

<x:set var="doc" select="$xmlText//a"/>
<x:out select="$doc"/>

  

原文地址:https://www.cnblogs.com/achengmu/p/8325724.html