Subject: [xsl] Is there a better way for doing printf() in XSLT? From: Wolfgang Laun <wolfgang.laun@xxxxxxxxx> Date: Thu, 11 Feb 2010 19:35:49 +0100 |
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 ... -->
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] [Announce] Upcoming hands, G. Ken Holman | Thread | RE: [xsl] Is there a better way for, Michael Kay |
Re: [xsl] Assign the XML text to Ja, Robert Koberg | Date | [xsl] XSL wish list - page column g, John Cavalieri |
Month |