请考虑以下架构:

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

第6行ref到“child”失败,而第7行type验证。对于属性,ref成功,type失败。我想知道为什么。
我对ref的理解是,它只是引用了另一个元素,并指定您希望在该位置看到引用类型的实例(定义中给出了名称)。显然我错了,那么ref到底是什么意思?

最佳答案

使用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>

关于xml - XML模式中的ref和type有什么区别?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17422175/

10-11 19:59
查看更多