2009-06-22 27 views
2

我使用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節點是這樣做的唯一節點,沒有其他節點使用輸出中的節點名稱獲取它的內容。這種行爲的原因是什麼?

回答

2

我會嘗試更換<xsl:apply-templates select="//PageContent" />

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

您的PageContent節點上調用apply-templates

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

匹配,這是這​​一個,所以PageContent節點被複制的唯一模板:

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

也許你應該加入一個匹配PageContent另一個模板,只是處理的子節點,而不復制PageContent本身:

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

謝謝大家,這些都非常有幫助。我還有一個其他問題。它們擺脫了父節點的問題,但PageContent內部的第一個節點獲得了`xlmns =「」`屬性,我很好奇它是否可以以任何方式留下。 如果這有幫助,PageContent節點將保存xhtml - 它基本上是xhtml中頁面的主要部分 - 運行用戶請求的任何進程的結果。 – 2009-06-23 17:27:36

2

前面已經指出的@sth和@molf,你得到的節點通過你的身份模板複製(<xsl:template match="@*|node()"> ),因爲樣式表中沒有其他模板匹配<PageContent>節點。

作爲替代已經被其他兩個提出的解決方案,您可以在身份模板更改爲:

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

現在,當遇到<PageContent>,身份模板不匹配了。爲元素會被替代的隱含的默認模板,它是:

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

這意味着模板將被自動地應用到<PageContent>的孩子,和節點本身不會被複制 - 正是你想要的。