MarkLogic XML到JSON的转换 [英] MarkLogic XML to JSON conversion

查看:55
本文介绍了MarkLogic XML到JSON的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将此XML文件转换为JSON,但无法获得任何成功.我的XML中有两个子元素,但它仅返回最后一个.如何获取两个JSON格式的记录?

I am trying to convert this XML file into JSON but unable to get any success. I have two child element in my XML but it is returning only last one. How to get both the records in JSON format?

XML

 <Carousel>
  <Video>
    <Title>1</Title>
    <Abstract>3</Abstract>
    <FileName type="custom" mediatype="image">D</FileName>
    <HasAccess>4</HasAccess>
  </Video>
  <Video>
    <Title>1</Title>
    <Abstract>2</Abstract>
    <FileName type="custom" mediatype="image">D</FileName>
    <HasAccess>3</HasAccess>
  </Video>
</Carousel>

XQUERY:

import module namespace json = "http://marklogic.com/xdmp/json"  at "/MarkLogic/json/json.xqy";

let $custom := let $config := json:config("custom")
           return 
             (
              map:put( $config, "whitespace", "ignore" ),

              $config
             )
let $XML := $XMLFile (: XML content :)
return json:transform-to-json($XML,$custom)

当前输出:

{"Carousel":{"Video":{"Title":"1", "Abstract":"2", "FileName":{"type":"custom", "mediatype":"image", "_value":"D"}, "HasAccess":"3"}}}

我也使用默认的"Basic" JSON设置json:transform-to-json($XML)进行了尝试,但出现了类似错误

I also tried it with default "Basic" JSON setting json:transform-to-json($XML) but got error like

XML Element not in expected namespace [http://marklogic.com/xdmp/json/basic] (json:INVALIDELEM): Carousel

它仅在完全"模式下工作,但是它以JSON格式(例如_attribute等)添加了一些额外的信息,这在我的输出JSON中不是必需的.

It works in "Full" mode only, but it is adding some extra information in JSON format like _attribute etc. which is not required in my output JSON.

请帮助我或说明为什么它没有返回JSON格式的完整输出.

Please help me or give any idea why it is not returning complete output in JSON format.

推荐答案

您将需要将重复元素标记为入门的数组元素.这似乎可行:

You will need to mark repeating elements as array-elements for starters. This seems to work:

xquery version "1.0-ml";

import module namespace json = "http://marklogic.com/xdmp/json" at "/MarkLogic/json/json.xqy";

let $xml := <Carousel>
  <Video>
    <Title>1</Title>
    <Abstract>3</Abstract>
    <FileName type="custom" mediatype="image">D</FileName>
    <HasAccess>4</HasAccess>
  </Video>
  <Video>
    <Title>1</Title>
    <Abstract>2</Abstract>
    <FileName type="custom" mediatype="image">D</FileName>
    <HasAccess>3</HasAccess>
  </Video>
</Carousel>

let $custom :=
  let $config := json:config("custom")
  let $_ := map:put( $config, "whitespace", "ignore" )
  let $_ := map:put( $config, "array-element-names", "Video" )
  return $config

return json:transform-to-json($xml,$custom)

如果愿意,您也可以隐藏属性.该配置支持各种选项,我建议查看其文档:

You can probably also suppress attributes if you like. The config supports all kinds of options, I recommend looking at its documentation:

http://docs.marklogic.com/json:config

HTH!

这篇关于MarkLogic XML到JSON的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆