0

MarkLogic 版本:9.0-6.2

我们有一个 XML 文档,其中的元素“CustomerInfo”出现在多个位置。根据架构定义,该元素在一个地方是一个数组 (maxOccurs="unbounded"),但在所有其他地方都是一个常规元素。

我正在尝试使用自定义配置将 XML 转换为 JSON,并给出我希望将“CustomerInfo”元素转换为数组的确切路径。

以下是样本数据...

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

下面是代码...

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

此代码不会将元素转换为数组。如果我只是给元素名称如下,那么我看到它转换为数组。

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

我也尝试了如下的 QName 但仍然没有转换为数组。

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!

4

1 回答 1

1

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.

于 2019-05-01T17:32:36.797 回答