简体   繁体   中英

Losing data while converting XML to JSON In Marklogic

I am converting XML to Json where the case having mixed content data is losing.

I have data having element "uidlink" within "abs" element this element repeated two times within "abs" element before second "uidlink" data is completely missing along with "uidlink".

what am i missing in the custom setting?

Input

<?xml version="1.0" encoding="UTF-8"?>
<abs>We are disconnected [Abs. <uidlink saiss="1917-02">1917A00171</uidlink> The extraordinarily
    insightful Nature of Americans study illuminates both .[Abs. <uidlink saiss="1917-08"
        >1917A00726</uidlink>]. or example, the insight that nature experiences are so often
    intensely social activities, a reminder of a sometimes forgotten key to connecting.</abs>

Code

declare function utils:ConvertXmlToJson($HighLightResponse as item()*) as item()* 
{
     let $config := json:config("custom")
                    let $_ := map:put( $config, "whitespace", "ignore" )
                    let $_ := map:put( $config, "array-element-names", (xs:QName("results"),xs:QName("personName"),xs:QName("affiliationGroup"),xs:QName("Highlight")
                            ,xs:QName("match"),xs:QName("indexTerms"),xs:QName("classification"),xs:QName("foreNames"),xs:QName("treatmentCodes")) )


                    let $_ := map:put( $config, "camel-case", fn:true())
    let $JsonOutput := json:transform-to-json($HighLightResponse, $config)  
    let $InputJson  := xdmp:quote($JsonOutput)
    let $Result     := fn:replace($InputJson, 'zzz@start', '<highlight>')
    let $JsonResult := fn:replace($Result, 'zzz@end', '</highlight>')
    return
            $JsonResult
};

What you are missing is that the "custom" strategy is not designed for this use case in general -- One of the simplifications that allow it to produce 'simple json output' from arbitrary XML is that mixed content is not handled (perfectly). The 'full' strategy is designed for that purpose (full fidelity, at the cost of a more verbose and 'ugly' format).

If your 'exceptions' to the assumptions of the custom strategy are few, you can work around this and advise it to switch to 'full' strategy for selected qnames. You can use the "full-element-names" property in the config object as described here: https://docs.marklogic.com/json:config

full-element-names

A list of XML element names which will be treated as an full expansion in JSON similar to the full strategy. These can be xs:QName or xs:string. If an xs:string is used then the default namespace is used to construct the QName.

BOTH

(xs:QName | xs:string)*

()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM