簡體   English   中英

通過使用xsl java將xhtml轉換為html

[英]xhtml to html by using xsl java

我有template.xsl和template_index.xsl模板,用於從xhtml文件動態創建html。 從任何文件夾本地,我都可以打開該xhtml文件並查看xsl創建的內容。 因此,現在我需要從服務器運行此文件,並通過尋址到服務器的鏈接來查看內容。 但是當我想從服務器查看內容時它是空的。 我做錯了什么?

template.xsl:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ui="ui" xmlns:e="entity" xmlns:page="page" xmlns="html" xmlns:exslt="http://exslt.org/common" exclude-result-prefixes="exslt page e ui html">

        <!-- <Includes> -->

        <xsl:output method="html" omit-xml-declaration="yes" indent="no"/>

        <xsl:strip-space elements="*" />

        <!-- <Data> -->

        <page:page name="about" title="API"></page:page>
        <!--xsl:param name="page" select="document('')/*/page:page"/-->

        <xsl:param name="pageXml">
          <page:page name="about" title="docs"/>
        </xsl:param>

        <xsl:variable name="title" select="(/ui:page/title | exslt:node-set($pageXml)/page:title)[1]"/>
        <xsl:variable name="page" select="(/ui:page/page:page | exslt:node-set($pageXml)/page:page)[1]"/>
        <xsl:variable name="subnav" select="(/ui:page/page:subnav | exslt:node-set($pageXml)/page:subnav)[1]"/>

        <xsl:variable name="page_name" select="/ui:page/@name"/>


        <!-- <Root> -->

        <xsl:template match="/ui:page">
            <xsl:text disable-output-escaping='yes'>&lt;!DOCTYPE html&gt;</xsl:text>
            <html style="background-color:#414247;">
            <head>
                <meta charset="utf-8"/>
                <title><xsl:value-of select="$title"/></title>

                <link href="/favicon.png" rel="shortcut icon" type="image/x-icon"  />
                <link href="/favicon.png" rel="icon" type="image/x-icon" />
                <link href="//fonts.googleapis.com/css?family=Open+Sans:400,300italic,600,700&amp;subset=latin,cyrillic-ext,cyrillic,latin-ext" rel="stylesheet" type="text/css"/>

                <link href="/_css/docs.css" rel="stylesheet" type="text/css" />

                <xsl:for-each select="$page/page:stylesheet">
                    <link rel="stylesheet" type="text/css" media="all" href="{@href}" />
                </xsl:for-each>

                <xsl:apply-templates select="link" mode="html"/>
                <xsl:apply-templates select="style" mode="html"/>
                <xsl:apply-templates select="script" mode="html"/>

            </head>
            <body style="background-color:#414247;">
                <xsl:call-template name="header"/>

                <div class="body">
                    <xsl:call-template name="pagetree"/>
                    <xsl:apply-templates select="ui:content"/>
                    <sub>dev</sub>
                </div>

                <xsl:call-template name="footer"/>
                <a id="feedback" href="http://idea.example.com/forum/"></a>
            </body>
        </html>
        </xsl:template>


    <xsl:template name="header">
    <div class="header"></div>
    </xsl:template>

    <xsl:template name="pagetree">
        <pagetree>
            <xsl:apply-templates select="$subnav/descendant-or-self::page:item[@code=$page_name]/ancestor-or-self::page:item"/>
        </pagetree>
    </xsl:template>

    <xsl:template match="page:item">
        <xsl:element name="a">
            <xsl:if test="@code=$page_name"><xsl:attribute name="class">selected</xsl:attribute></xsl:if>
            <xsl:attribute name="href">
                <xsl:choose>
                    <xsl:when test="@href"><xsl:value-of select="@href"/></xsl:when>
                    <xsl:otherwise><xsl:value-of select="concat($page/@name,'_',@code,'.xml')"/></xsl:otherwise>
                </xsl:choose>
            </xsl:attribute>
            <xsl:value-of select="@title"/>
        </xsl:element>
        <xsl:if test="position()!=last()"> / </xsl:if>
    </xsl:template>

    <xsl:template name="footer">      
        <div class="footer">
            <img src="/_img/powered_by.png"/><br/><br/>
            &#169; 2015 Copyright. 
        </div>
    </xsl:template>

    <xsl:template match="ui:content">
       <xsl:apply-templates mode="html"/>
    </xsl:template>

    <!--xsl:template match="node() | @*" mode="html">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template-->

    <!-- template to copy elements -->
    <xsl:template match="*" mode="html">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="@* | node()" mode="html"/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="br" mode="html">
    <xsl:if test="not(preceding-sibling::node()
                            [not(self::text() and normalize-space(.) = '')][1]
                            [self::br])">
          <xsl:copy>
              <xsl:apply-templates select="@*|node()" />
          </xsl:copy>
       </xsl:if>
    </xsl:template>

    <!-- template to copy attributes -->
    <xsl:template match="@*" mode="html">
        <xsl:attribute name="{local-name()}">
            <xsl:value-of select="."/>
        </xsl:attribute>
    </xsl:template>

    <!-- template to copy the rest of the nodes -->
    <xsl:template match="comment() | text() | processing-instruction()" mode="html">
        <xsl:copy/>
    </xsl:template>


    <xsl:template match="style" mode="html">
       <xsl:element name="style">
            <xsl:value-of select="." disable-output-escaping="yes"/>
       </xsl:element>
    </xsl:template>

    </xsl:stylesheet> 

template_index.xsl:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ui="ui" xmlns:e="entity" xmlns:page="page" xmlns="html" xmlns:exslt="http://exslt.org/common" exclude-result-prefixes="exslt page e ui html">

        <!-- <Includes> -->

        <xsl:import href="template.xsl"/>

        <xsl:param name="pageXml">
            <page:title> API - text</page:title>
            <page:subnav>
                <page:item href="/docs/v1/" code="index" title="docs">
                    <page:item href="/api/v1/" code="track" title="trackI">
                        <page:item href="/api/v1/subscribe" code="subscribe" title="Push Service"></page:item>
                    </page:item>
                </page:item>
            </page:subnav>
        </xsl:param>

        <!-- <Root> -->

        <xsl:template name="sample">

        </xsl:template>

    </xsl:stylesheet>

和index.xhtml:

    <?xml version="1.0" encoding="utf-8"?>
    <?xml-stylesheet type="text/xsl" href="/_templates/template_index.xsl"?>
    <ui:page xmlns:ui="ui" name="index">
        <ui:content>
            <h1>H1</h1>

            Some text. <br/>
            Continue of text:

            <ul>
                <li><a href="/api/v1/">V1</a></li>
            </ul>

            <h2>H2</h2>
            texttext
            <div>
                <info>
                    <h3>H3</h3>
                    <tt>ttt</tt>
                </info>

                <info>
                    <h3>Example</h3>
                    <tt>tt</tt>
                </info>


            </div>


          </ui:content>
    </ui:page>

我在服務器端所做的工作,創建了webapp,將xsl和css文件復制到了webapp中的web-inf文件夾中,將index.xhtml復制到了webapp文件夾中,創建了servlet並這樣寫:

    protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws IOException,
        ServletException {

     response.setContentType("text/html;charset=UTF-8");
     InputStream xslStream = this.getServletContext().getResourceAsStream("template_index.xsl");
     TransformerFactory factory = TransformerFactory.newInstance();
     Source xslSource = new StreamSource(xslStream);
     try {
     Transformer transformer = factory.newTransformer(xslSource);
     InputStream xmlStream = this.getServletContext().getResourceAsStream("index.xhtml");
     Source xmlSource = new StreamSource(xmlStream);
     Result output = new StreamResult(response.getOutputStream());
     transformer.transform(xmlSource, output);
     } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    } 

更改了web.xml:

<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>

<display-name>root</display-name>
<description>root</description>
<servlet>
    <servlet-name>Servlet</servlet-name>
    <servlet-class>package.Servlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>RootServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

從所有這些操作中,當我嘗試使用localhost:8080 / exampleapp /時,它為空。

在瀏覽器中打開XML時,它可以工作。 這是因為,瀏覽器將XML內容轉換為HTML。

在Java中執行相同的操作時,需要在XML文件中刪除XSLT定義。 您已經同時包含XSL和XML,並且轉換是使用Java啟動的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM