tags:

views:

4698

answers:

5

what is the best way to include a htlm entity in XSLT?

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

this one returns a XsltParseError

+17  A: 

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;

aku
+1  A: 

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;

Tom Lokhorst
+1  A: 

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

<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
Pierre Spring
+2  A: 

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

samjudson
+1  A: 

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.

James Sulak