tags:

views:

102

answers:

3

I'm completely new to using XSL, so if there's any information that I'm neglecting to include, just let me know.

I have a string in my XSLT file that I can display like this:

<xsl:value-of select="@Description/>

and it shows up, rendered in a browser like:

<div>I can't do anything about the html entities existing in the text.</div> <div>This includes quotes, like  &quot;Hello World&quot; and sometimes whitespaces.&nbsp;</div>

What can I do to get this string rendered as html, so that <div></div> results in newlines, &quot; gives me ", and &nbsp; gives me a space?

I could elaborate on things I've already tried that haven't worked, but I don't know if that's relevant.

A: 

I think you want to set the following attribute as so:

<xsl:value-of select="@Description" disable-output-escaping="yes"/>
Ben.Vineyard
Aha, thanks! I was doing things like finding a `string-replace` template and calling it over and over, once for every html entity.
Kache4
+1  A: 

Why do you need to have entities output? To the browser &nbsp; is the same as &#xA0; -- in both cases it will display a non-breaking space.

There is a feature in XSLT 2.0 called character-maps, that provide this functionality, if really needed. It is an XSLT best practice to try not to use DOE, unless absolutely necessary.

Also, DOE is not a mandatory feature of XSLT and some XSLT processors may not implement it. This means that an XSLT application that uses DOE is generally not portable across different XSLT processors.

Dimitre Novatchev
Do character-maps get applied throughout the whole document? The example from the page showed that it wasn't wrapped around some sort of element.Also, in my case, the string I get comes with the html entities (I've no control of that). Char-maps can still be used?
Kache4
@Kache4: The entity is only in the XML (xhtml) document. The parser always replaces it with ` ` and this is what the XSLT processor sees. Character maps don't know anything about the XML document -- they are used when the final output is done (serialized). They simply can replace every occurence of ` ` with ` ` (with whatever is specified for this character map). Character maps are used to serialize the whole output.
Dimitre Novatchev
A: 

The reason divs in HTML get an endline is completely different and related to the CSS boxmodel. Most browsers apply the style:

div {display:block;}

In lieu of the standard display:inline;. However, they only do that to divs in the XHTML namespace. You need to output divs to the XHTML namespace to faciliate that. Bind the XHTML namespace to the prefix xhtml at the top of your document like so:

<xsl:stylesheet xmnls:xhtml="http://www.w3.org/1999/xhtml" ... >

And then output the divs as <xhtml:div> ... </xhtml:div> most browsers would recognise the div to be in the XHTML namespace (http://www.w3.org/1999/xhtml) and apply the block style.

Lajla