Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is it possible to format a number as hexadecimal?

1,379 views
Skip to first unread message

David Lowndes

unread,
Feb 12, 2008, 6:37:54 AM2/12/08
to
Is there any way in XSLT to convert a numerical value into a
hexadecimal representation?

Something like a format-number-hex() function perhaps?

Dave Lowndes

Martin Honnen

unread,
Feb 12, 2008, 8:09:07 AM2/12/08
to

It should be possible to implement a named template to perform the
conversion
<URL:http://www.google.de/search?hl=en&q=XSLT+decimal+to+hex+conversion&btnG=Google+Search&meta=>.
Or use an extension function.
Which XSLT processor are you targetting?

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

David Lowndes

unread,
Feb 12, 2008, 8:59:28 AM2/12/08
to
>It should be possible to implement a named template to perform the
>conversion
><URL:http://www.google.de/search?hl=en&q=XSLT+decimal+to+hex+conversion&btnG=Google+Search&meta=>.

Ah, something like this maybe?:

http://www.stylusstudio.com/xsllist/200303/post80360.html

>Or use an extension function.
>Which XSLT processor are you targetting?

I'm currently using the MS .Net XSLT facilities but I'm considering
switching to another engine for V2 facilities.

Dave

Martin Honnen

unread,
Feb 12, 2008, 9:34:32 AM2/12/08
to
David Lowndes wrote:

>> Or use an extension function.
>> Which XSLT processor are you targetting?
>
> I'm currently using the MS .Net XSLT facilities


You could use an extension function then implemented in C# e.g.

<msxsl:script implements-prefix="mf" language="C#">
public string toHex (double n)
{
return ((int)n).ToString("X");
}
</msxsl:script>

<xsl:template match="some-element"
xmlns:mf="http://example.com/2008/functions">

<xsl:value-of select="mf:toHex(number(.))"/>

</xsl:template>

David Lowndes

unread,
Feb 12, 2008, 10:35:23 AM2/12/08
to
>You could use an extension function then implemented in C# e.g.

That's the first I've ever heard of such things - that looks much more
elegant :) - I'll give that a go.

I'm considering using a different XSLT processor because of a bug
(https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=314173).
Is there an elegant way of doing the hex output in XSLT 2?

Dave

Martin Honnen

unread,
Feb 12, 2008, 11:52:34 AM2/12/08
to
David Lowndes wrote:

> Is there an elegant way of doing the hex output in XSLT 2?

Instead of using a named template and xsl:call-template in XSLT 1.0 you
can write a named function in XSLT 2.0 and call it with XPath function
call syntax everywhere you need it.

David Lowndes

unread,
Feb 13, 2008, 11:59:40 AM2/13/08
to
>Instead of using a named template and xsl:call-template in XSLT 1.0 you
>can write a named function in XSLT 2.0 and call it with XPath function
>call syntax everywhere you need it.

Thanks once again for all the help Martin.

I've tried the named template & the C# script methods, and I'll go
with the C# script for now as it provides better (easy) control over
the formatting (the result looks better presented with 8 fixed
characters - "X8").

Cheers
Dave

0 new messages