当前位置: 代码迷 >> HTML/CSS >> xslt转html二
  详细解决方案

xslt转html二

热度:390   发布时间:2013-11-08 17:51:56.0
xslt转html2
(转)java使用XSL+XML生成HTML文件2009-10-27 19:56转:http://www.blogjava.net/yangxiang/archive/2009/08/11/290688.html

1、xml.xml
<?xml version="1.0" encoding="utf-8"?>
<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>

2、xsl.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>

3、java.java
package test;

import java.io.File;
import java.io.IOException;

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

import org.junit.Test;

public class Train {
    @Test
    public void test() throws IOException {
        String xmlFileName = "d:/test/xml.xml";
        String xslFileName = "d:/test/xsl.xsl";
        String htmlFileName = "d:/test/html.html";
        Train.Transform(xmlFileName, xslFileName, htmlFileName);
    }

    public static void Transform(String xmlFileName, String xslFileName,
            String htmlFileName) {
        try {
            TransformerFactory tFac = TransformerFactory.newInstance();
            Source xslSource = new StreamSource(xslFileName);
            Transformer t = tFac.newTransformer(xslSource);
            File xmlFile = new File(xmlFileName);
            File htmlFile = new File(htmlFileName);
            Source source = new StreamSource(xmlFile);
            Result result = new StreamResult(htmlFile);
            System.out.println(result.toString());
            t.transform(source, result);
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }
    }
}
4、html.html
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>使用XML+XSLT生成的HTML文件</title>
</head>
<body>
    XML与JSP
    <br>
<br>第1章 认识XML与DTD<br>
<br>&nbsp;&nbsp;&nbsp;&nbsp;1.1. XML的产生
    <br>
<br>第2章 XML名称空间<br>
<br>&nbsp;&nbsp;&nbsp;&nbsp;2.1. 名称空间在元素和属性中的应用<br>
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2.1.1. 名称空间在元素中的应用123,456,789<br>
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2.1.2. 缺省名称空间123,456,789<br>
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2.1.3. 名称空间在属性中的应用123,456,789<br>
<br>&nbsp;&nbsp;&nbsp;&nbsp;2.2. 名称空间和DTD
</body>
</html>
5、效果
XML与JSP

第1章 认识XML与DTD

    1.1. XML的产生

第2章 XML名称空间

    2.1. 名称空间在元素和属性中的应用

        2.1.1. 名称空间在元素中的应用123,456,789

        2.1.2. 缺省名称空间123,456,789

        2.1.3. 名称空间在属性中的应用123,456,789

    2.2. 名称空间和DTD

  相关解决方案