Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xslt: converting characters to their hexadecimal Unicode representation

Tags:

xslt

This is my input html (xhtml)

<SPAN style="font-family: wingdings"></SPAN>

I want to create an xml node like the following

<w:sym w:font="wingdings" w:char="F0D8"/>

How to get the character Unicode hexadecimal valude (F0D8) from the html: suggest a template for this.

like image 525
vignesh Avatar asked Mar 30 '11 06:03

vignesh


2 Answers

I was doing exactly the same thing as Michael Kay suggested in his answer :) Anyway, here is my code

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fn="http://www.w3.org/2005/xpath-functions"
    xmlns:my="http://www.example.com/my"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    version="2.0"
    exclude-result-prefixes="fn my xs">

    <xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>

    <xsl:function name="my:int-to-hex" as="xs:string">
      <xsl:param name="in" as="xs:integer"/>
      <xsl:sequence
        select="if ($in eq 0)
                then '0'
                else
                  concat(if ($in gt 16)
                         then my:int-to-hex($in idiv 16)
                         else '',
                         substring('0123456789ABCDEF',
                                   ($in mod 16) + 1, 1))"/>
    </xsl:function>

    <xsl:template match="//SPAN">
        <sym>
            <xsl:attribute name="char">
                <xsl:value-of select="my:int-to-hex(
                                        fn:string-to-codepoints(.))"/>
            </xsl:attribute>
        </sym>
    </xsl:template>

</xsl:stylesheet>

The int-to-hex function is courtesy of Yves Forkl. The output is:

<?xml version="1.0" encoding="UTF-8"?>
<sym char="F0D8"/>

I don't know how to do this using XSLT 1.0.

like image 117
MarcoS Avatar answered Sep 30 '22 21:09

MarcoS


In XSLT 2.0 you can get the numeric value of the character using the string-to-codepoints() function. You'll have to write a function to convert it to hex yourself, but that's not difficult.

like image 26
Michael Kay Avatar answered Sep 30 '22 20:09

Michael Kay