22

考虑以下架构:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:complexType name="Root">
        <xs:sequence>
            <xs:element ref="Child" />
            <xs:element name="Child2" type="Child" />
        </xs:sequence>
        <xs:attribute ref="Att" />
        <xs:attribute name="Att2" type="Att" />
    </xs:complexType>

    <xs:complexType name="Child">
        <xs:attribute ref="Att" />
    </xs:complexType>

    <xs:attribute name="Att" type="xs:integer" />

</xs:schema> 

ref6 行的 to "Child" 失败,而第type7 行的验证通过。对于属性,ref成功而type失败。我试图理解为什么。

我的理解ref是它只是引用另一个元素并指定您希望在该位置看到引用类型的实例(具有定义中给出的名称)。显然我错了,那么ref实际上是什么意思?

4

3 回答 3

22

使用 ref=".." 您正在“粘贴”在另一个地方定义的现有元素/属性。使用 type=".." 您正在为新元素/属性分配一些结构(在 complextype/simpletype 中定义)。请看以下内容:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tst="test" targetNamespace="test">

    <xs:complexType name="Root">
        <xs:sequence>
            <xs:element ref="tst:Child" />
            <xs:element name="Child2" type="tst:ChildType" />
        </xs:sequence>
        <xs:attribute ref="tst:AttRef" />
        <xs:attribute name="Att2" type="tst:AttType" />
    </xs:complexType>

    <xs:complexType name="ChildType">
        <xs:attribute ref="tst:AttRef" />
    </xs:complexType>

    <xs:element name="Child">
    </xs:element>

    <xs:simpleType name="AttType">
        <xs:restriction base="xs:string">
            <xs:maxLength value="10" />
        </xs:restriction>
    </xs:simpleType>

    <xs:attribute name="AttRef" type="xs:integer" />

</xs:schema> 
于 2013-07-02T10:11:56.767 回答
3

在内容模型中,xs:element元素可以是:

  1. 元素声明(给出元素的名称和类型)或
  2. 对顶级元素声明的引用(给出元素的名称作为识别它的一种方式,并遵循该类型的实际声明)。

(相同的名称/引用交替适用于属性声明和属性引用,并且内联类型定义和对命名类型的引用之间存在类似的二分法。)

在您的示例中,名为 Child 的元素没有顶级声明,因此该ref属性失败。我希望这有帮助。

于 2013-07-02T15:21:13.077 回答
1

ref 和 type 之间的显着区别在于,使用 type 你不能再往下一层,但是使用 ref,你可以使用一个可以有额外层次的元素等等。

于 2016-08-13T20:56:34.300 回答