参考和类型在XML模式有什么区别?

前端之家收集整理的这篇文章主要介绍了参考和类型在XML模式有什么区别?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
考虑以下模式:
  1. <?xml version="1.0" encoding="ISO-8859-1" ?>
  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  3.  
  4. <xs:complexType name="Root">
  5. <xs:sequence>
  6. <xs:element ref="Child" />
  7. <xs:element name="Child2" type="Child" />
  8. </xs:sequence>
  9. <xs:attribute ref="Att" />
  10. <xs:attribute name="Att2" type="Att" />
  11. </xs:complexType>
  12.  
  13. <xs:complexType name="Child">
  14. <xs:attribute ref="Att" />
  15. </xs:complexType>
  16.  
  17. <xs:attribute name="Att" type="xs:integer" />
  18.  
  19. </xs:schema>

第6行对“Child”的引用失败,而第7行的类型验证.对于属性,ref会在类型失败时成功.我正在想明白为什么

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

使用ref =“..”,您将“粘贴”在另一个地方定义的现有元素/属性.使用type =“..”,您将一些结构(在complextype / simpletype中定义)分配给新的元素/属性.看看下面:
  1. <?xml version="1.0" encoding="ISO-8859-1" ?>
  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tst="test" targetNamespace="test">
  3.  
  4. <xs:complexType name="Root">
  5. <xs:sequence>
  6. <xs:element ref="tst:Child" />
  7. <xs:element name="Child2" type="tst:ChildType" />
  8. </xs:sequence>
  9. <xs:attribute ref="tst:AttRef" />
  10. <xs:attribute name="Att2" type="tst:AttType" />
  11. </xs:complexType>
  12.  
  13. <xs:complexType name="ChildType">
  14. <xs:attribute ref="tst:AttRef" />
  15. </xs:complexType>
  16.  
  17. <xs:element name="Child">
  18. </xs:element>
  19.  
  20. <xs:simpleType name="AttType">
  21. <xs:restriction base="xs:string">
  22. <xs:maxLength value="10" />
  23. </xs:restriction>
  24. </xs:simpleType>
  25.  
  26. <xs:attribute name="AttRef" type="xs:integer" />
  27.  
  28. </xs:schema>

猜你在找的XML相关文章