2012-05-03 43 views
1

我想知道有沒有人可以幫我解決我遇到的棘手問題。大概在這裏問不可能,但會有興趣聽到任何建議。XSLT:通過XML節點中的代碼片段構建HTML文件

我有一個XML文件,其中一個節點包含頁面的HTML。我想要獲取該節點中的數據,以及如何從中創建HTML文件。因此,從下面的示例XML中,我想要轉換並從//dvm/rows/row中挑選cell[2]節點,並將其放在另一個文件中,以便我可以真正看到該HTML標記顯示的內容,並將其顯示爲適當的網頁而不是代碼。

如果我把它和XSL這樣,它會工作嗎?

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output indent="yes" method="html"/> 
<xsl:template match="/"> 
    <xsl:value-of select="//dvm/rows/row/cell[2]" disable-output-escaping="yes"/> 
</xsl:template> 
</xsl:stylesheet> 

的XML是:

<dvm> 
    <description>This is a DVM file</description> 
    <columns> 
    <column name="lang"/> 
    <column name="text"/> 
    <column name="code" qualifier="true" order="1"/> 
    <column name="type" qualifier="true" order="2"/> 
    <column name="subj"/> 
    <column name="urem"/> 
    <column name="vreq"/> 
</columns> 
<rows> 
    <row> 
    <cell>English(UK)</cell> 
    <cell><![CDATA[<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
      <html xmlns="http://www.w3.org/1999/xhtml"> 
      <head> 
       <title>My HTML File</title> 
      </head> 
      <body> 
      <div class="text"> 
      <p>This is a line of text</p> 
      <p>This is a line of text</p> 
      <p>This is a line of text</p> 
      </div> 
      </body> 
      </html>]]> 
    </cell> 
    <cell>Code 1</cell> 
    <cell>Text</cell> 
    <cell>HTML Block 1</cell> 
    <cell/> 
    <cell/> 
    </row> 
</rows> 
</dvm> 

回答

2

您必須擺脫CDATA部分並使用它包含的文本作爲常規標記。的DOCTYPE也應除去(將通過變換而產生):

<?xml-stylesheet type="text/xsl" href="delete_3.xsl" ?> 
<dvm> 
    <description>This is a DVM file</description> 
    <columns> 
     <column name="lang"/> 
     <column name="text"/> 
     <column name="code" qualifier="true" order="1"/> 
     <column name="type" qualifier="true" order="2"/> 
     <column name="subj"/> 
     <column name="urem"/> 
     <column name="vreq"/> 
    </columns> 
    <rows> 
     <row> 
      <cell>English(UK)</cell> 
      <cell> 
       <html xmlns="http://www.w3.org/1999/xhtml"> 
       <head> 
        <title>My HTML File</title> 
       </head> 
       <body> 
       <div class="text"> 
       <p>This is a line of text</p> 
       <p>This is a line of text</p> 
       <p>This is a line of text</p> 
       </div> 
       </body> 
       </html> 
     </cell> 
      <cell>Code 1</cell> 
      <cell>Text</cell> 
      <cell>HTML Block 1</cell> 
      <cell/> 
      <cell/> 
     </row> 
    </rows> 
</dvm> 

然後產生包含在第二cell元件的XHTML的變換是簡單

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" 
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="/"> 
    <xsl:copy-of select="/*/rows/*/cell[2]/node()"/> 
</xsl:template> 
</xsl:stylesheet> 

當現在我用IE打開文件:c:\temp\delete\delete2.xml,結果是

<!DOCTYPE html 
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <title>My HTML File</title> 
    </head> 
    <body> 
     <div class="text"> 
     <p>This is a line of text</p> 
     <p>This is a line of text</p> 
     <p>This is a line of text</p> 
     </div> 
    </body> 
</html> 

,這是由瀏覽器顯示爲:

我的HTML文件

這是文本

的線這是一行文字

這是一行文字

0
<xsl:value-of select="cell[2]" disable-output-escaping="yes" /> 

注意,因爲你的<cell>包含XHTML,有一個CDATA部分沒有真正的需要。 XHTML和XML是兼容的,您可以將其中一個傳輸到另一箇中,而不必先將其轉換爲字符串。

+0

謝謝!我已經對我的問題做了一些修改,因爲我嘗試將你的建議放到我的XSL模板中,但它只是以文本形式顯示了代碼塊(正如我正在做'查看源代碼'一樣)。我是否正確地稱呼它? – JaXL

+0

因此,當你查看源代碼時,你*看到了HTML代碼?我很困惑。畢竟,這是預期的結果。沒有? – Tomalak

+0

對不起 - 我最近的評論有點糟糕。當我在瀏覽器中嘗試查看XSL轉換時,它以文本形式顯示代碼塊(我的意思是說,如果您正在執行「查看源代碼」,實際上並沒有執行此操作)。我打算做的是如何看到我挑選出的HTML的渲染結果。即該代碼段的網頁。希望更有意義! – JaXL