tags:

views:

874

answers:

6

On my web site I have XMLs with my page contents (automatically generated from my DB) - which are displayed using XSLT. The problem is this: I'd like to have some formatting within some of the XML tags. For instance, if I have an XML containing an article in a format like this:

<article>
  <header>Cool article</header>
  <author>Me!</author>
  <content>
    This is an article. It's <b>HUGE</b>, and here's a <a href="http://Www.foo.com"&gt;link&lt;/a&gt;.
  </content>
</article>

However, if I simply get the contents using this: <xsl:value-of select="content" /> all the HTML formatting is ignored/lost. I guess it's mistaken for XML child nodes, and not actual data residing in the content node.

What's the preferred way of achieving formatting like what described here?

Thanks in advance.

A: 

Either encode your html when you embed it plainly in an elemnet, or use a CDATA block to preserve actual text. Either should work although depending on where the Transform takes place (like browser level via JS) the output may be different with encoded entities.

Quintin Robinson
Hi, this unfortunately makes the tags appear in plain text - that is, no formatting is applied. I fixed it using <xsl:output method="html" ... />. Thanks anyway
Hallgeir
+1  A: 
<xsl:value-of select="content" />

outputs the value of a node. And the value of your <content> node actually is:

This is an article. It's HUGE, and here's a link

What you probably need is to copy the entire node:

<xsl:copy-of select="content" />

This is largely a guess since I don't know how your system works.

Tomalak
Cheers, this was partly the problem. :) Using copy-of instead of value-of in combination with <xsl:output method="html" ... /> fixed it.
Hallgeir
This looks correct to me too, but it depends on having XHTML embedded in the XML, not just HTML. Secondly, copy-of is going to output the contents in the namespace of the original source XML document, not the namespace that the XSL is otherwise outputting to. Most web browsers probably won't really care, but if you see a bunch of xmlns="..." attributes in your output, this is why.
Chris Nielsen
A: 

You could change the generation of that XML file to put the content in a <![CDATA[ ]]> section, which tells the parser to ignore special content within that section.

Craig Martek
Hi, this unfortunately makes the tags appear in plain text - that is, no formatting is applied. I fixed it using <xsl:output method="html" ... />. Thanks anyway
Hallgeir
A: 

When putting the HTML in to the XML document, make sure that any HTML code is encoded before putting it into the XML. For example.

This is an article. It's <b>HUGE</b>, and here's a <a href="http://Www.foo.com"&gt;link&lt;/a&gt;.

Would become:

This is an article. It's &lt;b&gt;HUGE&lt;/b&gt;, and here's a &lt;a href="http://www.foo.com"&amp;gt;link&amp;lt;/a&amp;gt;

or use a CDATA block so that the HTML formatting isn't lost.

Justin Niessner
Hi, this unfortunately makes the tags appear in plain text - that is, no formatting is applied. I fixed it using <xsl:output method="html" ... />. Thanks anyway
Hallgeir
+1  A: 

I think your problem is this:

 <xsl:output method="xml" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
          media-type="application/html+xml" encoding="utf-8" omit-xml-declaration="yes" indent="no"/>

make sure your output is of type html,

application/html
Andrew
Cheers! Setting method="html" fixed it up nicely. :)
Hallgeir
I am glad I could help you :).
Andrew
+1  A: 
<xsl:value-of
select="..."
disable-output-escaping="yes"/>

This works on all browsers except Firefox.

ZippyV