一个完整的schema验证xml的例子

前端之家收集整理的这篇文章主要介绍了一个完整的schema验证xml的例子前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. xml文件
  1. <reference xmlns="http://www.w3school.com.cn" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3school.com.cn J.xsd">
  2. <author authorLoc="1">陈路瑶</author>
  3. <title>信息文档结构信任模式的提取及逻辑描述</title>
  4. <type>J</type>
  5. <publisher>计算机应用研究</publisher>
  6. <publish_year>2010</publish_year>
  7. <volumn_mark>27</volumn_mark>
  8. <page_number>4624-4629</page_number>
  9. </reference>


schema文件(J.xsd):

  1. <?xml version="1.0" encoding="GB2312"?>
  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.w3school.com.cn" targetNamespace="http://www.w3school.com.cn" elementFormDefault="qualified">
  3. <xs:element name="reference">
  4. <xs:complexType>
  5. <xs:sequence>
  6. <xs:element name="author" type="authorInfo" minOccurs="1" maxOccurs="1"/>
  7. <xs:element name="title" type="xs:string" minOccurs="1" maxOccurs="1"/>
  8. <xs:element name="type" type="xs:string" minOccurs="1" maxOccurs="1"/>
  9. <xs:element name="publish" type="xs:string" minOccurs="0" maxOccurs="1"/>
  10. <xs:element name="publisher" type="xs:string" minOccurs="1" maxOccurs="1"/>
  11. <xs:element name="publish_year" type="xs:string" minOccurs="1" maxOccurs="1"/>
  12. <xs:element name="volumn_mark" type="xs:string" minOccurs="1" maxOccurs="1"/>
  13. <xs:element name="page_number" minOccurs="1" maxOccurs="1">
  14. <xs:simpleType>
  15. <xs:restriction base="xs:string">
  16. <xs:pattern value="(\d{1,4}-)?\d{1,4}"/>
  17. </xs:restriction>
  18. </xs:simpleType>
  19. </xs:element>
  20. </xs:sequence>
  21. </xs:complexType>
  22. </xs:element>
  23. <xs:complexType name="authorInfo">
  24. <xs:simpleContent>
  25. <xs:extension base="xs:string">
  26. <xs:attribute name="authorLoc" use="required">
  27. <xs:simpleType>
  28. <xs:restriction base="xs:integer">
  29. <xs:enumeration value="1"/>
  30. </xs:restriction>
  31. </xs:simpleType>
  32. </xs:attribute>
  33. </xs:extension>
  34. </xs:simpleContent>
  35. </xs:complexType>
  36. </xs:schema>

猜你在找的XML相关文章