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 September 3rd, 2004, 05:40 AM
Authorized User
 
Join Date: Sep 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default Formatting dates in XSL

I have recently created an XSL transform that takes some XML and transforms it into some more XML.

I have hit a problem and i'm not sure if it is possible to do it in XSL.

I have THREE fields in my first XML file. <VEHICLEAGEYEARS>, <VEHICLEAGEMONTHS> and <TERM>
I need to transform these into one new field in my new XML file in the form of YYMM (Years and Months). I can convert them into months quite simply, i.e (VEHICLEAGEYEARS * 12) + VEHICLEAGEMONTHS + TERM.
But it needs to be in the form YYMM.

Any idea how this could be done?

I started with this but got stuck when the term came into the problem;
<NEW FIELD>
<xsl:for-each select="MOTOR">
  <xsl:choose>
     <xsl:when test="VEHICLEAGEYEARS[. &lt; 10]">
         <xsl:choose>
             <xsl:when test="VEHICLEAGEMONTHS[. &lt; 10]">
              <xsl:value-of select="concat('0', VEHICLEAGEYEARS)" />
              <xsl:value-of select="concat('0', VEHICLEAGEMONTHS)" />
             </xsl:when>
             <xsl:otherwise>
               <xsl:value-of select="concat('0', VEHICLEAGEYEARS)" />
               <xsl:value-of select="VEHICLEAGEMONTHS" />
             </xsl:otherwise>
          </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:choose>
           <xsl:when test="VEHICLEAGEMONTHS[. &lt; 10]">
             <xsl:value-of select="VEHICLEAGEYEARS" />
             <xsl:value-of select="concat('0', VEHICLEAGEMONTHS)" />
           </xsl:when>
        <xsl:otherwise>
           <xsl:value-of select="VEHICLEAGEYEARS" />
           <xsl:value-of select="VEHICLEAGEMONTHS" />
        </xsl:otherwise>
     </xsl:choose>
   </xsl:otherwise>
  </xsl:choose>
</xsl:for-each>
</NEW FIELD>

Any help would be most appreciated.

 
Old September 3rd, 2004, 06:28 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You're making this much too difficult. Just use

concat(format-number(VEHICLEAGEYEARS, "00"),
format-number(VEHICLEAGEMONTHS, "00"))



Michael Kay
http://saxon.sf.net/
 
Old September 3rd, 2004, 06:31 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Sorry, that code replaces the code you wrote, but doesn't handle TERM.

Calculate the number of months as $M using your existing code, then do

concat(format-number($M div 12, "00"),
format-number($M mod 12, "00"))

Michael Kay
http://saxon.sf.net/
 
Old September 3rd, 2004, 06:39 AM
Authorized User
 
Join Date: Sep 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Michael.

I used <xsl:value-of select="concat(format-number(VEHICLEAGEYEARS, "00"), format-number(VEHICLEAGEMONTHS, "00"))" /> as a test but when I transform it I get <NewField>NaNNaN</NewField> in the new XML.

Is there anything else I should be doing?


 
Old September 3rd, 2004, 08:17 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

That means that the values you are formatting are not numeric. Perhaps you are selecting the elements from the wrong context.



Michael Kay
http://saxon.sf.net/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Formatting Dates dhoward SQL Language 4 January 30th, 2007 04:00 PM
formatting strings / dates clandestine XML 3 June 22nd, 2005 09:52 AM
Formatting For Between Dates hcweb Classic ASP Basics 7 February 8th, 2005 02:29 AM
formatting dates treasacrowe Classic ASP Basics 3 October 20th, 2004 06:52 PM
Formatting Dates Louisa VB.NET 2002/2003 Basics 3 March 16th, 2004 05:52 AM





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