MarkLogic XML to JSON conversion

本小妞迷上赌 提交于 2019-12-13 14:24:38

问题


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)

Current OUTPUT:

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

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

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.

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


回答1:


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!




回答2:


The docs here http://docs.marklogic.com/json/json

Go over the concept but its often missed that there is a fundimental difference in the strategies, they are designed for different use cases.

The default ("basic") strategy is designed specifically to take arbitrary JSON and convert to a 'black box' XML and back with 100% fidelity.

The "full" strategy is degined for the reverse, to take arbitrary XML and convert to a 'black box' JSON format and back with as high as resonable fidelity (some XML features are lost in the process).

Neither of these is intended for having any control (or concern) for the target format, and they are not intended (or work) with any changes in the target format.

Where you want some control over both the source and target formats, the "custom" strategy is designed for controlling the transformation in a 'custom' way. The result is you have much more control over both JSON and XML formats, on the other hand, the simplifications required to produce "nice" results in one format or another require assumptions about the schema or data that is not always true. The configuration parameters help control this, but ultimately due to the mismatch of XML and JSON data models it is in general impossible to be able to create a transformation that is perfect for Document A and Document B without making the configuration so complex that you'd be better off hand coding it. So consider the "custom" transformation a compromise intended to satisfy a large set of common patterns easily , but may not be exactly what you want. You can extend the range by either being a little more flexibility in your requirements (XML or JSON formats) or by adding a pre or post processng step to convert your data into a format closer to that which the custom format can handle.



来源:https://stackoverflow.com/questions/26858466/marklogic-xml-to-json-conversion

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!