xml xsd 编写 第一课, 简单的引入和限制

前端之家收集整理的这篇文章主要介绍了xml xsd 编写 第一课, 简单的引入和限制前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.shiporder.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3.  
  4. <!-- 这里的id是六位数 -->
  5. <shiporder orderid="889923"
  6. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  7. xsi:noNamespaceSchemaLocation="shiporder.xsd">
  8. <orderperson>John Smith</orderperson>
  9. <shipto>
  10. <name>Ola Nordmann</name>
  11. <address>Langgt 23</address>
  12. <city>4000 Stavanger</city>
  13. <country>Norway</country>
  14. </shipto>
  15. <item>
  16. <title>Empire Burlesque</title>
  17. <note>Special Edition</note> <!-- 这里可以没有,如下元素 -->
  18. <quantity>1</quantity>
  19. <price>10.90</price>
  20. </item>
  21. <item>
  22. <title>Hide your heart</title>
  23. <quantity>1</quantity>
  24. <price>9.90</price>
  25. </item>
  26. </shiporder>

2.shiporder.xsd 全面

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  3.  
  4. <xs:simpleType name="stringtype">
  5. <xs:restriction base="xs:string"/>
  6. </xs:simpleType>
  7.  
  8. <xs:simpleType name="inttype">
  9. <xs:restriction base="xs:positiveInteger"/>
  10. </xs:simpleType>
  11.  
  12. <xs:simpleType name="dectype">
  13. <xs:restriction base="xs:decimal"/>
  14. </xs:simpleType>
  15.  
  16. <xs:simpleType name="orderidtype"> <!-- 限定id6位0到9的数 -->
  17. <xs:restriction base="xs:string">
  18. <xs:pattern value="[0-9]{6}"/>
  19. </xs:restriction>
  20. </xs:simpleType>
  21.  
  22. <xs:complexType name="shiptotype">
  23. <xs:sequence>
  24. <xs:element name="name" type="stringtype"/>
  25. <xs:element name="address" type="stringtype"/>
  26. <xs:element name="city" type="stringtype"/>
  27. <xs:element name="country" type="stringtype"/>
  28. </xs:sequence>
  29. </xs:complexType>
  30.  
  31. <xs:complexType name="itemtype"> <!-- 声明一个复杂类型 -->
  32. <xs:sequence>
  33. <xs:element name="title" type="stringtype"/>
  34. <xs:element name="note" type="stringtype" minOccurs="0"/> <!-- 最少可以是0个,也就是不用填写! -->
  35. <xs:element name="quantity" type="inttype"/>
  36. <xs:element name="price" type="dectype"/>
  37. </xs:sequence>
  38. </xs:complexType>
  39.  
  40. <xs:complexType name="shipordertype"> <!-- 声明一个复杂类型 -->
  41. <xs:annotation>
  42. <xs:documentation>
  43. 我是一个说明文件位于shipordertype下面
  44. </xs:documentation>
  45. </xs:annotation>
  46. <xs:sequence>
  47. <xs:element name="orderperson" type="stringtype"/>
  48. <xs:element name="shipto" type="shiptotype"/>
  49. <xs:element name="item" maxOccurs="2" type="itemtype"/> <!-- 允许出现两个item -->
  50. </xs:sequence>
  51. <xs:attribute name="orderid" type="orderidtype" use="required"> <!-- 指定属性 -->
  52. <xs:annotation>
  53. <xs:documentation>我是一个位数字orderidtype</xs:documentation>
  54. </xs:annotation>
  55. </xs:attribute>
  56. </xs:complexType>
  57. <xs:element name="shiporder" type="shipordertype">
  58. <xs:annotation>
  59. <xs:documentation>
  60. 我是一个说明文件位于shiporder下面
  61. </xs:documentation>
  62. </xs:annotation>
  63. </xs:element>
  64.  
  65. </xs:schema>

3.简单的引入

  1. <?xml version="1.0" encoding="ISO-8859-1" ?>
  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  3.  
  4. <!-- definition of simple elements -->
  5. <xs:element name="orderperson" type="xs:string"/>
  6. <xs:element name="name" type="xs:string"/>
  7. <xs:element name="address" type="xs:string"/>
  8. <xs:element name="city" type="xs:string"/>
  9. <xs:element name="country" type="xs:string"/>
  10. <xs:element name="title" type="xs:string"/>
  11. <xs:element name="note" type="xs:string"/>
  12. <xs:element name="quantity" type="xs:positiveInteger"/>
  13. <xs:element name="price" type="xs:decimal"/>
  14.  
  15. <!-- definition of attributes -->
  16. <xs:attribute name="orderid" type="xs:string"/>
  17.  
  18. <!-- definition of complex elements -->
  19. <xs:element name="shipto">
  20. <xs:complexType>
  21. <xs:sequence>
  22. <xs:element ref="name"/>
  23. <xs:element ref="address"/>
  24. <xs:element ref="city"/>
  25. <xs:element ref="country"/>
  26. </xs:sequence>
  27. </xs:complexType>
  28. </xs:element>
  29.  
  30. <xs:element name="item">
  31. <xs:complexType>
  32. <xs:sequence>
  33. <xs:element ref="title"/>
  34. <xs:element ref="note" minOccurs="0"/>
  35. <xs:element ref="quantity"/>
  36. <xs:element ref="price"/>
  37. </xs:sequence>
  38. </xs:complexType>
  39. </xs:element>
  40.  
  41. <xs:element name="shiporder">
  42. <xs:complexType>
  43. <xs:sequence>
  44. <xs:element ref="orderperson"/>
  45. <xs:element ref="shipto"/>
  46. <xs:element ref="item" maxOccurs="unbounded"/>
  47. </xs:sequence>
  48. <xs:attribute ref="orderid" use="required"/>
  49. </xs:complexType>
  50. </xs:element>
  51.  
  52. </xs:schema>

猜你在找的XML相关文章