JSON模式等效于XSD Maxoccurs

发布于 2025-01-28 06:06:53 字数 1351 浏览 2 评论 0原文

我已经搜索了几个小时,但是找不到查询的示例。我有一个XSD模式(这是一个非常削减的版本),

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Transaction-Header">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Origin" type="xs:string" minOccurs="1" maxOccurs="1">
        </xs:element>
        <xs:element name="Type" type="xs:string" minOccurs="0" maxOccurs="1">
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="Transaction">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="Transaction-Header" minOccurs="1" maxOccurs="1" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

我将其转换为Jsonschema(这是一个巨大的XSD),并取得了很大的成功。

本质上,总是只有1个交易头 IE

{
  "Transaction": {
    "Transaction-Header": {
      "Origin": "Origin",
      "Type": "Type"
    }
  }
}

无效:

{
  "Transaction": {
    "Transaction-Header": {
      "Origin": "Origin",
      "Type": "Type"
    },
    "Transaction-Header": {
      "Origin": "Origin",
      "Type": "Type"
    }
  }
}

我知道这可以通过使用数组Minitems / MaxItems功能来完成,但是这些确实是元素,而不是数组,我真的不想在可能的情况下定义它们。

有人知道这是怎么可能(甚至是)这是可能的吗?

I have been searching around for a few hours now but I can't find examples for the query I have. I have an XSD schema (this is a really cut down version)

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Transaction-Header">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Origin" type="xs:string" minOccurs="1" maxOccurs="1">
        </xs:element>
        <xs:element name="Type" type="xs:string" minOccurs="0" maxOccurs="1">
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="Transaction">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="Transaction-Header" minOccurs="1" maxOccurs="1" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

I am converting it to JSONSchema (it's a massive xsd) with a lot of success but what I can't seem to figure out is how to incorporate the minOccurs and maxOccurs restrictions.

Essentially, there should always only ever be 1 Transaction-Header
i.e.

{
  "Transaction": {
    "Transaction-Header": {
      "Origin": "Origin",
      "Type": "Type"
    }
  }
}

Invalid:

{
  "Transaction": {
    "Transaction-Header": {
      "Origin": "Origin",
      "Type": "Type"
    },
    "Transaction-Header": {
      "Origin": "Origin",
      "Type": "Type"
    }
  }
}

I understand this can be done by using the array minItems / maxItems functionality, but these are really elements and not arrays and I don't really want to have to define them as such if possible.

Anyone know how (or even if) this is possible please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白云悠悠 2025-02-04 06:06:53

JSON Schema使用JSON文档模型,您的第二个示例不是有效的JSON-属性名称不能出现两次。您必须重命名您的属性,以使其唯一或使用数组。

如果使用数组,则可以使用包含mincontains/maxContains来指定数组中项目的限制。

JSON Schema uses the JSON document model, and your second example is not valid JSON -- a property name cannot appear twice. You'll have to rename your properties so they are unique, or use an array.

If you use an array, you can use contains and minContains/maxContains to specify limitations on items in the array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文