Hari's Corner

Humour, comics, tech, law, software, reviews, essays, articles and HOWTOs intermingled with random philosophy now and then

HTML inside XML - rendering with XSLT issue

Filed under: Tutorials and HOWTOs by Hari
Posted on Fri, Jul 6, 2007 at 20:09 IST (last updated: Wed, Jul 16, 2008 @ 20:39 IST)

I've been meddling a bit with XML and XSLT, mostly out of curiosity, but also because I want to try and convert my reviews site completely to entirely to XML. I seem to have a strange fascination for XML because the possibilities seem limitless. Anyway, I digress. This topic is really to document a little discovery I made.

When you embed HTML inside XML document elements like this (an example). Note the HTML tags used inside <content> which is used as formatting tags and not part of the XML.
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="index.xsl" ?>

<review> <title>A sample review</title> <author>A sample author</author> <content> <p>This is the actual review content</p> <p>Hello World! How are you today?</p> </content> </review>

Here's the XSLT file index.xsl I wrote to render the above XML file
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
    <h1>Hari's Reviews</h1>
    <hr />
    <h2><xsl:value-of select="review/title"/> by
<xsl:value-of select="review/author"/></h2>

<xsl:value-of select="review/content"/> </body> </html> </xsl:template> </xsl:stylesheet>

The above code will not render correctly because the <xsl:value-of select="review/content"/> tag will strip the HTML tags (as they're considered to be actually XML tags) while returning the data.

The correct code will be: <xsl:copy-of select="review/content"/>

Update: The smart quotes problem in the code has been fixed.

No comments yet

There are no comments for this article yet.

Comments closed

The blog owner has closed further commenting on this entry.