去掉XSL模板输出中的XML根节点

去掉XSL模板输出中的XML根节点,xml,xslt,xhtml,Xml,Xslt,Xhtml,我使用XSL模板作为web框架的网页模板,最终输出为XHTML1.0;它接受XML输入并输出XHTML。除了一个问题外,它工作得非常好——最终输出也输出一个XML节点,而不仅仅是内容。以下是基本的XML(缺少一些项目,但总体设计是相同的): /js/myscript.js ./css/mycss.css 废话废话 这是XSL模板 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transfo

我使用XSL模板作为web框架的网页模板,最终输出为XHTML1.0;它接受XML输入并输出XHTML。除了一个问题外,它工作得非常好——最终输出也输出一个XML节点,而不仅仅是内容。以下是基本的XML(缺少一些项目,但总体设计是相同的):


/js/myscript.js
./css/mycss.css
废话废话
这是XSL模板

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:response="http://www.ntforl.com/"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:xhtml="http://www.w3.org/1999/xhtml"
    xmlns:lang="en">

    <xsl:output
        method="xml"
        doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
        doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
    />

    <xsl:template match="//PageCSS">
        <xsl:for-each select="CSS">
            <link type="text/css" rel="stylesheet">
                <xsl:attribute name="href">
                    <xsl:value-of select="." />
                </xsl:attribute>
            </link>
        </xsl:for-each>
    </xsl:template>

    <xsl:template match="//PageScript">
        <xsl:for-each select="Script">
            <script type="text/javascript">
                <xsl:attribute name="src">
                    <xsl:value-of select="." />
                </xsl:attribute>
            </script>
        </xsl:for-each>
    </xsl:template>

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

    <xsl:template match="/" disable-output-escaping="yes">
        <html>
            <head>
                <title>
                    <xsl:value-of select="//PageTitle" />
                </title>
                <meta http-equiv="content-type" content="text/html;charset=utf-8" />
                <link type="text/css" rel="stylesheet" href="template/style/globalStyle.css" />
                <link type="text/css" rel="stylesheet" href="template/style/standards.css" />
                <xsl:apply-templates select="//PageCSS" />
                <script type="text/javascript" src="template/js/some_file.js"></script>
                <xsl:apply-templates select="//PageScript" />
            </head>
            <body onload="">
                <div id="container">
                    <div id="top">
                        <div class="clear" />
                    </div>
                    <div id="main">
                        <div class="left" style="width: 708px; margin-top: 10px;">
                            <h1 class="center">
                                <xsl:value-of select="//PageTitle" />
                            </h1>
                        </div>
                        <div class="clear" />
                        <div id="rightPane">
                            <div id="rightPaneContent">
                                <xsl:apply-templates select="//PageContent" />
                            </div>
                            <img src="template/images/mainBgBottom.png" alt="" />
                        </div>
                    </div>
                </div>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

问题存在于PageContent中,并且只发生在PageContent中。当转换运行时,模板输出

<PageContent xmlns=""> node content </PageContent>
节点内容
在XHTML中。我需要知道如何摆脱它,这样我才能拥有一个有效的XHTML1.0严格文档


奇怪的是,PageContent节点是唯一执行此操作的节点,没有其他节点在输出中使用节点名称对其进行内容包装。这种行为的原因是什么?

我会尝试将
替换为:

<xsl:apply-templates select="//PageContent/node()" />

页面内容
节点上调用
应用模板

<xsl:apply-templates select="//PageContent" />
您可能应该添加另一个与
PageContent
匹配的模板,只处理子节点,而不复制
PageContent
本身:

<xsl:template match="//PageContent">
  <xsl:apply-templates/>
</xsl:template>

正如@sth和@molf已经指出的那样,您的标识模板复制了节点(
),因为样式表中没有其他模板与
节点匹配

作为其他两个解决方案的替代方案,您可以将标识模板更改为:

<xsl:template match="@*|node()[not(self::PageContent)]">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()" />
  </xsl:copy>
</xsl:template>

现在遇到
时,标识模板不再匹配。将改用元素的隐式默认模板,即:

<xsl:template match="*">
  <xsl:apply-templates />
</xsl:template>


这意味着模板将自动应用于
的子节点,而节点本身不会被复制-这正是您想要的。

谢谢大家,这些都非常有用。不过我还有一个问题。这些解决了父节点问题,但PageContent中的第一个节点获得了一个
xlmns=“”
属性,我很好奇这是否可以以任何方式保留。如果这有帮助的话,PageContent节点保存xhtml——它基本上是xhtml中页面的主要部分——运行用户请求的任何进程的结果。
<xsl:template match="@*|node()[not(self::PageContent)]">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()" />
  </xsl:copy>
</xsl:template>
<xsl:template match="*">
  <xsl:apply-templates />
</xsl:template>