您的位置:首页 > Web前端 > HTML

利用XSL解析XML为XHTML碰到的取值问题

2013-03-18 00:00 423 查看
小弟本段时间正在测试XSLT,在目前的使用过程中出现了一个诡异的情况,在定义了XML,XSL,XSD之后,在XML中不引用XSD的情况下能正常将XML解析为XHTML,但是引用了XSD之后,在引用XSL标签的地方都是获取不到正确的值,百思不得其解啊,求高手解惑。

XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="mail.xsl"?>
<html xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/forgotpwd forgotpwd.xsd "
xmlns="http://www.example.org/forgotpwd">
<content>
<subject>来自xx系统的密码重置邮件</subject>
<text>请点击以下链接重置密码:</text>
<link>#basePath#module/forgotPwd/resetPwd.jsp?email=#email#&key=#key#</link>
<tip>若链接无法跳转请复制该链接地址至您的浏览器并回车</tip>
</content>
</html>
XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.example.org/forgotpwd"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/forgotpwd"
elementFormDefault="qualified">
<xsd:element name="html">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="content">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="subject" type="xsd:string" />
<xsd:element name="text" type="xsd:string" />
<xsd:element name="link" type="xsd:string" />
<xsd:element name="tip" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<title>
<xsl:value-of select="html/content/subject"></xsl:value-of>
</title>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>

<xsl:template match="content">
<xsl:apply-templates select="text"></xsl:apply-templates>
<xsl:apply-templates select="link"></xsl:apply-templates>
<xsl:apply-templates select="tip"></xsl:apply-templates>
</xsl:template>

<xsl:template match="text">
<p>
<xsl:value-of select="."></xsl:value-of>
</p>
</xsl:template>

<xsl:template match="link">
<br />
<a>
<xsl:attribute name="href"><xsl:value-of select="."></xsl:value-of></xsl:attribute>
<xsl:value-of select="."></xsl:value-of>
</a>
<br />
</xsl:template>

<xsl:template match="tip">
<p>
<xsl:value-of select="."></xsl:value-of>
</p>
</xsl:template>
</xsl:stylesheet>
测试类:
package com.test.action;

import java.io.StringWriter;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class XHtmlTest {

public static void main(String[] args) {
try {
XHtmlTest test = new XHtmlTest();
test.transfer();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void transfer() throws Exception{
String basePath = XHtmlTest.class.getClassLoader().getResource(".").getPath();
Source xmlSource = new StreamSource(basePath + "com/test/action/forgotpwd.xml");
Source xsltSource = new StreamSource(basePath + "com/test/action/mail.xsl");
TransformerFactory transfact = TransformerFactory.newInstance();

StringWriter sw = new StringWriter();
Result result = new StreamResult(sw);

Transformer trans = transfact.newTransformer(xsltSource);
trans.transform(xmlSource, result);

System.out.println(sw.toString());
}

}
在不引用XSD情况下运行结果:
<html>
<title>来自xx系统的密码重置邮件</title>
<body>

<p>请点击以下链接重置密码:</p>
<br>
<a href="#basePath#module/forgotPwd/resetPwd.jsp?email=#email#&key=#key#">#basePath#module/forgotPwd/resetPwd.jsp?email=#email#&key=#key#</a>
<br>
<p>若链接无法跳转请复制该链接地址至您的浏览器并回车</p>

</body>
</html>
引用XSD之后的情况:
<html>
<title></title>
<body>

来自xx系统的密码重置邮件
请点击以下链接重置密码:
#basePath#module/forgotPwd/resetPwd.jsp?email=#email#&key=#key#
若链接无法跳转请复制该链接地址至您的浏览器并回车

</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: