How to insert   in XSLT

Xslt

Xslt Problem Overview


How can I insert

>  

Into an XSLT stylesheet, I keep getting this error:

> XML Parsing Error: undefined entity

Essentially I want a non breaking space character in the XSLT Template.

Xslt Solutions


Solution 1 - Xslt

Use the entity code   instead.

  is a HTML "character entity reference". There is no named entity for non-breaking space in XML, so you use the code  .

Wikipedia includes a list of XML and HTML entities, and you can see that there are only 5 "predefined entities" in XML, but HTML has over 200. I'll also point over to https://stackoverflow.com/questions/15798957/creating-a-space-nbsp-in-xsl which has excellent answers.

Solution 2 - Xslt

&#160; works really well. However, it will display one of those strange characters in ANSI encoding. <xsl:text> worked best for me.

<xsl:text> </xsl:text>

Solution 3 - Xslt

One can also do this :

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

Solution 4 - Xslt

Use this

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

edit: Downvoters should probably validate that this works first (it does, and is the most general solution to the problem.)

Solution 5 - Xslt

You might want to add the definition for this entity in the beginning of the file (below xml declaration):

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

Also you can add more entities such as Ntilde, Aacute, etc.

Solution 6 - Xslt

In addition to victor hugo's answer it is possible to get all known character references legal in an XSLT file, 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>&amp; &nbsp; &ndash;</xsl:text>

There is also certain difference in the result of this approach as compared to <xsl:text disable-output-escaping="yes"> one. The latter 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.

And when including all character references, it may be wise 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 http://s-n-ushakov.blogspot.com/2011/09/xslt-entities-java-xalan.html">XSLT, entities, Java, Xalan... for patch download and comments.

Solution 7 - Xslt

XSLT stylesheets must be well-formed XML. Since "&nbsp;" is not one of the five predefined XML entities, it cannot be directly included in the stylesheet. So coming back to your solution "&#160;" is a perfect replacement of "&nbsp;" you should use.

Example:

<xsl:value-of select="$txtFName"/>&#160;<xsl:value-of select="$txtLName"/>

Solution 8 - Xslt

When you use the following (without disable-output-escaping!) you'll get a single non-breaking space:

<xsl:text>&#160;</xsl:text>

Solution 9 - Xslt

I was trying to display borders on an empty cell in an HTML table. My old trick of using non-breaking space in empty cells was not working from xslt. I used line break with the same effect. I mention this just in case the reason you were trying to use the non-breaking space was to give some content to an 'empty' table cell in order to turn on the cell borders.

<br/>

Solution 10 - Xslt

you can also use:

<xsl:value-of select="&#160;"/>

Solution 11 - Xslt

Try to use

<xsl:text>&#160;</xsl:text>

But it depends on XSLT processor you are using: the XSLT spec does not require XSLT processors to convert it into "&nbsp;".

Solution 12 - Xslt

Although answer has been already provided by @brabster and others.
I think more reusable solution would be:

<xsl:variable name="space">&#160;</xsl:variable>
...
<xsl:value-of select="$space"/>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJL.View Question on Stackoverflow
Solution 1 - XsltbrabsterView Answer on Stackoverflow
Solution 2 - XsltSarojView Answer on Stackoverflow
Solution 3 - XsltWaterSoulView Answer on Stackoverflow
Solution 4 - XsltjagprinderdeepView Answer on Stackoverflow
Solution 5 - Xsltvictor hugoView Answer on Stackoverflow
Solution 6 - XsltSergey UshakovView Answer on Stackoverflow
Solution 7 - XsltAdilView Answer on Stackoverflow
Solution 8 - XsltShaamView Answer on Stackoverflow
Solution 9 - XsltB HView Answer on Stackoverflow
Solution 10 - XsltMattia VioView Answer on Stackoverflow
Solution 11 - XsltViacheslavView Answer on Stackoverflow
Solution 12 - XsltLeonid DashkoView Answer on Stackoverflow