Saxon抛出了XSLT的“顶级文本节点”异常,但XML似乎很适合我

时间:2013-10-16 04:20:05

标签: xml tomcat xslt saxon apache-cocoon

我在Mac OSX上使用Apache Tomcat 7.0.42运行Cocoon。我不断从萨克森那里得到一个非常奇怪的错误。它说我的XML中有一个顶级文本节点。首先,我不确定它们是否意味着我的输出xml或我的xsl xml。其次,我认为它们都没有顶级文本节点。

以下是来自堆栈跟踪的错误消息:

  

net.sf.saxon.trans.XPathException:当指定'standalone'或'doctype-system'时,文档必须格式正确;但是这个文档包含一个顶级文本节点       net.sf.saxon.serialize.XMLEmitter.characters(XMLEmitter.java:574)       net.sf.saxon.event.ReceivingContentHandler.flush(ReceivingContentHandler.java:543)

< ...长堆栈跟踪...>

  

org.apache.cocoon.servletservice.DispatcherServlet.service(DispatcherServlet.java:106)       javax.servlet.http.HttpServlet.service(HttpServlet.java:728)       org.apache.cocoon.servlet.multipart.MultipartFilter.doFilter(MultipartFilter.java:131)

这是我的departments_list.xsl,它导入common.xsl:

<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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


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

    <xsl:template match="/">
        <html lang="en">
            <body>
                <xsl:call-template name="header" />
                <xsl:call-template name="footer" />
            </body>
        </html>
    </xsl:template>

</xsl:stylesheet>

这是我的common.xsl:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.w3.org/1999/xhtml">

<xsl:template name="header">
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <head>
        <title> 
        <!-- static for now. Can use parameters though. -->
    Harvard University FAS Course Catalog
        </title>
        <xsl:call-template name="css" />
        <xsl:call-template name="js" />
    </head>
</xsl:template>

<xsl:template name="footer">
    <div id="footer">
        <hr />
        <ul>
            <li>Web Development with XML</li>
            <li>Bret Fontecchio</li>
            <li>Fall 2013</li>
        </ul>
    </div>
</xsl:template>

<xsl:template name="css">
    <style type="text/css">
        <!-- CSS Rules -->
    </style>
</xsl:template>

<xsl:template name="js">
    <!-- use this template to add js
    <script type="text/javascript" src="foobar.js"></script>
-->
</xsl:template>

</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

我无法重现这个问题,而且我很确定你的解释(把头放在体内)是无关紧要的。

这是序列化错误;这意味着当要求doctype-system或standalone作为序列化选项时,输出必须是格式良好的文档;它不能在任何元素之外有顶级文本。

首先,除非指定了一个序列化参数“standalone”或“doctype-system”,否则我不相信会发生错误。这些可能是从调用应用程序而不是样式表代码设置的。

其次,我很确定只有在没有打开元素的情况下将文本写入输出文件时才会出现错误。

所以有一些事情没有告诉我们。