Where to declare a simpleType so that it can be used in several elemets in the same XSD?

穿精又带淫゛_ 提交于 2019-12-03 18:02:20

All referenceable components in an XSD file must be placed directly under the xs:schema element. In other words, your global simple type declarations must be siblings of the FOO element.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="FOO">
        <xs:annotation>
            <xs:documentation>Comment</xs:documentation>
        </xs:annotation>
        <xs:complexType>
            <xs:sequence>
                <xs:element name="BAR1" type="BAR" />
                <xs:element name="BAR2" type="BAR" />
                <xs:element name="BAR3" type="xs:string" />
                <xs:element name="BAR4" type="xs:string" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:simpleType name="BAR">
        <xs:restriction base="xs:string">
            <xs:pattern value="BAR"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

Define global and use the type attribute to specify the type.

  <xs:simpleType name="test">
    <xs:annotation>
      <xs:documentation>
        Defines a string that is between 1 and 300 chars
      </xs:documentation>
    </xs:annotation>
    <xs:restriction base="xs:string">
      <xs:minLength value="1" />
      <xs:maxLength value="300" />
    </xs:restriction>
  </xs:simpleType>

  <xs:element name="foo">
    <xs:complexType>
      <xs:attribute type="test" name="bar"/>
    </xs:complexType>   
  </xs:element>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!