我知道有两种方法可以在XML模式中定义简单元素。如何在简单元素定义中仅添加maxlength和required属性YES。在以下两个例子中。
<xs:element name="Xyz">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="4"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Xyz" type="xs:string" minOccurs="0" maxOccurs="1"/>
答案 0 :(得分:2)
将受限制的文本内容定义为全局(=已命名)<xs:simpleType>
,然后将其用作<xs:extension>
的基本类型,以便在按扩展名创建新类型时添加属性。
具有属性的元素的类型定义必须为<xs:complexType>
。然后,如果元素内容只能是文本或属性而不是元素,则必须将内容定义为<xs:simpleContent>
。示例代码如下。
<!-- 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>
有关使用属性扩展简单内容元素的更完整说明,请参阅:
http://www.xml.com/pub/a/2001/08/22/easyschema.html