|
在JAVA中转換示例如下:
books.xml
<?xml version="1.0" encoding="ISO-8859-1"?><bookstore><book category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price></book><book category="CHILDREN"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price></book><book category="WEB"> <title lang="en">XQuery Kick Start</title> <author>James McGovern</author> <author>Per Bothner</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</author> <year>2003</year> <price>49.99</price></book><book category="WEB"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price></book></bookstore>
books.xsl
<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"><html><body><h2>This is my books</h2><table border="1"><tr><th>book title</th><th>book author</th></tr><xsl:for-each select="bookstore/book"><tr><td><xsl:value-of select="title"/></td><td><xsl:value-of select="author"/></td></tr></xsl:for-each></table></body></html></xsl:template></xsl:stylesheet>
转換JAVA文件:
package com.lwf.test;import javax.xml.transform.Result;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerConfigurationException;import javax.xml.transform.TransformerException;import javax.xml.transform.TransformerFactory;import javax.xml.transform.TransformerFactoryConfigurationError;import javax.xml.transform.stream.StreamResult;import javax.xml.transform.stream.StreamSource;public class Test {/** * @param args */public static void main(String[] args) {try {StreamSource xmlSource = new StreamSource("E:\\workspace_g2\\TT\\src\\books.xml");StreamSource xsl = new StreamSource("E:\\workspace_g2\\TT\\src\\books.xsl");Result outputTarget = new StreamResult("E:\\workspace_g2\\TT\\src\\bookR.xml");Transformer ts = TransformerFactory.newInstance().newTransformer(xsl);ts.transform(xmlSource, outputTarget);} catch (TransformerConfigurationException e) {e.printStackTrace();} catch (TransformerFactoryConfigurationError e) {e.printStackTrace();} catch (TransformerException e) {e.printStackTrace();}}}
转换后的文件bookR.xml
<html><body><h2>This is my books</h2><table border="1"><tr><th>book title</th><th>book author</th></tr><tr><td>Everyday Italian</td><td>Giada De Laurentiis</td></tr><tr><td>Harry Potter</td><td>J K. Rowling</td></tr><tr><td>XQuery Kick Start</td><td>James McGovern</td></tr><tr><td>Learning XML</td><td>Erik T. Ray</td></tr></table></body></html>
这样在服务器端就将XML经XSL转换了。
服务器端也可将Document类型经xsl文件转换成另一Document代码如下
public static Document generateXMLDOM(Document tradeDoc, String xslFile){Document doc = null;try {DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();DocumentBuilder db = dbf.newDocumentBuilder();Document document = db.parse(xslFile);DOMSource source = new DOMSource(document);Transformer tf = TransformerFactory.newInstance().newTransformer(source);//单线程//Templates ts = TransformerFactory.newInstance().newTemplates(source);//Transformer tf = ts.newTransformer();tf.transform(new DOMSource(tradeDoc), new DOMResult(doc));} catch (TransformerConfigurationException e) {e.printStackTrace();} catch (ParserConfigurationException e) {e.printStackTrace();} catch (SAXException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} catch (TransformerFactoryConfigurationError e) {e.printStackTrace();} catch (TransformerException e) {e.printStackTrace();}return doc;} |
|