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.
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.
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 Creating a space ( ) in XSL which has excellent answers.
Š
instead of  
, the latter which will always resolve to a Unicode non-breaking space. –
Mopey  
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>
One can also do this :
<xsl:text disable-output-escaping="yes"><![CDATA[ ]]></xsl:text>
disable-output-escaping
, which it is not required to and it was deprecated in XSLT 2.0 and more so in 3.0. In these versions you can reach the same effect with xsl:character-map
guaranteed to work cross-processor. Also, this places a named entity
in the output, which is not necessarily the same as a non-breaking space and the receiving end must have this entity declared (in HTML it usually is implicitly). –
Mopey Use this
<xsl:text disable-output-escaping="yes">&</xsl:text>nbsp;
edit: Downvoters should probably validate that this works first (it does, and is the most general solution to the problem.)
Â
, So, if you output in UTF-8, but don't tell whatever's loading it in Windows that it's UTF-8, you'll see the  character. –
Modigliani You might want to add the definition for this entity in the beginning of the file (below xml declaration):
<!DOCTYPE stylesheet [
<!ENTITY nbsp " " >
]>
Also you can add more entities such as Ntilde, Aacute, etc.
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>& –</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
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 XSLT, entities, Java, Xalan... for patch download and comments.
XSLT stylesheets must be well-formed XML. Since " "
is not one of the five predefined XML entities, it cannot be directly included in the stylesheet.
So coming back to your solution " "
is a perfect replacement of " "
you should use.
Example:
<xsl:value-of select="$txtFName"/> <xsl:value-of select="$txtLName"/>
When you use the following (without disable-output-escaping
!) you'll get a single non-breaking space:
<xsl:text> </xsl:text>
disable-output-escaping
for this –
Jugate Although answer has been already provided by @brabster and others.
I think more reusable solution would be:
<xsl:variable name="space"> </xsl:variable>
...
<xsl:value-of select="$space"/>
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/>
you can also use:
<xsl:value-of select=" "/>
&nbsp;
(assuming you intended to semicolon to be there), which will render as
, not as ` ` (nb-space). –
Mopey Try to use
<xsl:text> </xsl:text>
But it depends on XSLT processor you are using: the XSLT spec does not require XSLT processors to convert it into "
".
I do not know whether it is useful or not, but I have this work-about:
I define a new entity:
The healthy animal is up<nbsp />and<nbsp />doing.
Then I have a new defined template:
<xsl:template match="nbsp" >
<xsl:text disable-output-escaping='yes'>&nbsp;</xsl:text>
</xsl:template>
When parsed it gives:
"The healthy animal is up and doing"
I appreciate that <nbsp/> is almost as long as , so not much typing.
© 2022 - 2024 — McMap. All rights reserved.