如何使用一组属性扩展内置类型?

时间:2014-07-31 13:07:32

标签: xsd

我需要使用相同的两个属性xs:anyURI和{{1}扩展不同XML Schema内置类型的多个xml元素(例如xs:stringatt1 ...)一遍又一遍。

要验证的xml示例:

att2

我尝试过定义复杂类型的父级:

<extendedURI att1="abc" att2="def">my.uri</extendedURI>

然后将我的元素类型声明为<!-- The parent type defines the two attributes I'm interested in --> <xs:complexType name="parentType"> <xs:attribute name="att1" type="xs:string" /> <xs:attribute name="att2" type="xs:string" /> </xs:complexType> 并扩展它们,但这是无效的:

xs:anyURI

我还尝试将simpleContent限制为<!-- XXX: The following is not permitted: I cannot redefine the xs:anyURI type --> <xs:element name="extendedURI" type="xs:anyURI"> <xs:complexType> <xs:complexContent> <xs:extension base="parentType" /> </xs:complexContent> </xs:complexType> </xs:element> 并将其扩展为xs:anyURI,但我无法找到。类似的东西:

parentType

那么我怎么能做到这一点?是否可以使用complexType扩展内置类型(或使用内置类型的定义限制complexType)?

1 个答案:

答案 0 :(得分:1)

可以使用xsd:attributeGroup

重复使用同一组属性
<!-- several types will 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>

<!-- extending a simple content element with the two 'inherited' attributes -->
<xs:element name="extendedURI">
    <xs:complexType>
        <xs:simpleContent>
            <xs:extension base="xs:anyURI">
                <xs:attributeGroup ref="extensible"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
</xs:element>