xml关联xsl的四种方法及Transformer的transform方法学习心得

一、直接在顶层元素的上面假如<?xml-stylesheet type="text/xsl" href="XXX.xsl"?>

二、先创建一个空文档,利用appendChild方法加入由Document实例的方法

createProcessingInstruction("xml-stylesheet","type=/"text/xsl/" href=/"XXX.xsl/");

三、利用TransformerFactory的newTransformer(new StreamSource(XXX.xsl))关联xsl

四、利用Transformer的newTemplates(new StreamSource(XXX.xsl))关联xsl ,再用Templates实例创建newTransformer实例

transform(Source , Result)的参数:
Source 可以是StreamSource的实例也可以是DOMSource的实例。
StreamSource实例可以通过new StreamSource(File file)也可以new StreamSource( Inputstream
inputstream) 其中的参数都是xml源文档。

Result 可以是 SteamResult的实例。SteamResult的实例可以通过new StreamResult(response.getWriter())或new StreamResul(response.getOutputStream())获得; 

不对的地方请各位指出,谢谢!!

可以使用XSLT(XSL Transformations)来将XML转换为HTML。以下是一个简单的示例: XML文件(example.xml): ```xml <?xml version="1.0" encoding="UTF-8"?> <catalog> <book id="bk101"> <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> <description>An in-depth look at creating applications with XML.</description> </book> <book id="bk102"> <author>Ralls, Kim</author> <title>Midnight Rain</title> <genre>Fantasy</genre> <price>5.95</price> <publish_date>2000-12-16</publish_date> <description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description> </book> </catalog> ``` XSL文件(example.xsl): ```xml <?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> <body> <h2>My Book Catalog</h2> <table border="1"> <tr> <th>ID</th> <th>Author</th> <th>Title</th> <th>Genre</th> <th>Price</th> <th>Publish Date</th> <th>Description</th> </tr> <xsl:for-each select="catalog/book"> <tr> <td><xsl:value-of select="@id"/></td> <td><xsl:value-of select="author"/></td> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="genre"/></td> <td><xsl:value-of select="price"/></td> <td><xsl:value-of select="publish_date"/></td> <td><xsl:value-of select="description"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> ``` 使用命令行工具或代码解析器将XMLXSL文件传递给XSLT引擎,生成HTML文件: ```html <html> <body> <h2>My Book Catalog</h2> <table border="1"> <tr> <th>ID</th> <th>Author</th> <th>Title</th> <th>Genre</th> <th>Price</th> <th>Publish Date</th> <th>Description</th> </tr> <tr> <td>bk101</td> <td>Gambardella, Matthew</td> <td>XML Developer's Guide</td> <td>Computer</td> <td>44.95</td> <td>2000-10-01</td> <td>An in-depth look at creating applications with XML.</td> </tr> <tr> <td>bk102</td> <td>Ralls, Kim</td> <td>Midnight Rain</td> <td>Fantasy</td> <td>5.95</td> <td>2000-12-16</td> <td>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</td> </tr> </table> </body> </html> ``` 可以使用各种XSLT引擎来执行此转换。例如,如果您有Java环境,则可以使用Saxon XSLT引擎。以下是一个使用Saxon HE的Java代码示例: ```java import java.io.File; import java.io.FileOutputStream; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; public class XmlToHtml { public static void main(String[] args) throws Exception { TransformerFactory factory = TransformerFactory.newInstance(); StreamSource xslStream = new StreamSource(new File("example.xsl")); Transformer transformer = factory.newTransformer(xslStream); StreamSource in = new StreamSource(new File("example.xml")); FileOutputStream out = new FileOutputStream(new File("example.html")); transformer.transform(in, new StreamResult(out)); System.out.println("HTML file generated."); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值