zoukankan      html  css  js  c++  java
  • 【转】java使用XSL+XML生成HTML文件

    转自:http://www.blogjava.net/yangxiang/archive/2009/08/11/290688.html

    1、xml.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>

    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>    1.1. XML的产生
        <br>
    <br>第2章 XML名称空间<br>
    <br>    2.1. 名称空间在元素和属性中的应用<br>
    <br>        2.1.1. 名称空间在元素中的应用123,456,789<br>
    <br>        2.1.2. 缺省名称空间123,456,789<br>
    <br>        2.1.3. 名称空间在属性中的应用123,456,789<br>
    <br>    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
  • 相关阅读:
    【CSP-S膜你考】不怕噩梦 (模拟)
    【CSP-S膜你考】 A
    【CSP-S膜你考】即时战略(模拟)
    【CSP-S膜你考】最近公共祖先 (数学)
    【题解】洛谷 P1449 后缀表达式
    【题解】 洛谷 P2649 游戏预言
    【题解】洛谷 P1083 借教室
    【题解】洛谷 P1080 国王游戏
    【题解】洛谷 P1079 Vigenère 密码
    Bzoj4558 [JLoi2016]方
  • 原文地址:https://www.cnblogs.com/abinxm/p/2350609.html
Copyright © 2011-2022 走看看