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

Re: re: xsl-sort

Subject: Re: re: xsl-sort
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Sat, 23 Dec 2000 21:02:48 +0000
xsl tohex
Jack,

> Maybe I need to do more homework but it seems that xslt is not a
> very rich programming language for general-purpose stuff which is
> why I was previously using script to do certain things. For example,
> how do you output a number as a hex string in xsl(t) without
> resorting to indexing into an array of characters representing the
> hex digits?

It's certainly true that there are some things that XSLT is not very
good at, and some things that XSLT finds very difficult to cope with,
one of which is doing any sort of sophisticated maths.  Of course you
*can* output a number as a hex string in XSLT, but yes the obvious way
is to use a string holding the hex digits:

<xsl:template match="number">
  <xsl:call-template name="toHex">
    <xsl:with-param name="decimalNumber" select="." />
  </xsl:call-template>
</xsl:template>

<xsl:variable name="hexDigits" select="'0123456789ABCDEF'" />
<xsl:template name="toHex">
   <xsl:param name="decimalNumber" />
   <xsl:if test="$decimalNumber > 16">
      <xsl:call-template name="toHex">
         <xsl:with-param name="decimalNumber"
                         select="floor($decimalNumber div 16)" />
      </xsl:call-template>
   </xsl:if>
   <xsl:value-of select="substring($hexDigits,
                                   ($decimalNumber mod 16) + 1, 1)" />
</xsl:template>

More difficult are trig functions like sin() and cos(), which would
both be very helpful in generating SVG with XSLT but aren't part of
XPath.

To include these kinds of functions, XSLT 1.1 possibly prompted by
MSXML3, includes a 'xsl:script' element to enable you include
user-defined functions.  You can't use XSLT 1.1 yet, because it's only
a working draft and not (as far as I know) supported by any
processors, but you can use MSXML3's msxsl:script extension element:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:foo="http://www.foo.com/"
                extension-element-prefixes="msxsl" />

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

<msxsl:script language="javascript"
              implements-prefix="foo">
  function toHex(decimalNumber) {
    ...
  }
</msxsl:script>

</xsl:stylesheet>

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


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.