This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Embedding HTML in XML


Madhu,

>But instead of getting an image in the HTML page, I get the exact CDATA 
>text written as is to the browser. Is there some way I can embed the HTML 
>in the XML markup so that it's processed as HTML itself?

CDATA sections are designed to take the trauma out of escaping text that
has lots of characters that need escaping in (like < and &).  Putting
<![CDATA[...]]> round a section essentially says "do not parse this section
- everything in it should be interpreted as characters".

So, in your example:

<![CDATA[
  <img src="global.gif" alt="Go around the world" />
]]>

is exactly the same as:

  &lt;img src="global.gif" alt="Go around the world" />

The XML parser sees a string, not a tag.  The XSLT processor therefore sees
a string, not a tag, and processes it as a string, not a tag, which means
that it outputs:

  &lt;img src="global.gif" alt="Go around the world" />

which gets displayed in your browser.

The goal you're aiming for is copying something that you have in your XML
source directly into your HTML output.  The xsl:copy-of element is designed
precisely for this purpose.  xsl:copy-of will give an exact copy of
whatever you select, including attributes and content.

So, you can just have XML like:

<page>
  We offer the cheapest air fares to Bombay.
  <img src="global.gif" alt="Go around the world" />
</page>

And then a template that says 'when you come across an img element, just
copy it':

<xsl:template match="img">
  <xsl:copy-of select="." />
</xsl:template>

If you have lots of HTML that you want to copy straight over from your XML
source to your HTML output, the cleanest approach is to define an 'html'
namespace and mark all the HTML elements as belonging to this namespace:

<page xmlns:html="http://www.w3.org/1999/xhtml">
  We offer the cheapest air fares to Bombay.
  <html:img src="global.gif" alt="Go around the world" />
</page>

To copy these HTML elements whenever you come across them within your page,
have a template that matches them (html:* - any element in the html
namespace) and copies them:

<xsl:template match="html:*">
  <xsl:copy-of select="." />
</xsl:template>

I hope this helps,

Jeni

Dr Jeni Tennison
Epistemics Ltd * Strelley Hall * Nottingham * NG8 6PE
tel: 0115 906 1301 * fax: 0115 906 1304 * email: jeni.tennison@epistemics.co.uk


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]