struts框架下,在jsp页面显示XSL格式化的XML。需要以下步骤:
1 页面显示前XML的Dom对象和Xsl文件地址存入request
public class LoadDataAction extends Action {
private Document mydata;
private String xslPath;
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception{
mydata=loadData();
xslPath=”resources/example.xsl”;
//XML的Dom对象
request.setAttribute("userXml",mydata);
//Xsl文件地址
request.setAttribute("bodyXsl",xslPath);
return mapping.findForward("success");
}
private Document loadData(){
Document result=null;
try{
//创建XML的Dom对象,XML的DTD下面给出
}catch(Exception e){
e.printStackTrace();
}
return result;
}
}
dtd文件内容
<?xml version="1.0" encoding="UTF-8"?>
<!--DTD generated by XMLSpy v2005 rel. 3 U (http://www.altova.com)-->
<!--the entity declarations may be overridden in the internal subset-->
<!--the declarations below should not be modified-->
<!--element name mappings-->
<!ENTITY % UsersBasicInfo "UsersBasicInfo">
<!ENTITY % UserBasicInfo "UserBasicInfo">
<!ENTITY % UserID "UserID">
<!ENTITY % Password "Password">
<!ENTITY % UserName "UserName">
<!ENTITY % Email "Email">
<!--element and attribute declarations-->
<!--Title: ubi.xsd
Subject: the user basic information.
Publisher: Nova Corporation, Colimas.
Format: text/xml
Creator: Zhao Lei
Date.Created: 2005-04-26
Language: en-US
Description: User Basic Information Data Definition
Change Log:
Version Date Modifier Description
01.00 2005/04/26 Zhao Lei Initial release.
-->
<!ELEMENT %UsersBasicInfo; ((%UserBasicInfo;)*)>
<!--One User Basic Information-->
<!ELEMENT %UserBasicInfo; (%UserID;, (%Password;)?, %UserName;, %Email;)>
<!--The max Length of characters of it is 20-->
<!ELEMENT %UserID; (#PCDATA)>
<!--Show it only when user is administor. 12 fixed characters-->
<!ELEMENT %Password; (#PCDATA)>
<!ELEMENT %UserName; (#PCDATA)>
<!ELEMENT %Email; (#PCDATA)>
xsl文件内容
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:variable name="userinfo.comp" select="'Component Name'"/>
<xsl:variable name="userinfo.id" select="'Serial No'"/>
<xsl:variable name="userinfo.role" select="'Role Name'"/>
<xsl:variable name="userinfo.user" select="'User ID*'"/>
<xsl:variable name="userinfo.name" select="'User Name*'"/>
<xsl:variable name="userinfo.passord" select="'Password*'"/>
<xsl:variable name="userinfo.mail" select="'Email Address*'"/>
<xsl:variable name="userinfo.userp" select="'User Profile'"/>
<xsl:variable name="userinfo.required" select="'All items are required'"/>
<xsl:variable name="template.edit" select="'Edit!'"/>
<xsl:variable name="userinfo.access" select="'Access Role List'"/>
<xsl:template match="UserBasicInfo">
<h1><xsl:value-of select="$userinfo.userp"/></h1>
<xsl:variable name="userid" select="UserID"/>
<input type="button" name="edit" value="{$template.edit}"/>
<p><font color="#003399" size="4"><xsl:value-of select="$userinfo.required"/></font></p>
<table border="0" width="826" height="103" cellpadding="2" cellspacing="1">
<tr>
<td bgcolor="#E0F1FF"><xsl:value-of select="$userinfo.user"/></td>
<td ><xsl:value-of select="$userid"/></td>
</tr>
<tr>
<td bgcolor="#E0F1FF"><xsl:value-of select="$userinfo.passord"/></td>
<td><xsl:value-of select="Password"/></td>
</tr>
<tr>
<td bgcolor="#E0F1FF"><xsl:value-of select="$userinfo.name"/></td>
<td><xsl:value-of select="UserName"/></td>
</tr>
<tr>
<td bgcolor="#E0F1FF"><xsl:value-of select="$userinfo.mail"/></td>
<td><xsl:value-of select="Email"/></td>
</tr>
</table>
</xsl:template>
</xsl:stylesheet>
2 创建xsl的tag
tld文件内容如下:
<?<span s