xslt:将字符转换为其十六进制 Unicode 表示 [英] xslt: converting characters to their hexadecimal Unicode representation
本文介绍了xslt:将字符转换为其十六进制 Unicode 表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的输入 html (xhtml)
This is my input html (xhtml)
<SPAN style="font-family: wingdings"></SPAN>
我想创建一个像下面这样的xml节点
I want to create an xml node like the following
<w:sym w:font="wingdings" w:char="F0D8"/>
如何从 html 中获取字符 Unicode 十六进制值 (F0D8):为此建议一个模板.
How to get the character Unicode hexadecimal valude (F0D8) from the html: suggest a template for this.
推荐答案
我和 Michael Kay<做的完全一样/a> 在 他的回答 中建议:) 无论如何,这是我的代码
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>
int-to-hex
函数来自 Yves Forkl.输出为:
The int-to-hex
function is courtesy of Yves Forkl. The output is:
<?xml version="1.0" encoding="UTF-8"?>
<sym char="F0D8"/>
我不知道如何使用 XSLT 1.0 做到这一点.
I don't know how to do this using XSLT 1.0.
这篇关于xslt:将字符转换为其十六进制 Unicode 表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文