15

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.

1
  • Good Question (+1). See my answer, explaining why this isn't so important, how this is done in the spirit of XSLT in XSLT 2.0 and why using DOE (Disable Output Escaping) is not recommended. Commented May 18, 2010 at 1:16

3 Answers 3

20

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

<xsl:value-of select="@Description" disable-output-escaping="yes"/>
Sign up to request clarification or add additional context in comments.

3 Comments

Aha, thanks! I was doing things like finding a string-replace template and calling it over and over, once for every html entity.
This disable-output-escaping just does not work for me. See my question
What if I want to apply it to an attribute, like href="{a/href} ?
3

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.

2 Comments

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: The entity is only in the XML (xhtml) document. The parser always replaces it with &#xA0; 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 &#xA0; with &nbsp; (with whatever is specified for this character map). Character maps are used to serialize the whole output.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.