如何在XSD中表示具有耦合属性类型的元素序列?
我有以下需求,并试图确定如何最好地建模XSD来表示这些需求 我有很多XML元素的实例,比如如何在XSD中表示具有耦合属性类型的元素序列?,xsd,Xsd,我有以下需求,并试图确定如何最好地建模XSD来表示这些需求 我有很多XML元素的实例,比如。每个都有一个必需的属性t=“[box type]”,每个具有特定类型的框,比如说t=“toll”都有另一个必需的属性v=“10”,它表示高框的高度。所有E都具有t和v属性,但对其v属性接受哪些值的限制取决于其t属性的值 例如,以以下XML为例: <box t="tall" v="10"/> <box t="named" v="George"/> <box t="colored
。每个
都有一个必需的属性t=“[box type]”
,每个具有特定类型的框,比如说t=“toll”
都有另一个必需的属性v=“10”
,它表示高框的高度。所有
E都具有t
和v
属性,但对其v
属性接受哪些值的限制取决于其t
属性的值
例如,以以下XML为例:
<box t="tall" v="10"/>
<box t="named" v="George"/>
<box t="colored" v="green"/>
现在,在我的XSD中,我需要能够表示这样的元素序列。我的想法是做如下的事情,列出我序列中所有允许的框类型(在以下代码段的末尾):
不幸的是,这不是一个有效的XSD-VS给了我几个错误,最不幸的是具有相同名称和相同范围的
元素必须具有相同的类型。关于如何在XSD中表示这些t/v耦合属性限制的任何建议?XMLSchema 1.0无法验证值之间的依赖关系。你的选择是:
更改您的XML。例如,使用tallBox
、colorBox
和nameBox
作为元素名称
用XSD验证一般结构,用程序逻辑(或其他一些工具,如Schematron或XSLT样式表)验证值
使用XMLSchema1.1,它可以验证值约束,但目前还不普遍支持
XML架构1.0无法验证值之间的依赖关系。你的选择是:
更改您的XML。例如,使用tallBox
、colorBox
和nameBox
作为元素名称
用XSD验证一般结构,用程序逻辑(或其他一些工具,如Schematron或XSLT样式表)验证值
使用XMLSchema1.1,它可以验证值约束,但目前还不普遍支持
<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>