[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: Yves Forkl <Y.Forkl@xxxxxx>
Date: Tue, 17 Feb 2009 14:02:25 +0100
Re:  Decimal to hexadecimal in one function (XSLT/XPath
Michael Kay wrote:
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>

Thank you very much for this solution, and thanks to Michael M.-H. for his one which looks just as neat. Both work fine, but in the code above I first had to re-arrange the parentheses:

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

Well, not too much. I just don't like function definitions cluttering my stylesheet, their sole raison d'jtre being my limited understanding of XSLT and XPath... :-)


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.

You are absolutely right. I normally do declare all of my parameters and return types, but in this case copied the code from somewhere and forgot to "augment" it.

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.