p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   Formatting dates in XSL (http://p2p.wrox.com/showthread.php?t=17858)

francislang September 3rd, 2004 05:40 AM

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.


mhkay September 3rd, 2004 06:28 AM

You're making this much too difficult. Just use

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



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

mhkay September 3rd, 2004 06:31 AM

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/

francislang September 3rd, 2004 06:39 AM

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?



mhkay September 3rd, 2004 08:17 AM

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/


All times are GMT -4. The time now is 05:50 PM.

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