Subject: RE: Is there a better way for doing printf() in XSLT?
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 11 Feb 2010 22:04:21 -0000
|
No, there isn't a better approach. Some functions like this were proposed
during XSLT 2.0 development, but the general view was that formatting ASCII
layouts using fixed-width fonts was not a significant use case. Even though
email still seems incapable of handling much else.
Regards,
Michael Kay
http://www.saxonica.com/
http://twitter.com/michaelhkay
> -----Original Message-----
> From: Wolfgang Laun [mailto:wolfgang.laun@xxxxxxxxx]
> Sent: 11 February 2010 18:36
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: Is there a better way for doing printf() in XSLT?
>
> Trying to improve the readability of an XML file I've come up
> with some templates (below, part 1) which produce the output
> shown at the end. As this works fine, the only question I
> have whether this is the best one can do in XSLT 2, or did I
> miss some cuter approach?
> Thanks
> -W
>
> <xsl:function name="str:pad" as="xsd:string">
> <xsl:param name="str" as="xsd:string"/>
> <xsl:param name="len" as="xsd:integer"/>
> <xsl:variable name="lstr" select="string-length($str)"/>
> <xsl:variable name="pad"
> select="string-join((for $i in 1 to $len -
> $lstr return ' '),'')"/>
> <xsl:value-of select="concat( ' ',$str,$pad)"/> </xsl:function>
>
> <xsl:function name="str:fmt" as="xsd:string">
> <xsl:param name="int" as="xsd:string"/>
> <xsl:param name="len" as="xsd:integer"/>
> <xsl:variable name="lstr" select="string-length($int)"/>
> <xsl:variable name="pad"
> select="string-join((for $i in 1 to $len -
> $lstr return ' '),'')"/>
> <xsl:value-of select="concat( $pad,$int)"/> </xsl:function>
>
> <xsl:template match="/">
> <xsl:apply-templates/>
> <xsl:text>
> </xsl:text>
> <xsl:comment>
> <xsl:text>
> Name : Left Right
> </xsl:text>
> <xsl:for-each select="//topel[@lengthLeft]">
> <xsl:sort select="@name"/>
> <xsl:value-of select="str:pad(@name,12)"/><xsl:text> [</xsl:text>
> <xsl:value-of select="str:fmt(@group,2)"/><xsl:text>,</xsl:text>
> <xsl:value-of select="str:fmt(@x25No,4)"/><xsl:text>]:</xsl:text>
> <xsl:value-of select="str:fmt(@lengthLeft,6)"/><xsl:text>
> m </xsl:text>
> <xsl:value-of
> select="str:fmt(@maxSpeedLeft,5)"/><xsl:text> km/h </xsl:text>
> <xsl:value-of select="str:fmt(@lengthRight,6)"/><xsl:text>
> m </xsl:text>
> <xsl:value-of
> select="str:fmt(@maxSpeedRight,5)"/><xsl:text> km/h
> </xsl:text> <xsl:text> </xsl:text> </xsl:for-each>
>
> <xsl:for-each select="//topel[@length]">
> <xsl:sort select="@name"/>
> <xsl:value-of select="str:pad(@name,12)"/><xsl:text> [</xsl:text>
> <xsl:value-of select="str:fmt(@group,2)"/><xsl:text>,</xsl:text>
> <xsl:value-of select="str:fmt(@x25No,4)"/><xsl:text>]: </xsl:text>
> <xsl:value-of select="str:fmt(@length,6)"/><xsl:text> m
> </xsl:text>
> <xsl:value-of select="str:fmt(@maxSpeed,5)"/><xsl:text>
> km/h </xsl:text> <xsl:text> </xsl:text> </xsl:for-each>
> </xsl:comment>
> </xsl:template>
>
> </ns2:awsCheckInfo>
> <!--
> Name : Left Right
> Kr112 [ 2, 101]: 75 m 100 km/h 75 m 100 km/h
> W1 [ 1, 101]: 25 m 60 km/h 25 m 100 km/h
> ...
> Gl 101 [31,2101]: 500 m 100 km/h
> Gl 102 [31,2102]: 500 m 100 km/h
> Gl 104 [31,2113]: 150 m 100 km/h
> ...
> -->
|