Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 3rd, 2004, 05:40 AM
Authorized User
 
Join Date: Sep 2004
Location: , , United Kingdom.
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.

Reply With Quote
  #2 (permalink)  
Old September 3rd, 2004, 06:28 AM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
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/
Reply With Quote
  #3 (permalink)  
Old September 3rd, 2004, 06:31 AM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
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/
Reply With Quote
  #4 (permalink)  
Old September 3rd, 2004, 06:39 AM
Authorized User
 
Join Date: Sep 2004
Location: , , United Kingdom.
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?


Reply With Quote
  #5 (permalink)  
Old September 3rd, 2004, 08:17 AM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
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/
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 11:25 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.