开发者

xslt: converting characters to their hexadecimal Unicode representation

This is my input html (xhtml)

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

I want to creat开发者_运维问答e 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.


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.


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.


There is one error in @MarcoS's function: instead of if ($in gt 16) should be if ($in ge 16) (greater then or equal). Wrong hexadecimal values were generated for example for number 259.

The full function should looks like this:

<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 ge 16)
        then my:int-to-hex($in idiv 16)
        else '',
        substring('0123456789ABCDEF',
        ($in mod 16) + 1, 1))"/>
    </xsl:function>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜