我试图将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