我有一个 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: ')
};
最佳答案
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 的“value”属性,并返回 $content 以将转换结果添加到数据库中。我建议使用(可能递归的)typeswitch 来识别元素节点,然后相应地调整它们的值..
喂!
关于xquery - 使用 mlcp、csv 文件和转换解析日期导入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37609079/