xml语义约束:DTD和Schema

前端之家收集整理的这篇文章主要介绍了xml语义约束:DTD和Schema前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

XML不像HTML那样有一套预置的标签,但是XML有严格的语义约束,主要有两种模式:DTD和Schema

DTD

DTD有三种引用方式:

1.内部引用,DTD只能供一个XML文档使用。

2.外部(SYSTEM)引用,DTD是一个单独的文件,可以供多个XML文档使用。

3.公共(PUBLIC)引用,DTD是一个URL,可以供多个XML使用。

注:一个XML一般只能引入一个DTD。

Schema

按Schema是否指定命名空间,Schema的引用方式分为两种:

1.无命名空间引用。

2.有命名空间引用。

book.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- edited with XMLSpy v2013 (http://www.altova.com) by () -->
  3. <books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:noNamespaceSchemaLocation="file:///E:/XMLSpy/Users/zzj/book.xsd"
  5. xsi:schemaLocation="www.so.com file:///E:/XMLSpy/Users/zzj/movie.xsd
  6. www.baidu.com file:///E:/XMLSpy/Users/zzj/game.xsd"
  7. xmlns:b="www.so.com" xmlns:g="www.baidu.com">
  8. <book>
  9. <name>疯狂XML讲义</name>
  10. <author>李刚</author>
  11. </book>
  12. <book>
  13. <name>疯狂Java讲义</name>
  14. <author>李刚</author>
  15. </book>
  16. <b:movie>
  17. <b:name>卧虎藏龙</b:name>
  18. <b:author>李安</b:author>
  19. </b:movie>
  20. <b:movie>
  21. <b:name>英雄</b:name>
  22. <b:author>张艺谋</b:author>
  23. </b:movie>
  24. <g:game>
  25. <g:name>qq部落</g:name>
  26. <g:author>腾讯</g:author>
  27. </g:game>
  28. <g:game>
  29. <g:name>帝国文明</g:name>
  30. <g:author>腾讯</g:author>
  31. </g:game>
  32. </books>
book.xsd
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--W3C Schema generated by XMLSpy v2013 (http://www.altova.com)-->
  3. <!-- 根元素未指定targetNamespace属性,XML使用无命名空间方式引入 -->
  4. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  5. <xs:element name="name">
  6. <xs:simpleType>
  7. <xs:restriction base="xs:string"/>
  8. </xs:simpleType>
  9. </xs:element>
  10. <xs:element name="books">
  11. <!-- books下面除了可以有book元素,还可以有其他元素,这样可以让books元素引入其他schema定义的元素 -->
  12. <xs:complexType>
  13. <xs:sequence>
  14. <xs:choice minOccurs="0" maxOccurs="unbounded">
  15. <xs:element ref="book" />
  16. <xs:any namespace="##other" />
  17. </xs:choice>
  18. </xs:sequence>
  19. </xs:complexType>
  20. </xs:element>
  21. <xs:element name="book">
  22. <!-- book下面只能有name和author元素 -->
  23. <xs:complexType>
  24. <xs:sequence>
  25. <xs:element ref="name"/>
  26. <xs:element ref="author"/>
  27. </xs:sequence>
  28. </xs:complexType>
  29. </xs:element>
  30. <xs:element name="author">
  31. <xs:simpleType>
  32. <xs:restriction base="xs:string"/>
  33. </xs:simpleType>
  34. </xs:element>
  35. </xs:schema>
movie.xsd
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- 根元素指定了targetNamespace属性,XML使用有命名空间方式引入 -->
  3. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  4. xmlns="www.so.com"
  5. targetNamespace="www.so.com">
  6. <xs:element name="name">
  7. <xs:simpleType>
  8. <xs:restriction base="xs:string"/>
  9. </xs:simpleType>
  10. </xs:element>
  11. <xs:element name="author">
  12. <xs:simpleType>
  13. <xs:restriction base="xs:string"/>
  14. </xs:simpleType>
  15. </xs:element>
  16. <xs:element name="movie">
  17. <xs:complexType>
  18. <xs:sequence>
  19. <xs:element ref="name"/>
  20. <xs:element ref="author"/>
  21. </xs:sequence>
  22. </xs:complexType>
  23. </xs:element>
  24. </xs:schema>
game.xsd
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- 根元素指定了targetNamespace属性,XML使用有命名空间方式引入 -->
  3. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  4. xmlns="www.baidu.com"
  5. targetNamespace="www.baidu.com">
  6. <xs:element name="name">
  7. <xs:simpleType>
  8. <xs:restriction base="xs:string"/>
  9. </xs:simpleType>
  10. </xs:element>
  11. <xs:element name="author">
  12. <xs:simpleType>
  13. <xs:restriction base="xs:string"/>
  14. </xs:simpleType>
  15. </xs:element>
  16. <xs:element name="game">
  17. <xs:complexType>
  18. <xs:sequence>
  19. <xs:element ref="name"/>
  20. <xs:element ref="author"/>
  21. </xs:sequence>
  22. </xs:complexType>
  23. </xs:element>
  24. </xs:schema>
一个XSD文件的目标命名空间(targetNamespace)通常都会指定为一个URL(但并不是必须的),而这个URL通常又会指向这个XSD文件,因为URL是唯一的,这样就保证了XML文档元素和属性的唯一。

注:一个XML文档可以引入多个Schema,但只能引入一个无命名空间的Schema。

猜你在找的XML相关文章