我使用XSL模板作爲Web框架的網頁模板,最終輸出爲XHTML 1.0 Strict;它需要XML輸入和輸出XHTML。除了一個問題之外,它完美地工作 - 最終輸出也輸出XML節點而不僅僅是內容。這是基本的XML(遺漏某些項目,但整體設計是相同的):擺脫XSL模板輸出中的XML根節點
<Page>
<PageScript>
<Script>./js/myscript.js</Script>
</PageScript>
<PageCSS>
<CSS>./css/mycss.css</CSS>
</PageCSS>
<PageContent>Blah blah blah</PageContent>
</Page>
這裏是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中。我需要知道如何擺脫它,所以我可以擁有一個有效的XHTML 1.0 Strict文檔。
奇怪的是,PageContent節點是這樣做的唯一節點,沒有其他節點使用輸出中的節點名稱獲取它的內容。這種行爲的原因是什麼?
謝謝大家,這些都非常有幫助。我還有一個其他問題。它們擺脫了父節點的問題,但PageContent內部的第一個節點獲得了`xlmns =「」`屬性,我很好奇它是否可以以任何方式留下。 如果這有幫助,PageContent節點將保存xhtml - 它基本上是xhtml中頁面的主要部分 - 運行用戶請求的任何進程的結果。 – 2009-06-23 17:27:36