获取XML数据并使用样式表格式化
使用样式表格式化获得的XML数据,这个如果使用单独一个XML文件是完全可以的,在XML文件里面嵌入SXL文件,但是现在不要这么作,直接获取XML数据,然后调用XSL文件格式化数据。
这部分演示程序需要使用到AJAX客户端框架,如下例子所示:
1.ceshi.jsp
<%@ page contentType="text/html;charset=GBK"%>
<script language="javascript" src="js\request.js"></script>
<script language="javascript">
function showXML(){
var url = "Vehicles.xml";
get_request( url, "text" );
}
function pageChange( responseText ){
var xml = new ActiveXObject( "Microsoft.XMLDOM" );
xml.async = false;
//load the text of xml data
xml.loadXML( responseText );
var xsl = new ActiveXObject( "Microsoft.XMLDOM" );
xsl.async = false;
// Load the XSL
xsl.load( "Vehicles.xsl" );
var div = document.getElementById( "show" );
div.innerHTML = xml.transformNode( xsl );
/*
load()函数加载XML文件
loadXML()函数加载字符串形式的XML数据
*/
}
</script>
<p>
<input type="submit" name="Submit" value="提交" onclick="showXML()" />
</p>
<div id="show"></div>
<script language="javascript" src="js\request.js"></script>
<script language="javascript">
function showXML(){
var url = "Vehicles.xml";
get_request( url, "text" );
}
function pageChange( responseText ){
var xml = new ActiveXObject( "Microsoft.XMLDOM" );
xml.async = false;
//load the text of xml data
xml.loadXML( responseText );
var xsl = new ActiveXObject( "Microsoft.XMLDOM" );
xsl.async = false;
// Load the XSL
xsl.load( "Vehicles.xsl" );
var div = document.getElementById( "show" );
div.innerHTML = xml.transformNode( xsl );
/*
load()函数加载XML文件
loadXML()函数加载字符串形式的XML数据
*/
}
</script>
<p>
<input type="submit" name="Submit" value="提交" onclick="showXML()" />
</p>
<div id="show"></div>
2.Vehicles.xml
<?xml version="1.0" encoding="gb2312"?>
<!--?xml-stylesheet href='Vehicles.xsl' type='text/xsl'?-->
<vehicles>
<vehicle year="1002" make="Land Rover" model="Discovery">
<mileage>36500</mileage>
<color>black</color>
<price>$32999</price>
</vehicle>
</vehicles>
<!--?xml-stylesheet href='Vehicles.xsl' type='text/xsl'?-->
<vehicles>
<vehicle year="1002" make="Land Rover" model="Discovery">
<mileage>36500</mileage>
<color>black</color>
<price>$32999</price>
</vehicle>
</vehicles>
3.Vehicles.xsl
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD=xsl">
<xsl:template match="/">
<html>
<head>
<title>uuu</title>
</head>
<body>
<table align="center" border="2">
<tr>
<th>Year</th>
<th>Make</th>
<th>Model</th>
<th>Mileage</th>
<th>Color</th>
<th>Price</th>
</tr>
<xsl:for-each order-by="+price" select="vehicles/vehicle">
<tr>
<td><xsl:value-of select="@year"/></td>
<td><xsl:value-of select="@make"/></td>
<td><xsl:value-of select="@model"/></td>
<td><xsl:value-of select="mileage"/></td>
<td><xsl:value-of select="color"/></td>
<td><xsl:value-of select="price"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD=xsl">
<xsl:template match="/">
<html>
<head>
<title>uuu</title>
</head>
<body>
<table align="center" border="2">
<tr>
<th>Year</th>
<th>Make</th>
<th>Model</th>
<th>Mileage</th>
<th>Color</th>
<th>Price</th>
</tr>
<xsl:for-each order-by="+price" select="vehicles/vehicle">
<tr>
<td><xsl:value-of select="@year"/></td>
<td><xsl:value-of select="@make"/></td>
<td><xsl:value-of select="@model"/></td>
<td><xsl:value-of select="mileage"/></td>
<td><xsl:value-of select="color"/></td>
<td><xsl:value-of select="price"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
4.说明
需要注意的XML加载后,XSL文件的加载,以及 xml.transformNode( xsl ) 方法。