This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: re: xsl-sort


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]