1

I have a problem converting dates from XML document using XSLT 2.0.

In HTML doc my date must look as 15-12-2006 => 15 December 2006

I'm using XSLT function "format-date" but saxon9he ignores it.

XML code:

?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="test2.xsl" type="text/xsl" ?>
  <catalog>
<book id="bk111">
        <author>LALALALALALA</author>
        <title>LALALALALLALALAL</title>
        <genre>LALALLALALALAL</genre>
        <publish_date>01-11-2011</publish_date>
        <description>LALALLALA</description>
</book>
<book id="bk156">
        <author>LALLALALA</author>
        <title>LALALLALAL</title>
        <genre>LALAL</genre>
        <price>99.00</price>
        <publish_date>02-09-2009</publish_date>
        <description>LALALLALA</description>
</book>
<book id="bk132">
        <author>LALLALALALALAL</author>
        <title>LALAL</title>
        <genre>LALALALA</genre>
        <price>55.00</price>
        <publish_date>06-12-2012</publish_date>
        <description>LALLALALA</description>
</book>

          </catalog>

XSLT code:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="html"></xsl:output>
<xsl:strip-space elements="*"></xsl:strip-space>
<xsl:param    name="publish_date"   as="xs:dateTime"  />
<xsl:template match ="/" >
    <html>
      <head>
      </head>
    <body>
      <div>
           <table>
           <tr>
           </tr>
                <xsl:apply-templates/>
           </table>
      </div>
    </body>

    </html>
</xsl:template> 
<xsl:template match="catalog">
<td><b>Author</b></td>
<td><b>Book name</b></td>
<td><b>Category</b></td>
<td><b>Publish Date</b></td>
<td><b>Price</b></td>
<td><b>Description</b></td>
<xsl:for-each select="book">
<xsl:sort select="price" data-type="number"/>
<tr>
    <td><xsl:value-of select="author"/></td>
    <td><xsl:value-of select="title"/></td>
    <td><xsl:value-of select="genre"/></td>
    <td><xsl:value-of select="publish_date"/></td>
    <td><xsl:value-of select="price"/></td>
    <td><xsl:value-of select="description"/></td>
</tr>
</xsl:for-each>
</xsl:template>

<xsl:template match="book/publish_date">
<xsl:variable name="isoDate"
select="concat(substring(., 7, 4), '-', substring(., 4, 2), '-', substring(., 1, 2))"/>
<xsl:copy>
<xsl:value-of select="format-date(xs:date ($isoDate), '[D] [MNn] [Y]')"/>
</xsl:copy>
</xsl:template>


</xsl:stylesheet>

in HTML output dates NOT converts!

1 Answers1

0

The format-date() function requires a yyy-mm-dd date (with optional timezone) as its argument.


Edit:

Given the following input:

<catalog>
    <book id="bk101">
            <author>author1</author>
            <title>title1</title>
            <pdate>01-10-2011</pdate>
    </book>
    <book id="bk107">
            <author>author2</author>
            <title>title2</title>
            <pdate>02-12-2005</pdate>
    </book>
</catalog>

the following stylesheet:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="pdate">
    <xsl:variable name="isoDate" select="concat(substring(., 7, 4), '-', substring(., 4, 2), '-', substring(., 1, 2))" />
    <xsl:copy>
        <xsl:value-of select="format-date(xs:date($isoDate), '[D] [MNn] [Y]')" />
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

will produce:

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
   <book id="bk101">
      <author>author1</author>
      <title>title1</title>
      <pdate>1 October 2011</pdate>
   </book>
   <book id="bk107">
      <author>author2</author>
      <title>title2</title>
      <pdate>2 December 2005</pdate>
   </book>
</catalog>

Edit 2:

The reason why your updated code doesn't work is that the template matching book/publish_date is never applied. Instead of:

<td><xsl:value-of select="publish_date"/></td>

you need to have:

<td><xsl:apply-templates select="publish_date"/></td>

And of course you need to remove the <xsl:copy> </xsl:copy> wrappers from the template itself.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • so should look like my XSLT function? – alex_romal93 Jul 30 '14 at 16:49
  • @user3891597 You must use string functions to rearrange the date as yyyy-mm-dd before feeding it to the format-date() function. See an ecxample here: http://stackoverflow.com/questions/22868423/xslt-date-formating/22883013#22883013 – michael.hor257k Jul 30 '14 at 16:59
  • tried to do so as described in that method it does not work my conversion should look like this: 15-12-2006 => 15 December 2006 – alex_romal93 Jul 30 '14 at 18:56
  • @alex_romal93 No, your conversion needs to look like this: **Step 1:** "15-12-2006" => "2006-12-15"; **Step 2**: `format-date(xs:date('2006-12-15'), '[D] [MNn] [Y]')`. This is assuming you want to use the format-date() function (you can always format the date yourself). – michael.hor257k Jul 30 '14 at 19:19
  • I understand, but i can not make a step 1 if u can look at the code I have written on top – alex_romal93 Jul 30 '14 at 20:56
  • @alex_romal93 I am not sure why this is so difficult - see the edit to my post. – michael.hor257k Jul 30 '14 at 21:09
  • I understand that this is a very simple logic, but I'm sitting on this for the 3rd day! and that the code you've written all the same does not work – alex_romal93 Jul 31 '14 at 10:54
  • It sounds like you are trying to perform this transformation in a browser. You have tagged this question as XSLT 2.0, and `format-date()` is an XSLT 2.0 function. However, there is no browser that supports XSLT 2.0. – michael.hor257k Jul 31 '14 at 12:39
  • not in browser... i'm use saxon9.5he... in the output I have HTML doc – alex_romal93 Jul 31 '14 at 12:51