0

I'm trying to do a transformation that compare Dates using xpath

Here is a sample of my XML:

 <?xml version="1.0" encoding="UTF-8"?><shop xmlns="http://www.dei.isep.ipp.pt/lprog"        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xmlns:lprog="http://www.dei.isep.ipp.pt/lprog"         xsi:schemaLocation="http://www.dei.isep.ipp.pt/lprog TraXSD.xsd">        
        <Category nome="Fish">        
            <Article id="1" nome="fish1">                     
                <ProdDate>2018-10-02</ProdDate>                  
            </Article>              
        </Category >
    </shop>

And here is the Sample of a xslt with a

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

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
                xmlns:lprog="http://www.dei.isep.ipp.pt/lprog">
    <xsl:output method="html"/>

    <xsl:template match="/">
        <html>
            <head>
                <title>List of Products out of Time</title>
            </head>
            <body>
                <table border="1">
                    <tr bgcolor="#9acd32">
                        <th style="text-align:left">Category</th>
                    </tr>                 

                    <tr>          
                        <td>
                            <xsl:apply-templates select="//lprog:Category/lprog:Article"/>                   
                        </td>    

                    </tr>
                </table>
            </body>
        </html>
    </xsl:template>


    <xsl:template match="lprog:Category/lprog:Articleo">        
        <xsl:value-of select="@nome"/>
        <xsl:text>:</xsl:text>

        <xsl:value-of select="lprog:ProdDate[xs:date(lprog:ProdDate) le xs:date('2016-06-03')]"/>       ERROR HERE

        <xsl:text disable-output-escaping="yes">
            <p></p>   
        </xsl:text>       
    </xsl:template>

</xsl:stylesheet>

**Error in this line ** lprog:ProdDate[xs:date(lprog:ProdDate) le xs:date('2016-06-03')]"/>

Thank you!

Joseph
  • 159
  • 2
  • 4
  • 12

3 Answers3

2

First of all, if you want to use xs:date and the le operator, you need to use an XSLT 2.0 processor. Furthermore, of course the stylesheet needs to declare the xmlns:xs="http://www.w3.org/2001/XMLSchema" namespace.

I also think you want to correct lprog:ProdDate[xs:date(lprog:ProdDate) le xs:date('2016-06-03')] to lprog:ProdDate[xs:date(.) le xs:date('2016-06-03')] and match="lprog:Category/lprog:Articleo" to match="lprog:Category/lprog:Article".

With an XSLT 1.0 processor all you can compare is e.g. <xsl:value-of select="lprog:ProdDate[translate(., '-', '') &lt;= 20160603]"/>.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • I'm using the 1.0 version ..so perhaps i need to use a XSLT 2.0 or a different way ... It was a bad copy-paste to put "lprog:Articleo" .. but even with the date(.) it keep appearing the error – Joseph May 28 '16 at 18:41
  • 1
    You need to use an XSLT 2.0 processor like Saxon 9 or XmlPrime if you want to use XSLT/XPath 2.0 features like `xs:date` or the `le` operator. These features are not supported by XSLT 1.0 processors. – Martin Honnen May 28 '16 at 18:43
0

If you're using an XSLT 1.0 processor, you need to make your comparison like this:

translate(lprog:ProdDate, '-', '') &lt; 20160603

Note: there are other errors in your stylesheet. It's hard to list them all without knowing what exactly are you trying to accomplish here.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
0

I'm working with a quite similar problem - and I think the answer is to convert to Julian days (my style sheet is still a primitive draft). It's an absolute date, it should work with XSL 1.0, it doesn't require extensions.

I found the formula here:

Finding the difference between 2 dates in xslt

Community
  • 1
  • 1
Klas Blomberg
  • 115
  • 1
  • 9
  • No, you do **not** need to calculate the **difference** between two dates in order to **compare** them (find out which one is greater). --- And BTW you also don't need to calculate the **difference in days** in order to see if one date is more than 5 years later than the other - which seems to be [your original problem](http://stackoverflow.com/questions/37392402/xsl-1-0-datedifference-exslt). – michael.hor257k May 29 '16 at 16:46