首页 > 解决方案 > 在 Biztalk 中将 XML Schema 转换为 JSON 数组列表

问题描述

我在下面定义了一个 xml 架构

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1">
    <xs:element name="Root">
        <xs:complexType>
            <xs:all>
                <xs:element name="bblist">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="item" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:all>
        </xs:complexType>
    </xs:element>
</xs:schema>

我想使用管道生成下面的 Json。

{
    "bblist":
    [
        "13403"

    ]
}

但 BizTalk 管道将其转换为

{"bblist": "13403"}

只是想知道我的架构是否正确。我是否定义 xsd 以正确生成 Json 数组?

标签: jsonxmlbiztalk

解决方案


您的 XSD 架构存在三个问题

  1. 您尚未定义目标命名空间。这意味着当它通过 XML Receive 时,它​​将 MessageType 设置为不引用架构的默认值集。这意味着它可能不知道在 JSON 编码器中使用哪个模式。

MessageType Root Promoted http://schemas.microsoft.com/BizTalk/2003/system-properties

  1. 您在架构定义中使用了 a<xs:all>而不是 a <xs:sequence>。JSON 编码器无法处理。

如果您将架构定义为

<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns:b="http://schemas.microsoft.com/BizTalk/2003" xmlns="http://bblist" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" targetNamespace="http://bblist" vc:minVersion="1.1" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Root">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="1" name="bblist">
          <xs:complexType>
            <xs:sequence>
              <xs:element minOccurs="1" maxOccurs="unbounded" name="item" type="xs:string" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

有效载荷为

<ns0:Root xmlns:ns0="http://bblist">
  <bblist>
    <item>item_0</item>
  </bblist>
</ns0:Root>

你得到一个输出

{
  "bblist": {
    "item": [
      "item_0"
    ]
  }
}

这更接近您预期的 JSON,它制作了一个重复元素的数组。

  1. 您的结构对于您期望的 JSON 不正确,因为您在项目上有重复,而不是在 blist 上。

如果您将架构定义为

<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns:b="http://schemas.microsoft.com/BizTalk/2003" xmlns="http://bblist" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" targetNamespace="http://bblist" vc:minVersion="1.1" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Root">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="1" maxOccurs="unbounded" name="blist" type="xs:string" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

XML 是

<ns0:Root xmlns:ns0="http://bblist">
  <blist>blist_0</blist>
</ns0:Root>

JSON是

{
  "blist": [
    "blist_0"
  ]
}

推荐阅读