开发者

How can I add max length, required attributes: Yes in XML schema?

I know there are two ways to define simple elements in XML schema. How can I add only maxlength and required attribute YES to simple element definition. in the following two examples.

<xs:element name="Xyz">
    <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
            <xs:maxLength value="4"/>
        </xs:restriction>
    &开发者_如何学运维lt;/xs:simpleType>
</xs:element>

<xs:element name="Xyz" type="xs:string" minOccurs="0" maxOccurs="1"/>


Define your restricted text content as a global (=named) <xs:simpleType> then use this as a base type for <xs:extension> that you need when create a new type by extension to add the attribute.

Type definition of an element that has attributes must be <xs:complexType>. Then again if the element content can be only text or attributes but not elements, the content must be defined as <xs:simpleContent>. Sample code below.

  <!-- definition of the restricted string -->
  <xs:simpleType name="restrictedLength">
    <xs:restriction base="xs:string">
      <xs:maxLength value="4" />
    </xs:restriction>
  </xs:simpleType>

  <!-- definition for the element with an attribute and text content -->
  <xs:element name="Xyz">
    <xs:complexType>
      <xs:simpleContent>
        <xs:extension base="restrictedLength">
          <xs:attribute name="YES" use="required"/>
        </xs:extension>
      </xs:simpleContent>
    </xs:complexType>
  </xs:element>

For more complete explanation on extending simple content elements with attribute see this:
http://www.xml.com/pub/a/2001/08/22/easyschema.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜