[ACCEPTED]-Using an HTML entity in XSLT (e.g.  )-xslt

Accepted answer
Score: 124

You can use CDATA section

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

or you can describe 2 &nbsp in local DTD:

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

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

Score: 24

It is also possible to extend the approach 16 from 2nd part of aku's answer and get all known character 15 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 14 certain difference in the result as compared 13 to <xsl:text disable-output-escaping="yes"> approach. The latter one is going to 12 produce string literals like &nbsp; for all kinds 11 of output, even for <xsl:output method="text">, and this may happen 10 to be different from what you might wish... On 9 the contrary, getting entities defined for 8 XSLT template via <!DOCTYPE ... <!ENTITY ... will always produce output 7 consistent with your xsl:output settings.

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

Score: 15

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

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

this one returns a XsltParseError

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

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

Score: 6

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

Score: 5

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

Score: 1

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

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

Score: 0

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

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

/Sten 1 Hougaard

Score: 0

It is necessary to use the entity #x160;

0

Score: 0

I had no luck with the DOCTYPE approach 4 from Aku.

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

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

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

More Related questions