使用xsl-java将xhtml转换为html

使用xsl-java将xhtml转换为html,java,html,xslt,Java,Html,Xslt,我有template.xsl和template_index.xsl模板,用于从xhtml文件动态创建html。我可以从任何文件夹本地打开xhtml文件并查看xsl创建的内容。所以现在我需要从服务器上运行它,并通过寻址到服务器的链接来查看内容。但当我想查看服务器上的内容时,它是空的。我做错了什么 template.xsl: <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http:

我有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> 
已更改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>

index.xhtml
根
根
Servlet
包.Servlet
RootServlet
/*

从我尝试使用localhost:8080/exampleapp/时的所有操作来看,它是空的。

当您在浏览器中打开XML时,它可以工作。这是因为浏览器将XML内容转换为HTML


在Java中执行相同操作时,需要删除XML文件中的XSLT定义。因为您已经包含了XSL和XML,并且转换是用Java启动的。

我想,您不理解这个任务。当我从localhost:8080/exampleapp这样的已部署应用程序转到xhtml时,我需要查看站点内容。当我去当地的时候file://example/index.xhtml 没关系,我可以查看所有内容,但当我转到已部署的应用程序时,它就是empty@bakash_erni,您是否在服务器中收到任何错误?什么时候在servlet中调用doPost方法?如果您正在用java转换xhtml,为什么要在欢迎文件列表中提到它?您的servlet名称元素不匹配。一个包含
Servlet
,而另一个包含
RootServlet
    <?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>
    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();
    }
    } 
<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>