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

北城余情 提交于 2019-12-21 06:23:13

问题


Now I have an XSD file (e.g. FOO_SCHEMA.xsd) that looks something like this:

<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="xs:string" />
                <xs:element name="BAR2" type="xs:string" />
                <xs:element name="BAR3" type="xs:string" />
                <xs:element name="BAR4" type="xs:string" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

I want to modify two of the elements (say BAR1 and BAR2) to use a single time. I know how to use it for one of them,

<xs:element name="BAR1">
   <xs:simpleType>
       <!-- my type definition here -->
   </xs:simpleType>
</xs:element>

But I want to use it in two (and maybe more) elements, and I am not sure how I can do this without copying the same simple type definition everywhere. From googling, it seems that I need to declare the type with a name

But I don't know where to put this declaration. I try to put it in the same level in as xs:complexType and several other level, but it was rejected by the schema validator. Any idea where to put this declaration? Any idea?


回答1:


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>



回答2:


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>


来源:https://stackoverflow.com/questions/13150465/where-to-declare-a-simpletype-so-that-it-can-be-used-in-several-elemets-in-the-s

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