java - 使用 xsl java 将 xhtml 转换为 html

标签 java html xslt

我有 template.xsl 和 template_index.xsl 模板,用于从 xhtml 文件动态创建 html。在本地,我可以从任何文件夹打开该 xhtml 文件并查看由 xsl 创建的内容。所以现在我需要从服务器运行它,并通过寻址到服务器的链接来查看内容。但是当我想从服务器查看内容时它是空的。我做错了什么?

模板.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> 

模板索引.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 中启动的。

关于java - 使用 xsl java 将 xhtml 转换为 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30972984/

相关文章:

xslt插入cdata

javascript - 在 Canvas 中使用 arc 方法的 startAngle 以外的值

Asp.net 网页在查看页面源代码中以 xml 格式可见,但在浏览器上看起来用户友好?

java - 从 Android Studio Java 中的文本文件读取

java - 获取/订阅 stasis 消息( Asterisk ARI)

javascript - 使用 BOXOVER.js 工具提示时 IE 中的选择框问题

javascript - 在 ruby​​ on rails 中提交 "submit"之前执行 js 代码

xml - XSL+XPATH : Compare previous node attribute to current node attribute

java - 在 switch 中使用关系运算符

java - Left JOIN issuesdevices AS sm ON sd.DeviceID = sm.DeviceID' 在第 1 行