【问题标题】:How to extend built-in types with a set of attributes?如何使用一组属性扩展内置类型?
【发布时间】:2014-07-31 13:07:31
【问题描述】:

我需要使用相同的两个属性 att1att2 一遍又一遍地扩展不同 XML Schema 内置类型(例如 xs:anyURIxs:string...)的几个 xml 元素。

要验证的示例 xml:

<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 并扩展它们,但这无效:

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

我还尝试将 simpleContent 限制为 xs:anyURI 并使用 parentType 扩展它,但我找不到如何操作。比如:

<xs:element name="extendedURI">
    <xs:complexType>
        <xs:simpleContent>
            <!-- XXX: This is not permited: -->
            <xs:restriction base="xs:anyURI">
            </xs:restriction>
            <xs:extension base="parentType">
            </xs:extension>
            <!-- 'xs:restriction' and 'xs:extension' are alternatives and
            cannot be defined at the same time!
            -->
        <xs:simpleContent>
    </xs:complexType>
</xs:element>

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

【问题讨论】:

标签: xsd


【解决方案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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-18
    • 2021-04-07
    • 2011-06-22
    • 2018-04-11
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    相关资源
    最近更新 更多