0

Note: Please don't mark this as duplicate, hear my question out please

I'm completely new here and I've been running down a mental breakdown on getting a simple date difference between 2 dates from past 2 hours, every answer on the internet doesn't seem to work for me.

Can someone please provide exactly what do i have to put in the XML file and what goes in the XSL file to get the simplistic date difference possible?

Every answer out there just throws one segment of the code but with being new I have no idea where and how to implement it so thanks for understanding my issue :) Hope you can help me

Even if you mark this as duplicate, atleast put it in comment what exactly I have to put in XML file and what exactly goes in XSL file

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • I've gone through all of this links and none of them worked because of the vague answers http://stackoverflow.com/questions/9169650/how-to-calculate-date-difference-with-xpath-only http://stackoverflow.com/questions/16521746/how-to-get-date-difference-in-xslt http://stackoverflow.com/questions/5544762/finding-the-difference-between-2-dates-in-xslt – Radioactive Coffe Sep 15 '16 at 03:15
  • Is possible to use **xslt 2**? – uL1 Sep 15 '16 at 06:15
  • yes definitely as long as someone defines How because I've tried alot of things, they all didn't work – Radioactive Coffe Sep 15 '16 at 06:23
  • 1
    A question that says "I've tried everything and nothing works" is much less likely to get a good answer than one that says "I tried XYZ and it failed saying ABC". – Michael Kay Sep 15 '16 at 08:07

1 Answers1

2

One of the simplest XSLT 2.0-date-compare is like this:

XML:

<dates>
    <date id="1">2016-09-15</date>
    <date id="2">2016-09-10</date>
</dates>

XSLT 2.0:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">

    <xsl:template match="dates">
        <xsl:element name="difference">
            <xsl:value-of select="days-from-duration(xs:date(date[@id=1]) - xs:date(date[@id=2]))"/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

If this does not work, your problem is somewhere other located than the stylesheet. Then you have to state your environment like xslt-processor, software, programming language you are using.

uL1
  • 2,117
  • 2
  • 17
  • 28
  • doesn't work, just shows the data dump of the dates "2016-09-15 2016-09-10" – Radioactive Coffe Sep 15 '16 at 09:09
  • 1
    @RadioactiveCoffe, See it online at http://xsltransform.net/bFWR5DU where it is working to compute the difference. – Martin Honnen Sep 15 '16 at 09:13
  • That's funny, do you have any idea/solution on how I can get it to work on my pc? It just doesn't show up for some reason – Radioactive Coffe Sep 15 '16 at 09:30
  • also how does it work? you haven't made any reference to the xsl file – Radioactive Coffe Sep 15 '16 at 09:32
  • http://image.prntscr.com/image/e072f905af0f415f8c5207c93a49d988.png this is what it looks like on mine, if I copy paste the exact same code – Radioactive Coffe Sep 15 '16 at 09:33
  • Well, if you have never used XSLT 2.0 then read http://stackoverflow.com/documentation/xslt/1129/introduction-to-xslt/3641/installation-or-setup#t=201609150935232522275 on how to get started, you need an XSLT 2.0 processor like Saxon 9 or XmlPrime or Altova, browsers don't support XSLT 2.0. – Martin Honnen Sep 15 '16 at 09:36
  • is there any way with xslt 1.0? – Radioactive Coffe Sep 15 '16 at 09:40
  • @uL1 This question was tagged by the OP as XSLT 1.0. The answer given requires XSLT 2.0. You have edited the question and added the XSLT 2.0 tag, thus creating this whole mess. Please don't do that again. – michael.hor257k Sep 15 '16 at 10:32
  • @michael.hor257k see my comment and SO answer under the original question! XSLT 2.0 is possible! so i added the Tag! – uL1 Sep 15 '16 at 10:34