Hello,
I have a xml file which has 106 books..

I xsl tranformed it and here:

<xsl:value-of select=" bioghist/bioghist/chronlist/chronitem/date"/>

is the date every book was created with that format: ddmmyyyy

how can i add slashes? and make it like that dd/mm/yyyy ??

thanks in advance :)

Recommended Answers

All 6 Replies

Here is a simple example of how to do it. Suppose you have the following XML file

<timesheet>
<date>04072010</date>
</timesheet>

and you apply the following stylesheet to it

<?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" />

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

  <xsl:template match="date">
     <xsl:element name="date">
         <xsl:call-template name="formatdate">
             <xsl:with-param name="datestr" select="."/>
        </xsl:call-template>
     </xsl:element>
  </xsl:template>

  <xsl:template name="formatdate">
     <xsl:param name="datestr" />
     <!-- input format ddmmyyyy -->
     <!-- output format dd/mm/yyyy -->

    <xsl:variable name="dd">
       <xsl:value-of select="substring($datestr,1,2)" />
    </xsl:variable>

    <xsl:variable name="mm">
       <xsl:value-of select="substring($datestr,3,2)" />
    </xsl:variable>

    <xsl:variable name="yyyy">
       <xsl:value-of select="substring($datestr,5,4)" />
    </xsl:variable>

    <xsl:value-of select="$dd" />
    <xsl:value-of select="'/'" />
    <xsl:value-of select="$mm" />
    <xsl:value-of select="'/'" />
    <xsl:value-of select="$yyyy" />
  </xsl:template>

  <xsl:template match="node()|@*">
     <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
     </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

The output from the transformation is

<?xml version="1.0"?>
<timesheet>
<date>04/07/2010</date>
</timesheet>

Perfect!!! thank you so much!!!!!!

Is you're using XSLT 2.0, this is a one line solution that is much more concise. There's a format-date() function available. If you're using XSLT 1.0, the above is unfortunately evil, but necessary.

Is you're using XSLT 2.0, this is a one line solution that is much more concise. There's a format-date() function available. If you're using XSLT 1.0, the above is unfortunately evil, but necessary.

Yes that is true. However mosts people still use XSLT 1.0 hence I provided an XSLT 1.0 solution. For completeness here is the solution using XSLT 2.0.

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" indent="yes"/>

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

  <xsl:template match="date">
       <xsl:copy>
           <xsl:value-of select="format-dateTime(.,'[M01]/[D01]/[Y0001]')" />
       </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

Do you have a sample or how would you convert from yyyy-mm-dd HH:MM:SS.s to yyyy-mm-dd HH:MM:SS.s

Thanks

Not visible with the pictures. My question is how you will do the conversion to the format YYYY-MM-DDTHH:MM:DD:SS.S

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.