把三个文件放在同一目录下。
JSP页面:
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- <%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Insert title here</title>
- </head>
- <body>
- <!-- import的url是相对于当前jsp文件所在的路径的 -->
- <c:import url="users.xml" var="xml" />
- <c:import url="xslt.xsl" var="xslt" />
- <x:parse xml="${xml }" var="users" />
- 使用c:set 设置字符串字面量<br/>
- <c:set var="stringletral" value="string letral value" />
- 字符串字面量:<x:out select="$stringletral"/><br/>
- <br/>
- 使用c:set 设置XML节点集<br/>
- <c:set var="nodeSet" value="${users }"/>
- 用户总数:<x:out select="count($nodeSet//user)"/><br/><br/>
- 迭代输出前两个(根据节点的位置过滤):<br/>
- <x:forEach select="$users//user[position() < 3]" var="user">
- 姓名:<font color="red" ><x:out select="name"/></font>;姓名长度:<x:out select="string-length(name)" /><br/>
- ID:<x:out select="id"/><br/>
- desc:<x:out select="desc"/><br/>
- schoole:<x:out select="schoole"/><br/>
- age:<x:out select="age"/><br/>
- <x:out select="position()"/>
- <br/>
- </x:forEach>
- <font color="red">
- 刷选年龄小于26岁的(根据属性过滤):<br/>
- <x:forEach select="$users//user[age < '26']" var="user">
- 姓名:<x:out select="./name"/><br/>
- ID:<x:out select="./id"/><br/>
- desc:<x:out select="./desc"/><br/>
- schoole:<x:out select="./schoole"/><br/>
- age:<x:out select="./age"/><br/>
- <br/>
- </x:forEach>
- </font>
- <br/><br/>
- 样式转换:<br/>
- <x:transform xml="${xml }" xslt="${xslt }" />
- <br/><br/>
- </body>
- </html>
users.xml 数据文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <users>
- <user>
- <name>liuwh</name>
- <id>123456</id>
- <desc>super</desc>
- <schoole>gdut.edu.cn</schoole>
- <age>26</age>
- </user>
- <user>
- <name>wangxiaowei</name>
- <id>234567</id>
- <desc>good man</desc>
- <schoole>moyang.edu.cn</schoole>
- <age>26</age>
- </user>
- <user>
- <name>zouyubin</name>
- <id>345678</id>
- <desc>man</desc>
- <schoole>hunan.edu.cn</schoole>
- <age>25</age>
- </user>
- <user>
- <name>tanqing</name>
- <id>456789</id>
- <desc>womman</desc>
- <schoole>scut.edu.cn</schoole>
- <age>26</age>
- </user>
- <user>
- <name>jingli</name>
- <id>567890</id>
- <desc>super man</desc>
- <schoole>haiyang.edu.cn</schoole>
- <age>27</age>
- </user>
- </users>
样式转换文件:xslt.xsl
- <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
- version="1.0" >
- <xsl:template match='/'>
- <table border='1'>
- <tr>
- <th>index</th>
- <th>name</th>
- <th>id</th>
- <th>desc</th>
- <th>schoole</th>
- <th>age</th>
- </tr>
- <xsl:call-template name="outUser">
- <xsl:with-param name="users" select="users" />
- </xsl:call-template>
- </table>
- </xsl:template>
- <xsl:template name="outUser">
- <xsl:param name="users" />
- <xsl:for-each select="$users/user">
- <tr>
- <td><xsl:value-of select="position()" /></td>
- <xsl:choose>
- <xsl:when test="position() mod 2 = 0">
- <td><font color="red"><xsl:value-of select="name" /></font></td>
- </xsl:when>
- <xsl:otherwise>
- <td><xsl:value-of select="name" /></td>
- </xsl:otherwise>
- </xsl:choose>
- <td><xsl:value-of select="id" /></td>
- <td><xsl:value-of select="desc" /></td>
- <td><xsl:value-of select="schoole" /></td>
- <td><xsl:value-of select="age" /></td>
- </tr>
- </xsl:for-each>
- </xsl:template>
- </xsl:stylesheet>
运行结果: