考虑以下模式:
- <?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行对“Child”的引用失败,而第7行的类型验证.对于属性,ref会在类型失败时成功.我正在想明白为什么
我对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>