Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an HTML entity in XSLT (e.g.  )

Tags:

xslt

What is the best way to include an html entity in XSLT?

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

this one returns a XsltParseError

like image 741
Pierre Spring Avatar asked Aug 28 '08 08:08

Pierre Spring


People also ask

How can XSLT transform XML into HTML explain with example?

The standard way to transform XML data into other formats is by Extensible Stylesheet Language Transformations (XSLT). You can use the built-in XSLTRANSFORM function to convert XML documents into HTML, plain text, or different XML schemas. XSLT uses stylesheets to convert XML into other data formats.

What is XSLT explain with an example?

XSLT is a transformation language for XML. That means, using XSLT, you could generate any sort of other document from an XML document. For example, you could take XML data output from a database into some graphics.

Can you use CSS with XSLT?

An XSLT style sheet can emit HTML <STYLE> elements, including CSS specifications, directly into the HTML that results from the XSLT transformation. This option works best when the number of CSS rules is small and easily managed.

Can we use Javascript in XSLT?

If you need to use the javascript in the transformation (for example, it contains a set of extension functions that are called within the transformation), you need to put the javascript contents (at least that of one javascript file) in a separate XSLT stylesheet file, using the proper extension element (such as <msxml ...


2 Answers

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;

like image 82
aku Avatar answered Sep 28 '22 09:09

aku


It is also possible to extend the approach from 2nd part of aku's answer and get all known character references available, 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>&nbsp;&minus;30&deg;</xsl:text> 

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

It may be wise then 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.

like image 35
13 revs Avatar answered Sep 28 '22 09:09

13 revs