[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Decimal to hexadecimal in one function (XSLT/XPath

Subject: RE: Decimal to hexadecimal in one function (XSLT/XPath 2)
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Tue, 17 Feb 2009 12:31:16 -0000
RE:  Decimal to hexadecimal in one function (XSLT/XPath
Something like this perhaps:

<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>

But what's wrong with having two functions anyway?

Incidentally, do try to get into the habit of declaring your parameter and
return types. It helps people reading your code, it helps the compiler, and
it helps you.

Michael Kay
http://www.saxonica.com/

> -----Original Message-----
> From: Yves Forkl [mailto:Y.Forkl@xxxxxx] 
> Sent: 17 February 2009 11:23
> To: xsl-list
> Subject:  Decimal to hexadecimal in one function (XSLT/XPath 2)
> 
> Hi,
> 
> I want to convert the decimal integers returned by the XPath 
> 2 function fn:string-to-codepoints into a sequence of strings 
> that use the conventional "U+..." hexadecimal notation of the 
> Unicode standard.
> 
> Neither the XPath 2 spec nor the XSLT FAQ offered a complete 
> solution to this problem, so I developed my own. It is 
> working, but I wonder how to integrate these two functions, 
> which convert a decimal integer into its hexadecimal 
> equivalent, into a single function which still returns just 
> one string:
> 
> <xsl:function name="my:decimal-to-hex" as="xs:string">
>    <xsl:param name="decimalNumber"/>
>    <xsl:sequence
> select="string-join(my:decimal-to-hex-internal($decimalNumber),'')"/>
> </xsl:function>
> 
> <xsl:function name="my:decimal-to-hex-internal" as="xs:string+">
>    <xsl:param name="decimalNumber"/>
>    <xsl:variable name="hexDigits" select="'0123456789ABCDEF'"/>
>    <xsl:if test="$decimalNumber &gt;= 16">
>      <xsl:sequence
>        
> select="my:decimal-to-hex-internal(floor($decimalNumber div 16))"/>
>    </xsl:if>
>    <xsl:sequence
>      select="substring($hexDigits, ($decimalNumber mod 16) + 
> 1, 1)"/> </xsl:function>
> 
> 
>    Yves

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.