开发者

Is it the correct way to create XML schema? Please suggest

开发者 https://www.devze.com 2023-03-10 03:37 出处:网络
1: <xs:element name=\"abcd\" minOccurs=\"1\" maxOccurs=\"开发者_JAVA技巧1\"/> <xs:simpleType>

1:

  <xs:element name="abcd" minOccurs="1" maxOccurs="开发者_JAVA技巧1"/>
    <xs:simpleType>
     <xs:restriction base="xs:string">
      <xs:length value="4"/>
    </xs:restriction>
   </xs:simpleType>
  </xs:element>

2:

<xs:element name="abcd" type="xs:string" length value = "4" minOccurs="1" maxOccurs="1"/>

Can I add maxOccurs and minOccurs as I have done in number 1. Is number 2 also correct way to define schema?


Your first code example has a small syntax error. The <xs:element> start tag has a slash / in the end which makes it a self-closing element and your XML would be malformed. Removing the slash fixes that error.

<xs:element name="abcd" minOccurs="1" maxOccurs="1"/>
  <xs:simpl...
</xs:element>

Yes, you can generally add minOccurs and maxOccurs as you have done, but this is not allowed for global element definitions (which means that <xs:element> is a (direct) child of the <xs:schema> element). The default values both for minOccurs and maxOccurs is 1, so in your example code the meaning would stay the same even if they were left out.

Your second example contains an error.

<xs:element name="abcd" type="xs:string" length value = "4" minOccurs="1" maxOccurs="1"/>

length is not a valid attribute for element <xs:element> Also the XML recommendation does not allow spaces before or after the equals character = but not all parsers complain about this. Otherwise it seems correct.

0

精彩评论

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