Friday, February 4, 2011

using a html entity in xslt (e.g.  )

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

  • 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;

    From aku
  • 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;

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

    <xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
    
  • XSLT only handles the five basic entities by default: lt, gt, apos, quot, and amp. All others need to be defined as @Aku mentions.

    From samjudson
  • 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.

0 comments:

Post a Comment