Bhanu
Bhanu

Reputation: 419

MarkLogic : Array specification in XML to JSON conversion

MarkLogic Version : 9.0-6.2

We have an XML document with the element 'CustomerInfo' appearing in multiple places. As per the schema definition, this element is an array (maxOccurs="unbounded") at one place but a regular element in all other places.

I am trying to convert XML to JSON using custom configuration and giving exact path where I want the 'CustomerInfo' element to be converted to an array.

Below is the sample data...

<instance>
  <tns:CustomerDownload xmlns:tns="http://new.webservice.namespace" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <tns:CustomerDownloadInfo>
      <tns:CustomerInfo>
        ...
        ...

Below is the code...

const JsonConfig = json.config('custom');
JsonConfig['array-element-names'] = 
    ['\instance\CustomerDownload\CustomerDownloadInfo\CustomerInfo']

This code is not converting the element to array. If I just give the element name as below, then I see that its converted to array.

JsonConfig['array-element-names'] =['CustomerInfo']

I also tried QName as below but still not converting to array.

JsonConfig['array-element-names'] = 
  [xs.QName('\instance\CustomerDownload\CustomerDownloadInfo\CustomerInfo')]

How can I specify exact path in JsonConfig['array-element-names'], so that I can explicitly control which elements to be converted to arrays?

Thanks in advance!

Upvotes: 0

Views: 117

Answers (1)

DALDEI
DALDEI

Reputation: 3732

Using "\" as part of a name is not doing what you think. It is literally using "\" as part of a name NOT as a path spec. If your xml/schema uses the same QName in different places in your document that you do not want treated the same, then this will not work for you (there is no equivalent parameter for specifying a path to a name for special use).

Most standard schemas do not reuse the same QName with different structure -- its possible but its not common -- if you can get away with simply using "CustomerInfo" in all occurrences regardless of path in the document then your good, just "CustomerInfo".

If you must treat different paths in your document differently, what I would do is to first pre-process your document to an intermediate form replacing all the 'special' elements with uniquely named ones then you can run the run the transformation on the intermediate document. If you choose to use the same base name but a different namespace then the resulting JSON output (which discards the namespace by default) will be identical and need no further processing.

Upvotes: 1

Related Questions