2011-02-15 83 views
1

我試圖將XML文檔轉換成HTML。這個區域很簡單,所有包括孩子節點的節點都將按順序顯示出來。少數節點包含html標籤,我想保留它們,以便格式化可以在html中工作。轉換XML到HTML

所以我在XML文件:

 <?xml version="1.0" encoding="UTF-8"?> 
      <para class="para"> 
       <table style="1"> 
        <col width="50*"/> 
        <col align="right" width="25*"/> 
        <col align="right" width="25*"/> 
        <thead> 
        <tr> 
         <th> 
         <text class="text">xyz</text> 
         </th> 
         <th> 
         <text class="text">Abc</text> 
         </th> 
        </tr> 
        </thead> 
        <tr> 
        <td> 
         <text class="text">2,000 Common</text> 
        </td> 
        <td> 
         <text class="text">($200.00)</text> 
        </td> 
        </tr> 
       </table> 
       </para> 
      <para class="para"> 
       <div>Some Text 
        <product><b>this should be in bold</b></product> 
       </div> 
      </para> 

我寫的XSL腳本:這裏

<xsl:template name="para" > 
<xsl:for-each select="child::text()|child::node()" > 
    <xsl:if test ="node()"> 
    <xsl:if test="text()"> 
       <xsl:value-of select="text()"/> 
      <br/> 
      <br/> 
     </xsl:if> 
    <xsl:call-template name="para"></xsl:call-template> 
    </xsl:if> 
</xsl:for-each> 

問題是,它也在考慮HTML標籤作爲節點,並將其渲染thoes標籤中的值。在哪裏,我想保留這些標籤在HTML輸出。父節點「para」可以有多個子節點和子節點,因此不需要通用解決方案。輸出應該

 <table> 
     <tr> 
     <td> 
     xyz 
     </td> 
     <td> 
     abc 
     </td> 
     </tr> 
     <tr> 
     <td> 
     2,000 Common  
     </td> 
     <td> 
     ($200.00) 
     </td> 
     </tr> 
     </table> 

     Some Text 

     **this should be in bold** 

感謝

+0

問得好,+1。請參閱我的答案,以獲得一個完整,簡短且簡單的解決方案,該解決方案使用最基本且功能強大的XSLT設計模式之一:重寫身份模板。 :) – 2011-02-15 18:10:02

回答

2

這種變換:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

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

<xsl:template match="t|para|col|text|product"> 
    <xsl:apply-templates/> 
</xsl:template> 
</xsl:stylesheet> 

當施加上所提供的XML文檔(包裹在頂部元件,使其充分形成的):

<t> 
    <para class="para"> 
     <table style="1"> 
      <col width="50*"/> 
      <col align="right" width="25*"/> 
      <col align="right" width="25*"/> 
      <thead> 
       <tr> 
        <th> 
         <text class="text">xyz</text> 
        </th> 
        <th> 
         <text class="text">Abc</text> 
        </th> 
       </tr> 
      </thead> 
      <tr> 
       <td> 
        <text class="text">2,000 Common</text> 
       </td> 
       <td> 
        <text class="text">($200.00)</text> 
       </td> 
      </tr> 
     </table> 
    </para> 
    <para class="para"> 
     <div>Some Text      
      <product> 
       <b>this should be in bold</b> 
      </product> 
     </div> 
    </para> 
</t> 

產生想,正確的結果:

<table style="1"> 
    <thead> 
     <tr> 
      <th>xyz</th> 
      <th>Abc</th> 
     </tr> 
    </thead> 
    <tr> 
     <td>2,000 Common</td> 
     <td>($200.00)</td> 
    </tr> 
</table> 
<div>Some Text      
      <b>this should be in bold</b> 
</div> 

說明:這是「壓倒一切的身份規則」的設計模式的簡單應用。

+0

感謝Dimitre,我從你的回答中得到了一個提示,問題解決了。 – atif 2011-02-15 19:37:57

1

如果你想傳遞通過HTML的是,那麼你應該考慮把他們在一個單獨的命名空間從XML的其餘部分。 W3C甚至爲各種版本的(X)HTML定義了一堆名稱空間,您可以使用它。

0

您可以使用命名空間 - 創建一個命名空間,把你的所有非HTML節點到這個命名空間。

比XSLT選擇一個給定的命名空間僅節點 - 這將讓你的HTML標記文本處理。