【问题标题】:Add element names dynamically to array-element-names configuration of json:transform-to-json() in MarklogicAdd element names dynamically to array-element-names configuration of json:transform-to-json() in Marklogic
【发布时间】:2023-02-21 19:11:56
【问题描述】:

我正在执行 cts:search 并使用 MarkLogic 10 中的 json:transform-to-json() 函数将结果节点转换为 JSON。

示例代码:

let $config := json:config("custom")
let $response := cts:search(fn:doc(),$query)
return json:transform-to-json($response,$config)

生成的 XML 节点可能具有同名的子元素。我希望在转换期间将它们转换为数组,因此我需要获取所有具有相同名称的多个子元素,并在转换前将 QName 值添加为 array-element-names 配置。

这里的问题是我不知道会出现在结果 XML 节点中的数组子元素名称,而且一些结果可能有更多数量的子节点(大于 50 个节点)。

示例 XML 结果节点:

<meta>
      <id>draa066</id>
      <id>draa088</id>
      <xref rid="aff1" ref-type="aff"/>
      <xref rid="cor1" ref-type="corresp"/>
      <email>surowiec@mathematik.uni-marburg.de</email>
</meta>

预期的 JSON 输出:

{
  "meta": {
    "id": [
      "draa066",
      "draa088"
    ],
    "xref": [
      "",
      ""
    ],
    "email": "surowiec@mathematik.uni-marburg.de"
  }
}

请让我知道这样做的可能性。

【问题讨论】:

    标签: json xml xquery marklogic marklogic-10


    【解决方案1】:

    我相信以下内容对您有用。

    请特意注意 3 项的示例。中间一个(无数组)表明,即使没有结果,也可以安全地使用 array-element-names 作为一种可能不返回结果的函数签名(因为 MarkLogic 不会存储没有值的映射条目)

    xquery version "1.0-ml";
    import module namespace json="http://marklogic.com/xdmp/json"
     at "/MarkLogic/json/json.xqy";
    
    declare function local:get-duplicate-element-names($doc) as xs:QName* {
      fn:distinct-values(for $node in $doc/meta/node()
        let $name := fn:node-name($node)
        where fn:count($doc/meta/node()[fn:node-name(.) = $name]) > 1
        return $name)
    };
    
    
    let $responses := (
      document{
        <meta>
              <id>draa066</id>
              <id>draa088</id>
              <xref rid="aff1" ref-type="aff"/>
              <xref rid="cor1" ref-type="corresp"/>
              <email>surowiec@mathematik.uni-marburg.de</email>
        </meta>
      },
      document{  
        <meta>
              <id>draa088</id>
              <xref rid="aff1" ref-type="aff"/>
              <email>surowiec@mathematik.uni-marburg.de</email>
        </meta>
      },
      document{
        <meta>
              <id>draa066</id>
              <xref rid="aff1" ref-type="aff"/>
              <xref rid="cor1" ref-type="corresp"/>
              <email>surowiec@mathematik.uni-marburg.de</email>
        </meta>
      }
    )
    
    let $config := json:config("custom")
    
    return for $response in $responses
      return json:transform-to-json($response,$config=>map:with("array-element-names", local:get-duplicate-element-names($response)))
    

    结果:

       {
          "meta":{
             "id":[
                "draa066",
                "draa088"
             ],
             "xref":[
                {
                   "rid":"aff1",
                   "ref-type":"aff"
                },
                {
                   "rid":"cor1",
                   "ref-type":"corresp"
                }
             ],
             "email":"surowiec@mathematik.uni-marburg.de"
          }
       }
       
       {
          "meta":{
             "id":"draa088",
             "xref":{
                "rid":"aff1",
                "ref-type":"aff"
             },
             "email":"surowiec@mathematik.uni-marburg.de"
          }
       }
       
       {
          "meta":{
             "id":"draa066",
             "xref":[
                {
                   "rid":"aff1",
                   "ref-type":"aff"
                },
                {
                   "rid":"cor1",
                   "ref-type":"corresp"
                }
             ],
             "email":"surowiec@mathematik.uni-marburg.de"
          }
       }
    

    【讨论】:

      猜你喜欢
      • 2022-12-26
      • 1970-01-01
      • 1970-01-01
      • 2022-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-28
      • 2016-02-12
      相关资源
      最近更新 更多