在 xml 模式中的相同元素名称中选择原始类型和复杂类型?

问题描述 投票:0回答:1

考虑以下 XML:

<CLIENTS>
<CLIENT ID="18">
    <NAME>Tim</NAME>
    <MIDDLE_NAME>Billy</MIDDLE_NAME>
    <SURNAME>Riggins</SURNAME>
    <ADDRESS>
        <STREET>
            Eight St
        </STREET>
        <NUMBER>3</NUMBER>
    </ADDRESS>
    <EMAIL>[email protected]</EMAIL>
    <PHONE_NUMBER>756-2222</PHONE_NUMBER>
</CLIENT>


<CLIENT ID="19">
    <BASIC_INFO>
        <NAME>James</NAME>
        <MIDDLE_NAME>Morgan</MIDDLE_NAME>
        <SURNAME>McGill</SURNAME>
        <ADDRESS>Ninth St.1</ADDRESS>
    </BASIC_INFO>
    <EMAIL>[email protected]</EMAIL>
    <PHONE_NUMBER>756-3333</PHONE_NUMBER>
</CLIENT>
</CLIENTS>

我需要编写 XML Schema。一切顺利,直到我到达 ADDRESS 部分,它可以是简单的字符串或由 2 个元素组成的复杂类型。

我尝试过的:

<xsd:simpleType name="STREET-SIMPLE-TYPE">
    <xsd:restriction base="xsd:string">
        <xsd:pattern value="[A-Za-z]+ St\.[0-9]{1,2}"/>
    </xsd:restriction>
</xsd:simpleType>

<xsd:complexType name="STREET-COMPLEX-TYPE">
    <xsd:sequence>
        <xsd:element name="STREET" type="xsd:string"/>
        <xsd:element name="NUMBER" type="xsd:integer"/>
    </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="STREET-TYPE" mixed="true">
    <xsd:choice>
        <xsd:element name="STREETSIMPLE" type="STREET-SIMPLE-TYPE"/>
        <xsd:element name="STREETCOMPLEX" type="STREET-COMPLEX-TYPE"/>
    </xsd:choice>
</xsd:complexType>

但是,我收到以下错误:

Cvc-complex-type.2.4.b: 元素'ADDRESS'的内容不完整。预计其中之一是“{STREETSIMPLE, STREETCOMPLEX}”

xml xsd
1个回答
0
投票

如果你把选择再往上一层,它就起作用了。

<?xml version="1.0" encoding="utf-16"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="CLIENTS">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element maxOccurs="unbounded" name="CLIENT">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:choice minOccurs="0">
                <xsd:element minOccurs="0" name="BASIC_INFO">
                  <xsd:complexType>
                    <xsd:sequence>
                      <xsd:element name="NAME" type="xsd:string" />
                      <xsd:element name="MIDDLE_NAME" type="xsd:string" />
                      <xsd:element name="SURNAME" type="xsd:string" />
                      <xsd:element name="ADDRESS">
                        <xsd:simpleType>
                          <xsd:restriction base="STREET-SIMPLE-TYPE" />
                        </xsd:simpleType>
                      </xsd:element>
                    </xsd:sequence>
                  </xsd:complexType>
                </xsd:element>
                <xsd:sequence>
                  <xsd:element name="NAME" type="xsd:string" />
                  <xsd:element name="MIDDLE_NAME" type="xsd:string" />
                  <xsd:element name="SURNAME" type="xsd:string" />
                  <xsd:element name="ADDRESS">
                    <xsd:complexType>
                      <xsd:complexContent mixed="false">
                        <xsd:extension base="STREET-COMPLEX-TYPE" />
                      </xsd:complexContent>
                    </xsd:complexType>
                  </xsd:element>
                </xsd:sequence>
              </xsd:choice>
              <xsd:element name="EMAIL" type="xsd:string" />
              <xsd:element name="PHONE_NUMBER" type="xsd:string" />
            </xsd:sequence>
            <xsd:attribute name="ID" type="xsd:unsignedByte" use="required" />
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
  <xsd:simpleType name="STREET-SIMPLE-TYPE">
    <xsd:restriction base="xsd:string">
      <xsd:pattern value="[A-Za-z]+ St\.[0-9]{1,2}" />
    </xsd:restriction>
  </xsd:simpleType>
  <xsd:complexType name="STREET-COMPLEX-TYPE">
    <xsd:sequence>
      <xsd:element name="STREET" type="xsd:string" />
      <xsd:element name="NUMBER" type="xsd:integer" />
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>
© www.soinside.com 2019 - 2024. All rights reserved.