在SimpleType中嵌套simpleType

时间:2014-12-01 21:25:20

标签: xml xsd

下面的代码在验证期间给出了以下错误。这是否意味着我无法在complexType元素中嵌套simpleType元素?

Error - Line 18, 17: org.xml.sax.SAXParseException; lineNumber: 18; columnNumber: 17; s4s-elt-must-match.1: The content of 'sequence' must match (annotation?, (element | group | choice | sequence | any)*). A problem was found starting at: simpleType.

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.example.org/carType"
    elementFormDefault="qualified">

    <element name="carType">
        <complexType>
            <sequence>
                <simpleType name="colour">
                    <restriction base="string">
                        <enumeration value="blue" />
                        <enumeration value="yellow" />
                        <enumeration value="green" />
                        <enumeration value="black" />
                        <enumeration value="white" />
                    </restriction>
                </simpleType>
                <simpleType name="body">
                    <restriction base="string">
                        <enumeration value="sedan" />
                        <enumeration value="hatchback" />
                    </restriction>
                </simpleType>
            </sequence>
        </complexType>
    </element>
</schema>

1 个答案:

答案 0 :(得分:2)

你很亲密。只需对您的XSD进行两次小调整:

  1. 你有<simpleType name="colour">的地方,而是声明一个 colour元素:

        <element name="colour">
          <simpleType>
    
  2. 同样,你有<simpleType name="body">的地方 声明body元素:

        <element name="body">
          <simpleType>
    
  3. 然后:

    <?xml version="1.0" encoding="UTF-8"?>
    <schema xmlns="http://www.w3.org/2001/XMLSchema" 
            targetNamespace="http://www.example.org/carType"
            elementFormDefault="qualified">
      <element name="carType">
        <complexType>
          <sequence>
            <element name="colour">
              <simpleType>
                <restriction base="string">
                  <enumeration value="blue" />
                  <enumeration value="yellow" />
                  <enumeration value="green" />
                  <enumeration value="black" />
                  <enumeration value="white" />
                </restriction>
              </simpleType>
            </element>
            <element name="body">
              <simpleType>
                <restriction base="string">
                  <enumeration value="sedan" />
                  <enumeration value="hatchback" />
                </restriction>
              </simpleType>
            </element>
          </sequence>
        </complexType>
      </element>
    </schema>