利用XSL解析XML为XHTML碰到的取值问题
2013-03-18 00:00
423 查看
小弟本段时间正在测试XSLT,在目前的使用过程中出现了一个诡异的情况,在定义了XML,XSL,XSD之后,在XML中不引用XSD的情况下能正常将XML解析为XHTML,但是引用了XSD之后,在引用XSL标签的地方都是获取不到正确的值,百思不得其解啊,求高手解惑。
XML:
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>
相关文章推荐
- 关于利用Schema解析XML,生成、编辑XML的节点问题
- 在Resin3下配置SpringSide的HelloWorld示例程序碰到的问题和解决方法(说白了还是resin3.0解析xml问题)
- dom4j解析xml中碰到的两个问题
- 使用REXML解析XML时碰到invalid byte sequence in GBK (ArgumentError)问题
- 利用 NSXMLParser 类解析 XML 文件时所遇到的问题
- DOM4j解析XMl中碰到的问题解决
- C# 解析html —— 将html转为XHTML,然后利用Xml解析
- android xml 解析汉字只出来一个字的问题
- 利用JAXP和DOM4J解析XML
- VC++中利用MSXML解析XML文档
- 解析xml的问题未解决
- 使用 XHTML 的局限有哪些?如果页面使用 'application/xhtml+xml' 会有什么问题吗?
- 利用MSXML解析XML文本(VC)
- Android中采用XmlPullParser解析xml文件错误的有关问题
- 利用XML和XSL生成较为复杂的多表头表格
- 用了spring后XML解析的问题
- 小知识积累-C++使用tinyxml解析Xml内存泄漏问题
- 利用dom4j解析.xml之查询属性
- 利用python处理xml -- 中文编码问题
- Python解析大XML文件及读取XML不全的问题