2

There are already several threads with similar questions but I haven’t found this one: What’s the best way to compare dates in XPath 1.0 when they have different formats? For example:

<?xml version="1.0" encoding="utf-8"?>
<entries>
  <entry date="2010-11-22" format="yyyy-mm-dd" />
  <entry date="12.03.2014" format="dd.mm.yyyy" />
  <entry date="09/30/14" format="mm/dd/yy" />
</entries>

I want to find all entries with a date > than 03/03/14. Are there any tricks to split strings and reorder the chunks according to the format line and perform the translate() trick afterwards?

Community
  • 1
  • 1
Christian Vorhemus
  • 2,396
  • 1
  • 17
  • 29
  • You can create one format of all the dates, `yyyymmdd`, and do a number comparison. – Lingamurthy CS Mar 21 '15 at 00:36
  • Yes, but unfortunately the data format is unknown in advance. Of course it would be possible to preprocess every date in another programming language to yyyy-mm-dd and do a number comparison with XPath afterwards, but that would be an additional effort. – Christian Vorhemus Mar 21 '15 at 10:52
  • There must be a set of date formats you would receive. You would have to have rules to format **all such formats** to `yyyymmdd` and then will you be able to compare them. If there are no particular set of formats, I don't think any programming language will be of any help. – Lingamurthy CS Mar 21 '15 at 11:52
  • 1
    Okay, let's assume there can be 3 formats: yyyy-mm-dd, dd.mm.yyyy and mm/dd/yy. How can I transform them to yyyymmdd in XPath? – Christian Vorhemus Mar 21 '15 at 12:11
  • The first two formats, `yyyy-mm-dd` and `dd.mm.yyyy`, can be converted to `yyyymmdd` using substring, and translate functions. To convert `mm/dd/yy`, you can go through this [answer](http://stackoverflow.com/a/17685237/3244046). – Lingamurthy CS Mar 21 '15 at 13:05
  • 1
    Consider transforming your XML to a more rational format before querying it. – Michael Kay Mar 21 '15 at 16:20

0 Answers0