简体   繁体   中英

My XML output with date Format using XSL

I have my xml File

   <root>
         <item>Apple</item>
         <name><![CDATA[Indhu && Mathi]]></name>
         <date>28-02-2018</date>
         <dollar>500</dollar>
   </root>

Have my XSL file as:

      <xsl:import href="lookupValues.xsl"/>
       <xsl:template match="/">
         <html>
           <head></head>
           <body>
           <h1>Hello World</h1>
           <table border="1">
           <tr><td><xsl:value-of select="root/item"/></td> </tr>
           <tr><td><xsl:value-of select="root/name"/></td> </tr>
           <tr><td><xsl:value-of select="root/date"/></td></tr>


          <tr>
           <td> Date Format </td>
            <td><xsl:call-template name="format_date">
                <xsl:with-param name="arg" select="root/date" />
          </xsl:call-template> </td>
         </tr>       
           </table>
         </body>
         </html>
          </xsl:template>

Also the call function is written as lookupValues.xsl:

     <xsl:template name="format_date">
    <xsl:param name="arg"/>

    <xsl:variable name="vDay" select="substring-before($arg, '/')"/>        
    <xsl:variable name="vMonth" select="substring-before(substring-after($arg, '/'), '/')"/>        
    <xsl:variable name="vYear" select="substring-before(substring-after(substring-after($arg, '/'), '/'), ' ')"/>

    <xsl:if test="string-length($vDay) &lt; 3">
        <xsl:text>0</xsl:text>
    </xsl:if>
    <xsl:value-of select="$vDay"/>      
    <xsl:text>-</xsl:text>

    <xsl:if test="string-length($vMonth) &lt; 4">
        <xsl:text>0</xsl:text>
    </xsl:if>
    <xsl:value-of select="$vMonth"/>
    <xsl:text>-</xsl:text>

    <xsl:value-of select="$vYear"/>     
</xsl:template> 

I want to print my date in DD/MMM/YYYY format as . Need some corrections in my code. Am not sure about the function.

XSLT 1.0 does not provide any inbuilt functions for date formatting. There are date time extensions for slightly easing the task of working with dates, however not all the extensions work as per our requirements.

You will have to write custom templates for handling the conversion of numeric month to month name ie 02 to Feb . Below is the stylesheet that will convert the date from DD-MM-YYYY format to DD/MMM/YYYY format.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:output method="xml" />
    <xsl:strip-space elements="*" />

    <xsl:template match="root">
        <formattedDate>
            <xsl:call-template name="format-date">
                <xsl:with-param name="date" select="date" />
            </xsl:call-template>
        </formattedDate>
    </xsl:template>

    <xsl:template name="format-date">
        <xsl:param name="date" />
        <xsl:variable name="day" select="substring($date, 1, 2)" />
        <xsl:variable name="month" select="substring($date, 4, 2)" />
        <xsl:variable name="year" select="substring($date, 7, 4)" />
        <xsl:variable name="monthName">
            <xsl:call-template name="month-name">
                <xsl:with-param name="month" select="$month" />
            </xsl:call-template>
        </xsl:variable>
        <xsl:value-of select="concat($day, '/', $monthName, '/', $year)" />
    </xsl:template>

    <xsl:template name="month-name">
        <xsl:param name="month" />
        <xsl:choose>
            <xsl:when test="$month = 1">Jan</xsl:when>
            <xsl:when test="$month = 2">Feb</xsl:when>
            <xsl:when test="$month = 3">Mar</xsl:when>
            <xsl:when test="$month = 4">Apr</xsl:when>
            <xsl:when test="$month = 5">May</xsl:when>
            <xsl:when test="$month = 6">Jun</xsl:when>
            <xsl:when test="$month = 7">Jul</xsl:when>
            <xsl:when test="$month = 8">Aug</xsl:when>
            <xsl:when test="$month = 9">Sep</xsl:when>
            <xsl:when test="$month = 10">Oct</xsl:when>
            <xsl:when test="$month = 11">Nov</xsl:when>
            <xsl:when test="$month = 12">Dec</xsl:when>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

If your input format of dates is permanent and looks like 28-02-2018 (dd-mm-yyyy), then capturing of the date from the param can be simplified:

<xsl:template name="format_date">
    <xsl:param name="arg"/>
    <xsl:variable name="day">
        <xsl:value-of select="substring($arg,1,2)"/>
    </xsl:variable>
    <xsl:variable name="month">
        <xsl:value-of select="substring($arg,4,2)"/>
    </xsl:variable>
    <xsl:variable name="year">
        <xsl:value-of select="substring($arg,7,4)"/>
    </xsl:variable>
    <xsl:variable name="monthName">
        <xsl:call-template name="month-name">
            <xsl:with-param name="month" select="$month" />
        </xsl:call-template>
    </xsl:variable>
    <xsl:value-of select="concat($day, '/', $monthName, '/', $year)" />
</xsl:template>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM