converting xml to html
I am trying to convert an xml document into html. The locgic is simple that all the nodes including childnode will be displayed in order. Few of the nodes contains the html tags and I want to keep them as it is so that formating will work in html.
so I have the xml file:
<?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>
I have write the xsl script:
<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>
Problem here is that it is also considering html tags as nodes and it rendering on the values inside thoes tags. Where as i want to preserve these tags in html output. The parent node "para" can have numbers of child nodes and sub nodes so can't need a generic solution. The output should be
<table>
<tr>
<td>
xyz
</td>
<td>
abc
</td>
</tr>
<tr>
<td>
2,000 Common
</td>
<td>
($200.00)
</td>
</tr>
</table>
Some Text
**th开发者_Python百科is should be in bold**
Thanks
This transformation:
<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>
when applied on the provided XML document (wrapped in a top-element to make it well-formed):
<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>
produces the wanted, correct result:
<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>
Explanation: This is a simple application of the "overriding the identity rule" design pattern.
If you want the HTML to be passed through as is then you should consider putting them in a separate namespace from the rest of the XML. The W3C even defines a bunch of namespaces for the various versions of (X)HTML that you can use for this.
You can use namespaces - create a namespace and put all your non-html nodes into this namespace.
Than in xslt select only nodes from a given namespace - this will let your html tags be treated as text.
精彩评论