开发者

Parsing date strings

开发者 https://www.devze.com 2022-12-13 04:07 出处:网络
I\'m writing XSLT to transform an XML document from one DTD to another and in the process of doing so trying to tidy up some date strings.

I'm writing XSLT to transform an XML document from one DTD to another and in the process of doing so trying to tidy up some date strings.

Each record in my source document has a date element that contains a string representation of a date. Some illustrative examples:

  • 11 April 1995
  • 14th April 1995
  • 10 Sept 2002
  • 14 Sep 2004
  • 21-23 June 2002
  • 2005

I want my resulting document to contain dates (or date ranges where appropriate) in ISO 8601 format.

What's my best strategy for achieving this? I could开发者_StackOverflow knuckle down and start writing a function to convert them (probably based on regexes) but I find it hard to believe someone hasn't solved this problem already.

Is there an XSLT module/function out there that will do most of the work for me? Should I be looking outside XSLT for a solution?


I don't find so hard to believe it, since when you have dates in a nonstandard format, you can have literally anything, and hence it is not possible to develop a universal solution. How would you for example convert 21-23 June 2002 to one single date?

I guess that using a regex is indeed the best option. Maybe you will be able to find some ready made regex to handle some nonstandard date formats, but most probably you will have to tune it up for your needs.


You will need to do some lexical conversion and probably write most of it yourself. XSLT is not a good language to do the sort of thing you want - I would choose a DOM which has procedural code and Date libraries. In my own case I would use Java and XOM (http://xml.xom.nu) but you can do the same in Python or C# or...

I'd transform the dataes in the XML and then do the XSLT transformation in two steps

0

精彩评论

暂无评论...
验证码 换一张
取 消