Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 29th, 2009, 05:23 AM
Authorized User
 
Join Date: Sep 2008
Posts: 26
Thanks: 2
Thanked 0 Times in 0 Posts
Default Right aligment of field

Hi!

I need format quantity field by adding leading space and align it right.

Like that:

Source:

<OriginalQuantity>1.000</OriginalQuantity>

Target:

<OriginalQuantity> __________1.000</OriginalQuantity>

(_) means space

So I tried different patterns:

<xsl:value-of select="format-number(OriginalQuantity, ###########.###)"/>

<xsl:value-of select="format-number(OriginalQuantity, ###############)"/>

<xsl:value-of select="format-number(OriginalQuantity, )"/>


No one was succesfull :( I've got error message on invalid character or unexpected token.


Would really appreciate any help.
 
Old March 29th, 2009, 08:05 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

The second argument to format-number should be a string so you will need to wrap the format string into quotes e.g.
format-number(OriginalQuantity, '0.000')

As for right-aligning, that is not possible with format-number, you will need to write your own function (in XSLT 2.0) or template (in XSLT 1.0) that does that although checking http://www.xsltfunctions.com/xsl/c0011.html#c0012 or the XSLT FAQ might turn up solutions for that.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old March 29th, 2009, 03:13 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Try the following, adapted from http://www.xsltfunctions.com/xsl/fun...o-length.html:

<xsl:function name="f:pad-string-to-length" as="xs:string"
xmlns:f="....." >
<xsl:param name="stringToPad" as="xs:string?"/>
<xsl:param name="padChar" as="xs:string"/>
<xsl:param name="length" as="xs:integer"/>

<xsl:sequence select="
string-join (
($stringToPad, for $i in (1 to ($length - string-length($stringToPad)) return $padChar)
,'')
"/>

</xsl:function>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
dani1 (March 31st, 2009)
 
Old March 30th, 2009, 02:44 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

On top of what others have said what output format are you using? If it's HTML then you could maybe able able to use CSS to right align and keep your output more 'pure'.
__________________
Joe
http://joe.fawcett.name/
 
Old March 31st, 2009, 05:04 AM
Authorized User
 
Join Date: Sep 2008
Posts: 26
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by mhkay View Post
Try the following, adapted from http://www.xsltfunctions.com/xsl/fun...o-length.html:

<xsl:function name="f:pad-string-to-length" as="xs:string"
xmlns:f="....." >
<xsl:param name="stringToPad" as="xs:string?"/>
<xsl:param name="padChar" as="xs:string"/>
<xsl:param name="length" as="xs:integer"/>

<xsl:sequence select="
string-join (
($stringToPad, for $i in (1 to ($length - string-length($stringToPad)) return $padChar)
,'')
"/>

</xsl:function>
Thanks mhkay, solved problem





Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace Gridview field if null with new field Indo77 ASP.NET 2.0 Basics 1 June 18th, 2007 06:22 AM
Fill field based on another field skwilliams Classic ASP Basics 3 December 30th, 2006 11:02 AM
Updating a Date field based on another field arholly Access VBA 6 November 22nd, 2006 11:19 AM
Copy previous field record if next field is null ecampos Access VBA 6 June 23rd, 2006 12:55 PM
Update city field based on zip field nganb SQL Server ASP 0 April 22nd, 2004 10:30 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.