简体   繁体   English

Marklogic中的日期格式问题

[英]Date formatting issue in Marklogic

I am using xdmp:document-filter(doc(uri)) to fetch the metadata from the documents. 我正在使用xdmp:document-filter(doc(uri))从文档中获取元数据。 When I run this command on one of the documents I get the following result:- 当我在其中一个文档上运行此命令时,得到以下结果:

xdmp:document-filter(doc("/Vision.doc"))//*:meta[@name eq "Creation_Date"]/@content xdmp:document-filter(doc(“ / Vision.doc”))// *:meta [@name eq“ Creation_Date”] / @ content

<?xml version="1.0" encoding="UTF-8"?>
<results warning="attribute node">
  <warning warning="attributes cannot be root nodes" content="17-05-2012 00:48:00"/>
</results>

And when I run this command on another document then I get this:- 当我在另一个文档上运行此命令时,我得到了:

<?xml version="1.0" encoding="UTF-8"?>
<results warning="attribute node">
  <warning warning="attributes cannot be root nodes" content="2012-06-03T13:45:00Z"/>
</results>

You can see that date format is different in both the outputs. 您可以看到两个输出中的日期格式都不同。 There may be different date formats in documents uploaded in Marklogic Server. 在Marklogic Server中上载的文档中可能有不同的日期格式。 But I want to show the creation date of documents in some fixed format (eg May 16, 2012). 但我想以某种固定格式显示文档的创建日期(例如2012年5月16日)。 How can I convert the different date formats to a fixed date format ? 如何将不同的日期格式转换为固定的日期格式? And also I want to compare these dates to the date entered by the user. 我也想将这些日期与用户输入的日期进行比较。 The documents matching the search criteria should get returned by the search query. 符合搜索条件的文档应由搜索查询返回。 So I have two questions here:- 所以我在这里有两个问题:

  1. How to convert creation date of particular documents to some fixed format and to display it in the UI. 如何将特定文档的创建日期转换为某种固定格式并在UI中显示。
  2. How to compare this creation date to the date entered by the user(which is in "mm/dd/yyyy" format) so that I can get the correct result. 如何将此创建日期与用户输入的日期(以“ mm / dd / yyyy”格式)进行比较,以便获得正确的结果。

You will have to parse the dateTime value. 您将必须解析dateTime值。 For example: 例如:

let $dt := "17-05-2012 00:48:00"
return
  if ($dt castable as xs:dateTime)
  then xs:dateTime($dt)
  else xdmp:parse-dateTime("[Y01]-[M01]-[D01] [h01]:[m01]:[s01]", $dt)

This will return an xs:dateTime atomic value, which can be compared and displayed in the UI. 这将返回xs:dateTime原子值,可以对其进行比较并在UI中显示。 If you want to support additional formats, you will need to create additional parse "picture" strings so they can also be converted to xs:dateTime . 如果要支持其他格式,则需要创建其他解析“图片”字符串,以便它们也可以转换为xs:dateTime See the documentation on xdmp:parse-dateTime() for more information. 有关更多信息,请参见xdmp:parse-dateTime()上的文档。

As a part of a larger open source project I cooked up a date parsing library that handles at least 20 different formats in 6 different languages. 作为一个较大的开源项目的一部分,我制作了一个日期解析库,该库可以使用6种不同的语言处理至少20种不同的格式。 You can also supply your own formats if one is not already defined. 如果尚未定义格式,也可以提供自己的格式。 It works by feeding it a date as a string in any of the defined formats and returning an xs:dateTime if it was able to successfully parse it. 它通过以任何定义的格式将日期作为字符串提供给日期,并能够成功解析它,则返回xs:dateTime来工作。 You can find the library here: 您可以在这里找到该库:

https://github.com/marklogic/Corona/blob/master/corona/lib/date-parser.xqy https://github.com/marklogic/Corona/blob/master/corona/lib/date-parser.xqy

To use it: 要使用它:

import module namespace dateparser="http://marklogic.com/dateparser" at "date-parser.xqy";

dateparser:parse($filteredDocument//*:meta[@name eq "Creation_Date"]/@content)

This will allow you to normalize the various date formats that binary documents can have. 这将允许您规范化二进制文件可以具有的各种日期格式。 I will note that different binary formats (Word, PDF, JPEG, etc) will use different names for the creation date. 我将注意到,不同的二进制格式(Word,PDF,JPEG等)将为创建日期使用不同的名称。 So just looking for the "Creation_Date" metadata could leave some holes depending on what formats you're storing in MarkLogic. 因此,仅查找“ Creation_Date”元数据可能会留下一些漏洞,具体取决于您存储在MarkLogic中的格式。

Also note, that if you just want the date information without the time portion, you can cast the xs:dateTime to an xs:date . 还要注意,如果只需要日期信息而没有时间部分,则可以将xs:dateTime转换为xs:date Doing so will retain the timezone information which is likely a good thing. 这样做将保留时区信息,这可能是一件好事。

As for your second question… 至于第二个问题...

There is a number of different ways to do this and reading some of the MarkLogic documentation is a good place to start. 有许多不同的方法可以做到这一点,阅读一些MarkLogic文档是一个不错的起点。 I'd recommend taking a look at: 我建议您看一下:

http://docs.marklogic.com/guide/search-dev/rangequery http://docs.marklogic.com/guide/search-dev/rangequery

Hopefully that will shed a bit of light on what your query needs to look like. 希望这将使您对查询的外观有所了解。 In simplest form you will probably have to first parse the date that the user provided. 以最简单的形式,您可能必须首先解析用户提供的日期。 This can also be done with the date parsing library so users can enter tons of different date formats (eg: November 13th, 2012 or even in Spanish Noviembre 13th, 2012). 也可以使用日期解析库来完成此操作,以便用户可以输入大量不同的日期格式(例如:2012年11月13日,甚至使用西班牙语Noviembre,2012年13月)。 Then use that parsed date to construct date range queries in MarkLogic. 然后使用该解析日期在MarkLogic中构造日期范围查询。

If that doesn't help I'd post another question here with the specifics of where you're getting hung up. 如果那没有帮助,我会在这里发布另一个问题,详细说明您要挂断电话的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM