【问题标题】:Conversion of XML Schema to JSON array list in Biztalk在 Biztalk 中将 XML Schema 转换为 JSON 数组列表
【发布时间】:2020-04-14 08:59:07
【问题描述】:

我在下面定义了一个 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 数组?

【问题讨论】:

  • 我在下面的回答是否有助于解决您的问题?

标签: json xml biztalk


【解决方案1】:

您的 XSD 架构存在三个问题

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

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

  1. 您在架构定义中使用了&lt;xs:all&gt; 而不是&lt;xs:sequence&gt;。 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"
  ]
}

【讨论】:

  • 我有同样的问题,但不可能添加目标命名空间,因为架构是由 SAP (IDoc) 生成的。在这种情况下,您是否知道在转换后的 JSON 中“强制”数组的其他选项?谢谢,克里斯。见:stackoverflow.com/questions/68016286/…
  • @Chris 您可能想将它作为一个新问题发布并链接到这个问题并解释为什么您不能添加命名空间。也许您无法将其添加到 SAP 架构中,但您应该能够创建另一个架构,然后将 SAP 消息映射到。
猜你喜欢
  • 2014-08-02
  • 1970-01-01
  • 2016-07-03
  • 2016-12-05
  • 2013-08-22
  • 1970-01-01
  • 2022-09-24
  • 1970-01-01
  • 2012-02-27
相关资源
最近更新 更多