User 4.5.5
User 4.5.5

Reputation: 1321

transforming content in xml having day,month,year into another xml to date of YYYYMMDD format using xslt

i have input xml as

           <content>
             <date>
            <day>14</day>
               <month>06</month>
                <year>2012</year>
               </date>
            </content>

want it to be converted into

          <content>
               <date>2012-06-14T00:00:00</date>
           </content>

xslt used

           <?xml version="1.0" encoding="utf-8"?>
            <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
             <xsl:output method="xml" indent="yes"/>

              <xsl:template match="content">
                 <content>
                   <xsl:apply-templates select="date"/>
                 </content>
              </xsl:template>

                 <xsl:template match="date">
                  <date>
                   <xsl:variable name="year" select="year"/>
                   <xsl:variable name="month" select="month"/>
                   <xsl:variable name="day" select="day"/>
               <xsl:value-of select="$year" />-<xsl:value-of select="$month"/>-
                 <xsl:value-of  select="$day"/>

                 </date>   
                </xsl:template>        

    </xsl:stylesheet>

i want date in YYYYMMDDTHH:MM:SS format as example 2012-06-15T02:52:37 how can i get it.Which function in xslt "1.0" pics the strings and take the format pattern as such. Can aynone please help me in getting the above format.

Upvotes: 3

Views: 1720

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243529

This transformation:

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

 <xsl:template match="/*">
     <content>
       <xsl:apply-templates/>
     </content>
 </xsl:template>

 <xsl:template match="date">
  <date>
   <xsl:value-of select="concat(year, '-', month, '-', day, 'T00:00:00')"/>
  </date>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<content>
    <date>
        <day>14</day>
        <month>06</month>
        <year>2012</year>
    </date>
</content>

produces the wanted, correct result:

<content>
   <date>2012-06-14T00:00:00</date>
</content>

Upvotes: 1

Related Questions