Using HTML entities in XSL

From VYRE

Jump to: navigation, search

There are occasions when you need to output special characters, such as a £ sign as HTML entities. One simple method is to wrap the entity inside an xsl:text tag like the following:

<xsl:text disable-output-escaping="yes">&amp;pound;: </xsl:text><xsl:value-of select="price"/></xsl:text>

This however can be labourious, having to repeat this method everytime you want to output an entity. An alternative method is to declare any entities you desire in a DOCTYPE in your stylesheet. You can do so in the following way:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet  [
	<!ENTITY pound  "&#163;">
]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8"/>
<xsl:template match="/">
 
</xsl:template>
</xsl:stylesheet>

Once an entity is declared in a DOCTYPE then it can used anywhere in your output:

&pound;: <xsl:value-of select="price"/>

It can be useful to declare a set of common HTML entities in your stylesheet:

<!DOCTYPE xsl:stylesheet  [
	<!ENTITY nbsp   "&#160;">
	<!ENTITY copy   "&#169;">
	<!ENTITY reg    "&#174;">
	<!ENTITY trade  "&#8482;">
	<!ENTITY mdash  "&#8212;">
	<!ENTITY ldquo  "&#8220;">
	<!ENTITY rdquo  "&#8221;">
	<!ENTITY pound  "&#163;">
	<!ENTITY euro   "&#8364;">
]>

Take a look at the W3Schools website for more information on HTML entities.

Personal tools