tags:

views:

506

answers:

2

I would like to convert some value from a XML attribute into valid HTML with entities. So that for example the string "olá" from the XML would be transformed from the XSLT into "olá"

I can't find any xsl function to do this. Any ideas ?

+3  A: 

You can specify us-ascii encoding in xsl:stylesheet element. The following XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:output indent="yes" method="html" encoding="us-ascii"/>

    <xsl:variable name="data" select="'olá'" />

    <xsl:template match="/">
        <xsl:text>olá - </xsl:text>
        <xsl:value-of select="$data" />
    </xsl:template>
</xsl:stylesheet>

gives:

ol&#225; - ol&#225;

The xml method in xsl:stylesheet gives the same result with the standard XML heading.

Igor Kuzmitshov
The problem is that xsl:text only works with literal values. I would like to encode a value coming from the input XML and xsl:value-of doesn't seem to care about the output encoding.
Hugo Palma
I used `xsl:text` for simplicity only. It works with `xsl:value-of` as well, I have checked it. I will add an example to my initial answer.
Igor Kuzmitshov
@Hugo: Either way, `xsl:text` and `xsl:value-of` should both work. `<xsl:variable name="foo" select="'olá'"/><xsl:value-of select="$foo"/>` produces `olá`.
Mads Hansen
A: 

Hi Hugo,

I´m pretty sure you´ll find this template useful: Download this xslt and check the one called "url-encode-num" http://advanced-internal-onebox.googlecode.com/files/obox_stylesheet.xslt

It transforms any special character... Well, any of the ones listed here with their numeric encodings:

<xsl:variable name="latbis">¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ"&lt;&gt;=</xsl:variable>
<xsl:variable name="digit1">111111111111111111111111111111111111111222222222222222222222222222222222222222222222222222222220000</xsl:variable>
<xsl:variable name="digit2">666666666777777777788888888889999999999000000000011111111112222222222333333333344444444445555552777</xsl:variable>
<xsl:variable name="digit3">123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123452465</xsl:variable>

But is easy to update.

Let me know if this is what you were looking for.

Pablo

Pablo Solera