[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] 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
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|