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