xsl-list
[Top] [All Lists]

Re: Need help rendering the HTML residing within the XML

2004-08-12 01:08:19


First timer problem (couldn't find an exact answer in the archives): I 
have an XML file with a hundred plus nodes of which any number could at 
any time contain HTML tags such as the <b></b> tag in line 2.

The easiest way if you want to be able to use html anywhere is to define a html
namespace so

1 <book>
2 <name><b>Count of Monte Cristo</b></name>
3 <author>Alexander Dumas</author>
4 <content />
5 </book>

becomes 
1 <book xmlns:h="http://www.w3.org/1999/xhtml";>
<!-- I'm referring to the xhtml namespace although that is not necessarily what
we will put out or accept as input -->
2 <name><h:b>Count of Monte Cristo</h:b></name>
3 <author>Alexander Dumas</author>
4 <content />
5 </book>
then have in your xslt the following template

<xsl:template match="h:*">
<xsl:element name="{local-name()}"><xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>

I prefer that template to just doing a straight copy of because in that way you
can break out of copying at some lower level of a tree if necessary. 


the proper formatting of the b tags can then be controlled via a css. 


otherwise you can just have the following template for name

<xsl:template match="name">
<xsl:copy-of select="*"/>
</xsl:template>
that means of course that everything within name will have to be within a html
tag of some sort.