Java根据xsl和xml生成html网页

Java代码:


package test;

import java.io.IOException;
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 Test {
	public static void main(String[] args) throws IOException {
		String xmlFileName = "source/xml.xml";
		String xslFileName = "source/xml.xsl";
		try {
			String s = new Test().Transform(xmlFileName, xslFileName);
			System.out.println(s);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String Transform(String xmlFileName, String xslFileName) throws Exception {
			TransformerFactory tFac = TransformerFactory.newInstance();
			Source xslSource = new StreamSource(this.getClass().getClassLoader().getResourceAsStream(xslFileName));
			Transformer t = tFac.newTransformer(xslSource);
			Source source = new StreamSource(this.getClass().getClassLoader().getResourceAsStream(xmlFileName));
			StringWriter writer = new StringWriter();
			Result result = new StreamResult(writer);
			t.transform(source, result);
			return writer.toString();
	}
}

XML文件:

<?xml version="1.0" encoding="utf-8"?>


<?xml-stylesheet type="text/xsl" href="xsl.xsl"?>
<book>

	<title>XML与JSP</title>
	<chapter>
		<title>第1章 认识XML与DTD</title>
		<section>
			<title>XML的产生</title>
			<example>HelloWorld.html</example>
		</section>
	</chapter>
	<chapter>
		<title>第2章 XML名称空间</title>
		<section>
			<title>名称空间在元素和属性中的应用</title>
			<section>
				<title>名称空间在元素中的应用</title>
				<example>people.xml</example>
			</section>
			<section>
				<title>缺省名称空间</title>
				<example>book.xml</example>
			</section>
			<section>
				<title>名称空间在属性中的应用</title>
				<example>book2.xml</example>
			</section>
		</section>
		<section>
			<title>名称空间和DTD</title>
		</section>
	</chapter>
</book>

 


XSL文件:



<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" version="1.0" encoding="utf-8" standalone="yes"/>
    <xsl:template match="/">
        <html>
            <head>
                <title>使用XML+XSLT生成的HTML文件</title>
            </head>
            <body>
                <xsl:apply-templates select="book"/>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="chapter">
        <br/>
        <br/>
        <xsl:value-of select="./title"/>
        <xsl:apply-templates select="./section"/>
    </xsl:template>
    <xsl:template match="chapter/section">
        <br/>
        <br/>
        <xsl:text>    </xsl:text>
        <!--<xsl:number format="1. " level="multiple"/>-->
        <xsl:number format="1. " level="multiple" count="chapter | section" from="book"/>
        <xsl:value-of select="./title"/>
        <xsl:apply-templates select="./section"/>
    </xsl:template>
    <xsl:template match="chapter/section/section">
        <br/>
        <br/>
        <xsl:text>        </xsl:text>
        <!--<xsl:number format="1. " level="multiple"/>-->
        <xsl:number format="1. " level="multiple" count="chapter | section" from="book"/>
        <xsl:value-of select="./title"/>
        <xsl:number value="123456789" grouping-separator="," grouping-size="3"/>
    </xsl:template>
</xsl:stylesheet>


本博客所有文章如无特别注明均为原创。作者:封笔尘缘复制或转载请以超链接形式注明转自 封笔尘缘
原文地址《Java根据xsl和xml生成html网页
分享到:更多

相关推荐

网友评论(0)