3

我有一个 csv 文件。我已经使用 mlcp 管理将这些数据导入 MarkLogic,然后在 MarkLogic 中创建了一个 xml 文件。

现在在 csv 中,我在其中一列中随机使用了这种格式“6/29/2013 5:00:00 PM”。如何使用 xquery 和可能的节点替换作为转换函数将此日期转换为不同的格式,例如“2013-06-29”作为 MarkLogic 默认日期格式?

任何帮助表示赞赏...


我已经创建了 transform.xqy 并将其安装在 MLogic 中的模块上。我正在考虑使用“xdmp:node-replace”将日期替换为预期格式。或者我应该逐列浏览 csv 列(如何做?)并使用“castable as xs:dateTime”来确定日期值与否。然而,即使只是打印出内容值/uri,总是给我错误。

xquery version "1.0-ml";
module namespace example = "http://test.com/example";

(: If the input document is XML, insert @NEWATTR, with the value
 : specified in the input parameter. If the input document is not
 : XML, leave it as-is.
 :)
declare function example:transform(
  $content as map:map,
  $context as map:map
) as map:map*
{
  let $the-doc-uri := map:get($content, "uri")
  let $the-doc := map:get($content, "value")
  return
    trace($the-doc, 'The value of doc is: ')
};
4

2 回答 2

2

MarkLogic 文档包含 MLCP 转换的完整示例:

https://docs.marklogic.com/guide/mlcp/import#id_65640

它显示了这个示例,它向 XML 内容添加了一个属性:

declare function example:transform(
  $content as map:map,
  $context as map:map
) as map:map*
{
  let $attr-value := 
   (map:get($context, "transform_param"), "UNDEFINED")[1]
  let $the-doc := map:get($content, "value")
  return
    if (fn:empty($the-doc/element()))
    then $content
    else
      let $root := $the-doc/*
      return (
        map:put($content, "value",
          document {
            $root/preceding-sibling::node(),
            element {fn:name($root)} {
              attribute { fn:QName("", "NEWATTR") } {$attr-value},
              $root/@*,
              $root/node()
            },
            $root/following-sibling::node()
          }
        ), $content
      )
};

请记住,您应该更新 $content map:map 的“值”属性,并返回 $content 以将您的转换结果添加到数据库中。我建议使用(可能是递归的)类型开关来识别元素节点,然后相应地调整它们的值..

于 2016-06-03T10:33:33.240 回答
0

终于做到了。

问题是我必须使用mem:node-replace因为它在运行中,在内存中。而xdmp:node-replace是当数据已经在 MarkLogic 上时。

其余的正如预期的那样,我必须使用format-datexdmp:parse-dateTime来获取所需的日期格式。

这是一些片段

xquery version "1.0-ml";
module namespace ns_transform = "this_is_my_namespace";
import module namespace mem = "http://xqdev.com/in-mem-update" at "/MarkLogic/appservices/utils/in-mem-update.xqy";

declare variable $ns := "this_is_my_namespace";

declare function ns_transform:transform(
  $content as map:map,
  $context as map:map
) as map:map*
{  

    let $doc := map:get($content, "value")

    let $format_in := "[M]/[D]/[Y0001] [h01]:[m01]:[s01] [P]"
    let $format_out := "[Y0001]-[M01]-[D01]"

    let $old_date := $doc/*:root_doc/*:date/text()
    let $new_date :=  format-date(xs:date(xdmp:parse-dateTime($format_in, $old_date)), $format_out)

    let $new_doc := mem:node-replace($doc/*:root_doc/*:date,element {fn:QName($ns, "date")}{$new_date})
    let $_ := map:put($content, "value", $new_doc)

    return $content  
};
于 2016-06-08T04:00:30.853 回答