69

What is the best way to include an html entity in XSLT?

<xsl:template match="/a/node">
    <xsl:value-of select="."/>
    <xsl:text>&nbsp;</xsl:text>
</xsl:template>

this one returns a XsltParseError

11 Answers 11

129

You can use CDATA section

<xsl:text disable-output-escaping="yes"><![CDATA[&nbsp;]]></xsl:text>

or you can describe &nbsp in local DTD:

<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#160;"> ]>

or just use &#160; instead of &nbsp;

Sign up to request clarification or add additional context in comments.

1 Comment

"or just use &#160; instead of &nbsp;" ❤️
26

It is also possible to extend the approach from 2nd part of aku's answer and get all known character references available, like this:

<!DOCTYPE stylesheet [
  <!ENTITY % w3centities-f PUBLIC "-//W3C//ENTITIES Combined Set//EN//XML"
      "http://www.w3.org/2003/entities/2007/w3centities-f.ent">
  %w3centities-f;
]>
...
<xsl:text>&nbsp;&minus;30&deg;</xsl:text>

There is certain difference in the result as compared to <xsl:text disable-output-escaping="yes"> approach. The latter one is going to produce string literals like &nbsp; for all kinds of output, even for <xsl:output method="text">, and this may happen to be different from what you might wish... On the contrary, getting entities defined for XSLT template via <!DOCTYPE ... <!ENTITY ... will always produce output consistent with your xsl:output settings.

It may be wise then to use a local entity resolver to keep the XSLT engine from fetching character entity definitions from the Internet. JAXP or explicit Xalan-J users may need a patch for Xalan-J to use the resolver correctly. See my blog XSLT, entities, Java, Xalan... for patch download and comments.

1 Comment

This is beautiful- wish it was more well known
15

one other possibility to use html entities from within xslt is the following one:

<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>

1 Comment

The unicode character &#160 was giving question marks and boxes, outputting the un-escaped html entity worked for me, thanks.
8

XSLT only handles the five basic entities by default: lt, gt, apos, quot, and amp. All others need to be defined as @Aku mentions.

Comments

6

this one returns a XsltParseError

Yes, and the reason for that is that &nbsp; is not a predefined entity in XML or XSLT as it is in HTML.

You could just use the unicode character which &nbsp; stands for: &#160;

Comments

5

Now that there's Unicode, it's generally counter-productive to use named character entities. I would recommend using the Unicode character for a non-breaking space instead of an entity, just for that reason. Alternatively, you could use the entity &#160;, instead of the named entity. Using named entities makes your XML dependent on an inline or external DTD.

Comments

1

I found all of these solutions produced a  character in the blank space.

Using <xsl:text> </xsl:text> solved the problem for me; but <xsl:text>#x20;</xsl:text> might work as well.

Comments

0

Thank you for your information. I have written a short blog post based on what worked for me as I was doing XSLT transformation in a template of the Dynamicweb CMS.

The blog post is here: How to add entities to XSLT templates.

/Sten Hougaard

Comments

0

It is necessary to use the entity #x160;

1 Comment

It's not x160 but decimal 160.
0

I had no luck with the DOCTYPE approach from Aku.

What worked for me in MSXML transforms on an Windows 2003 server, was

    <xsl:text disable-output-escaping="yes">&amp;#160;</xsl:text>

Sort of a hybrid of the above. Thanks Stackoverflow contributors!

Comments

-3

One space character between text tags should be enough.

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.