【问题标题】:How can I represent a sequence of an element with coupled attribute types in XSD?如何在 XSD 中表示具有耦合属性类型的元素序列?
【发布时间】:2011-03-10 02:30:07
【问题描述】:

我有以下要求,并正在尝试确定如何最好地对 XSD 建模以表示这些要求。

我有很多 XML 元素的实例,比如<box>。每个<box> 都有一个必需的属性t="[box-type]",每个具有特定类型的盒子,比如t="tall" 有另一个必需的属性v="10",它表示高盒子的高度。所有<box>es 都具有tv 属性,但对其v 属性接受哪些值的限制取决于其t 属性的值。

以下面的 XML 为例:

<box t="tall" v="10"/>
<box t="named" v="George"/>
<box t="colored" v="green"/>

现在,在我的 XSD 中,我需要能够表示这些元素的序列。我的想法是做类似以下的事情,它只列出我序列中所有允许的框类型(在以下 sn-p 的末尾):

<xsd:simpleType name="box_types">
    <xsd:restriction base="xsd:token">
        <xsd:enumeration value="tall" />
        <xsd:enumeration value="named" />
        <xsd:enumeration value="colored" />
    </xsd:restriction>
</xsd:simpleType>

<!--Box base-->
<xsd:complexType name="box_type">
    <xsd:attribute name="t" use="required" type="box_types"/>
    <xsd:attribute name="v" use="required"/>
</xsd:complexType>

<!--Box concrete types-->
<xsd:complexType name="tall_box_type">
    <xsd:complexContent>
        <xsd:extension base="box_type">
            <xsd:attribute name="t" fixed="tall" use="required"/>
            <xsd:attribute name="v" type="xsd:int" use="required"/>
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="named_box_type">
    <xsd:complexContent>
        <xsd:extension base="box_type">
            <xsd:attribute name="t" fixed="named" use="required"/>
            <xsd:attribute name="v" type="xsd:string" use="required"/>
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="colored_box_type">
    <xsd:complexContent>
        <xsd:extension base="box_type">
            <xsd:attribute name="t" fixed="colored" use="required"/>
            <xsd:attribute name="v" type="xsd:token" use="required"/>
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>

<!--And finally, the place where boxes show up-->
<xsd:complexType name="box_usage">
    <xsd:sequence>
        <xsd:element name="box" type="tall_box_type"/>
        <xsd:element name="box" type="named_box_type"/>
        <xsd:element name="box" type="colored_box_type"/>
    </xsd:sequence>
</xsd:complexType>

不幸的是,这不是一个有效的 XSD - VS 给了我几个错误,最不幸的是Elements with the same name and in the same scope must have the same type。关于如何在 XSD 中表示这些 t/v 耦合属性限制的任何建议?

【问题讨论】:

    标签: xsd


    【解决方案1】:

    XML Schema 1.0 无法验证值之间的依赖关系。您的选择是:

    1. 更改您的 XML。例如,使用tallBoxcolorBoxnameBox 作为元素名称。
    2. 使用 XSD 验证一般结构并使用程序逻辑(或其他一些工具,如 Schematron 或 XSLT 样式表)验证值。
    3. 使用 XML Schema 1.1,它可以验证值约束,但目前还不普遍支持。

    【讨论】:

      猜你喜欢
      • 2020-06-14
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-23
      • 1970-01-01
      • 2023-03-30
      相关资源
      最近更新 更多