在XML模式(XSD)中引用多个ID

时间:2011-08-31 09:18:02

标签: xml namespaces xsd schema validating

我想知道是否可以区分XML模式中的ID字段。我有以下架构:

<element name="define_apple">
    <complexType>
        <attribute name="colour" type="ID"/>
    </complexType>
</element>

<element name="define_orange">
    <complexType>
        <attribute name="colour" type="ID"/>
    </complexType>
</element>

<element name="reference_apple">
    <complexType>
        <attribute name="colour" type="IDREF"/>
    </complexType>
</element>

<element name="reference_orange">
    <complexType>
        <attribute name="colour" type="IDREF"/>
    </complexType>
</element>

但是,引用并不是唯一链接到各自的定义。我仍然可以编写以下废话但有效的XML:

<define_apple colour="green"/>
<define_orange colour="orange"/>

<reference_apple colour="orange"/>
<reference_orange colour="green"/>

有没有办法使用ID和IDREF正确链接字段,例如使用命名空间?我知道我可以使用key和keyref,但ID对我来说更有吸引力。

1 个答案:

答案 0 :(得分:1)

不,我不认为这是可能的。 http://www.w3.org/TR/xmlschema-2/#IDhttp://www.w3.org/TR/xmlschema-2/#IDREF表示ID和IDREF属性类型来自XML标准,而http://www.w3.org/TR/2000/WD-xml-2e-20000814#NT-TokenizedType表示IDREF的有效性限制只是为了匹配文档中的某些ID。我想ID和IDREF主要是XML Schema,以便向后兼容DTD。