XSD:如何派生一个简单类型以向其添加属性并限制其可接受的值 [英] XSD: How to derive a simpletype both to add a attribute to it and to restrict the acceptable value of it

查看:29
本文介绍了XSD:如何派生一个简单类型以向其添加属性并限制其可接受的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如验证以下元素:

 <population class="AAA">100</population > 

我想要对文本节点的约束是它应该是一个介于 1 和 1000 之间的数值.

The constraint I want on the text node is that it should be a numeric value, between 1 and 1000.

我的想法看起来像这样,但行不通

my idea looks like this but it doen't work

 <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>

顺便说一下,我不想再定义任何新类型.谁能帮我.谢谢

By the way, I do not want to define any more new type. Can anyone help me. thank you

推荐答案

使用属性扩展简单类型

灵感来自 Petru Gardea 对XSD 自定义类型与属性的回答和限制,我想出了一个适合你的解决方案.您的元素必须是一个 complexType,它扩展了受限制的 simpleType 并带有一个属性:

Extending a simple type with attributes

Inspired by Petru Gardea's answer to XSD custom type with attribute and restriction, I have come up with a solution that should work for you. Your element must be a complexType extending the restricted simpleType with an attribute:

<!-- 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:attributeGroupC. M. Sperberg-McQueen 对 XSD 的回答:向强类型简单"元素添加属性:

<!-- 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 元素:

This would validate the following xml element:

<population att1="AAA" att2="BBB">100</population >

这篇关于XSD:如何派生一个简单类型以向其添加属性并限制其可接受的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆