要验证以下元素:
<population class="AAA">100</population >
我想要在文本节点上的约束是它应该是一个介于1和1000之间的数值。
我的想法看起来像这样,但它不起作用
<xsd:element name="population">
<xsd:complexType>
<xsd:simpleContent>
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="0"/>
<xsd:maxInclusive value="1000"/>
</xsd:restriction>
</xsd:simpleContent>
<xsd:attribute name="class" type="xsd:string" use="required"/>
</xsd:complexType>
顺便说一下,我不想再定义任何新类型了。 谁能帮我。谢谢
答案 0 :(得分:1)
不添加新类型是不可能的;你不能同时延伸和限制。
答案 1 :(得分:1)
受到Petru Gardea's answer to XSD custom type with attribute and restriction的启发,我提出了一个适合您的解决方案。您的元素必须是complexType,它使用以下属性扩展受限制的simpleType:
<!-- simple type that we want to extend with an attribute -->
<xs:simpleType name="populationType">
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="1000"/>
</xs:restriction>
</xs:simpleType>
<!-- extending a simple content element with an attribute -->
<xs:element name="population">
<xs:complexType>
<xs:simpleContent>
<!-- populationType is a simple type -->
<xs:extension base="populationType">
<xs:attribute name="class" type="xs:string" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
此外,如果您想使用同一组属性扩展多个类型,则可以使用xsd:attributeGroup
中建议的
C. M. Sperberg-McQueen's answer to XSD: Adding attributes to strongly-typed “simple” elements:
<!-- several types declare this set of attributes -->
<xs:attributeGroup name="extensible">
<xs:attribute name="att1" type="xs:string" />
<xs:attribute name="att2" type="xs:string" />
</xs:attributeGroup>
<!-- simple type that we want to extend with attributes -->
<xs:simpleType name="populationType">
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="1000"/>
</xs:restriction>
</xs:simpleType>
<!-- extending a simple content element with two 'inherited' attributes -->
<xs:element name="population">
<xs:complexType>
<xs:simpleContent>
<!-- populationType is a simple type -->
<xs:extension base="populationType">
<xs:attributeGroup ref="extensible"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
这将验证以下xml元素:
<population att1="AAA" att2="BBB">100</population >