тип элемента xsd, основанный на количестве вхождений - PullRequest
0 голосов
/ 07 апреля 2020

У меня есть следующая xml

<Root>
    <custom_fields>
        <name_key>additional_locations</name_key>
            <value>California</value>
            <value>Texas</value>
    </custom_fields>
    <custom_fields>
        <name_key>cost_center</name_key>
        <value>IT</value>
    </custom_fields>
</Root>

Я хотел бы создать схему XML для генерации следующей схемы JSON Schema

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "Root": {
      "type": "object",
      "properties": {
        "custom_fields": {
          "type": "array",
          "items": [
            {
              "type": "object",
              "properties": {
                "name_key": {
                  "type": "string"
                },
                "value": {
                  "type": "array",
                  "items": [
                    {
                      "type": "string"
                    },
                    {
                      "type": "string"
                    }
                  ]
                }
              },
              "required": [
                "name_key",
                "value"
              ]
            },
            {
              "type": "object",
              "properties": {
                "name_key": {
                  "type": "string"
                },
                "value": {
                  "type": "string"
                }
              },
              "required": [
                "name_key",
                "value"
              ]
            }
          ]
        }
      },
      "required": [
        "custom_fields"
      ]
    }
  },
  "required": [
    "Root"
  ]
}

JSON дает следующее JSON

{
    "Root": {
        "custom_fields": [
            {
                "name_key": "additional_locations",
                "value": [
                    "California",
                    "Texas"
                ]
            },
            {
                "name_key": "cost_center",
                "value": "IT"
            }
        ]
    }
}

Я пробовал что-то вроде этого

<xs:schema attributeFormDefault="unqualified"
    elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="Root">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="custom_fields" maxOccurs="unbounded"
                    minOccurs="0">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element type="xs:string" name="name_key" />
                            <xs:choice>
                                <xs:element type="xs:string" name="value" maxOccurs="1"
                                    minOccurs="0" />
                                <xs:element type="xs:string" name="value" maxOccurs="unbounded"
                                    minOccurs="2" />
                            </xs:choice>        
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Любая помощь высоко ценится, спасибо.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...