Schema语法

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

XSD元素

定义元素的语法:

  1. <xs:element name="xxx" type="yyy"/>

Schema常用类型:

  1. xs:string
  2. xs:decimal
  3. xs:integer
  4. xs:boolean
  5. xs:date
  6. xs:time

例子:

这是一些XML元素:

  1. <lastname>Refsnes</lastname>
  2. <age>36</age>
  3. <dateborn>1970-03-27</dateborn>

元素的定义:

  1. <xs:element name="lastname" type="xs:string"/>
  2. <xs:element name="age" type="xs:integer"/>
  3. <xs:element name="dateborn" type="xs:date"/>

元素的默认值和固定值

元素可拥有指定的默认值或固定值。
当没有其他的值被规定时,默认值就会自动分配给元素。
下面的例子中,缺省值是”red”

  1. <xs:element name="color" type="xs:string" default="red"/>

固定值同样会自动分配给元素,并且无法规定另外一个值

  1. <xs:element name="color" type="xs:string" fixed="red"/>

XSD属性

定义属性的语法:

  1. <xs:attribute name="xxx" type="yyy"/>

可选的属性和必须的属性

在缺省的情况下,属性是可选的。如果规定属性为必选,使用”use”属性

  1. <xs:attribute name="lang" type="xs:string" use="required"/>


XML示例

示例1:

Myfamily.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <persons xmlns="http://www.sync.sz" xmlns:child="http://www.sync.sz.children" xmlns:gender="http://www.sync.sz.attr" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sync.sz family.xsd http://www.sync.sz.attr attribute.xsd">
  3.  
  4. <person>
  5. <firstname>your</firstname>
  6. <lastname>father</lastname>
  7. </person>
  8.  
  9. <person>
  10. <firstname>baba</firstname>
  11. <lastname>nide</lastname>
  12. <child:children>
  13. <child:childname>sson</child:childname>
  14. </child:children>
  15. </person>
  16.  
  17. <person gender:gender="female">
  18. <firstname>yours</firstname>
  19. <lastname>fa</lastname>
  20. <child:children>
  21. <child:childname>sons1</child:childname>
  22. </child:children>
  23. </person>
  24.  
  25.  
  26. </persons>

family.xsd:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.sync.sz" elementFormDefault="qualified">
  3. <xs:element name="persons">
  4. <xs:complexType>
  5. <xs:annotation>
  6. <xs:documentation source="description">
  7. any元素使我们有能力通过未被 schema 规定的元素来拓展 XML 文档!
  8. anyAttribute 元素使我们有能力通过未被 schema 规定的属性来扩展 XML 文档!
  9. </xs:documentation>
  10. </xs:annotation>
  11. <xs:all>
  12. <xs:element name="person">
  13. <xs:complexType>
  14. <xs:sequence>
  15. <xs:element name="firstname" type="xs:string"/>
  16. <xs:element name="lastname" type="xs:string"/>
  17. <xs:any minOccurs="0"/>
  18. </xs:sequence>
  19. <xs:anyAttribute/>
  20. </xs:complexType>
  21. </xs:element>
  22. </xs:all>
  23. </xs:complexType>
  24. </xs:element>
  25.  
  26. </xs:schema>

clilden.xsd

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.sync.sz.children" elementFormDefault="qualified">
  3.  
  4. <xs:element name="children">
  5. <xs:complexType>
  6. <xs:sequence>
  7. <xs:element name="childname" type="xs:string" maxOccurs="unbounded"/>
  8. </xs:sequence>
  9. </xs:complexType>
  10. </xs:element>
  11.  
  12.  
  13. </schema>

attribute.xsd:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.sync.sz.attr" elementFormDefault="qualified">
  3.  
  4. <xs:attribute name="gender">
  5. <xs:simpleType>
  6. <xs:restriction base="xs:string">
  7. <xs:pattern value="male|female"/>
  8. </xs:restriction>
  9. </xs:simpleType>
  10. </xs:attribute>
  11.  
  12.  
  13. </xs:schema>


示例2:

shiporder_refactor.xml:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2.  
  3. <!--xmlns后面直接接uri,使用默认命名空间-->
  4. <shiporder xmlns="http://www.sync.sz" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sync.sz shiporder_refactor.xsd" orderid="a1">
  5.  
  6. <orderperson>synczzz</orderperson>
  7.  
  8. <shipto>
  9. <name>aaa</name>
  10. <address>shenzhenbaoanqu</address>
  11. <city>shenzhen</city>
  12. <country>china</country>
  13. </shipto>
  14.  
  15. <item>
  16. <title>abca</title>
  17. <quantity>12</quantity>
  18. <price>99.9</price>
  19. </item>
  20. <item>
  21. <title>asd</title>
  22. <quantity>12</quantity>
  23. <price>99.9</price>
  24. </item>
  25. <item>
  26. <title>aasdasdbca</title>
  27. <quantity>12</quantity>
  28. <price>99.9</price>
  29. </item>
  30.  
  31. </shiporder>

shiporder_refactor.xsd:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.sync.sz" elementFormDefault="qualified">
  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">
  17. <xs:annotation>
  18. <xs:documentation source="description">简单类型元素</xs:documentation>
  19. </xs:annotation>
  20. <xs:restriction base="xs:string">
  21. <xs:pattern value="[0-9]{6}"/>
  22. </xs:restriction>
  23. </xs:simpleType>
  24.  
  25. <xs:complexType name="shiptotype">
  26. <xs:annotation>
  27. <xs:documentation source="description">复杂类型元素</xs:documentation>
  28. </xs:annotation>
  29. <xs:sequence>
  30. <xs:element name="name" type="stringtype"/>
  31. <xs:element name="address" type="stringtype"/>
  32. <xs:element name="city" type="stringtype"/>
  33. <xs:element name="country" type="stringtype"/>
  34. </xs:sequence>
  35. </xs:complexType>
  36.  
  37. <xs:complexType name="itemtype">
  38. <xs:sequence>
  39. <xs:element name="title" type="stringtype"/>
  40. <xs:element name="note" type="stringtype" minOccurs="0"/>
  41. <xs:element name="quantity" type="inttype"/>
  42. <xs:element name="price" type="dectype"/>
  43. </xs:sequence>
  44. </xs:complexType>
  45.  
  46. <xs:attribute name="orderid" type="stringtype">
  47.  
  48. </xs:attribute>
  49.  
  50. <xs:complexType name="shipordertype">
  51. <xs:annotation>
  52. <xs:documentation source="description">通过type引用复杂类型或者简单类型,ref引用元素属性</xs:documentation>
  53. </xs:annotation>
  54. <xs:sequence>
  55. <xs:element name="orderperson" type="stringtype" maxOccurs="1" minOccurs="1"/>
  56. <xs:element name="shipto" type="shiptotype" maxOccurs="1" minOccurs="1"/>
  57. <xs:element name="item" type="itemtype" maxOccurs="unbounded" minOccurs="1"/>
  58. </xs:sequence>
  59. <xs:attribute ref="orderid" use="required"/>
  60. </xs:complexType>
  61.  
  62. <xs:element name="shiporder" type="shipordertype"/>
  63.  
  64. </xs:schema>

猜你在找的XML相关文章