Thursday 23 August 2007

XSLT Maths operators

Here is a stylesheet to example XSLTs maths operators

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes"/>

<xsl:template match="numbers">
A. 4 + 3.2 = <xsl:value-of select="x + y"/>
B. 3.2 - 4 = <xsl:value-of select="y - x"/>
C. 4 * 3.2 = <xsl:value-of select="x * y"/>
D. 11/3.2 = <xsl:value-of select="z div y"/>
E. 4 + 3.2 * 11 = <xsl:value-of select="x+y*z"/>
F. (4 + 3.2) * 11 = <xsl:value-of select="(x+y)*z"/>
G. 11 mod 4 = <xsl:value-of select="z mod x"/>
H. 4 + 3.2 + 11 = <xsl:value-of select="sum(*)"/>
I. floor(3.2) = <xsl:value-of select="floor(y)"/>
J. ceiling(3.2) = <xsl:value-of select="ceiling(y)"/>
K. round(3.2) = <xsl:value-of select="round(y)"/>
L. 11 + count(*) = <xsl:value-of select="11+count(*)"/>
M. 3.2 + string-length("3.2") =
<xsl:value-of select="y + string-length(y)"/>
N. 11 + "hello" = <xsl:value-of select="z + 'hello'"/>
</xsl:template>

</xsl:stylesheet>

this results in:

A. 4 + 3.2 = 7.2
B. 3.2 - 4 = -0.8
C. 4 * 3.2 = 12.8
D. 11/3.2 = 3.4375
E. 4 + 3.2 * 11 = 39.2
F. (4 + 3.2) * 11 = 79.2
G. 11 mod 4 = 3
H. 4 + 3.2 + 11 = 18.2
I. floor(3.2) = 3
J. ceiling(3.2) = 4
K. round(3.2) = 3
L. 11 + count(*) = 14
M. 3.2 + string-length("3.2") =
6.2
N. 11 + "hello" = NaN

Wednesday 8 August 2007

convert to camel case using XPath 2

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="http://www.metaphoricalweb.org/xmlns/string-utilities"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="2.0">
<xsl:function name="str:title-case" as="xs:string">
<xsl:param name="expr"/>
<xsl:variable name="tokens" select="tokenize($expr,' ')"/>
<xsl:variable name="titledTokens" select="for $token in $tokens return
concat(upper-case(substring($token,1,1)),
lower-case(substring($token,2)))"/>
<xsl:value-of select="string-join($titledTokens,'')"/>
</xsl:function>
<xsl:template match="/">
<data><xsl:value-of select="str:title-case('This is a test')"/></data>
</xsl:template>
</xsl:stylesheet>