zoukankan      html  css  js  c++  java
  • 5分钟搞定Schema

    http://ivan-pig.javaeye.com/blog/296892
    事先声明:搞定不是精通,只是能够运用!
    其实schema很简单,之前写过DTD,比DTD学习成本还低!最近做的项目基于文件系统的,一堆的xml文件,所以想写个验证,也方便IDE操作,既然推荐Schema,那就用Schema
    不废话,看xml文件。
    Xml代码 复制代码
    1. <?xml version="1.0" encoding="gb2312"?>  
    2. <tests id="1" name="2">  
    3.    <name>Ivan</name>  
    4.    <test id="test1">  
    5.      <name>hi</name>  
    6.    </test>  
    7.    <test id="test2">  
    8.      <name>hello</name>   
    9.    </test>  
    10. </tests>  
    <?xml version="1.0" encoding="gb2312"?> <tests id="1" name="2"> <name>Ivan</name> <test id="test1"> <name>hi</name> </test> <test id="test2"> <name>hello</name> </test> </tests>
    这个应该算是个比较典型的xml文件了。看schema怎么写。
    Xml代码 复制代码
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.javaeye.com"  
    3.         xmlns:tns="http://www.javaeye.com"  
    4.         elementFormDefault="qualified">  
    5.     <!-- 一个tests标签-->  
    6.     <xs:element name="tests">  
    7.         <!-- 由于tests标签由属性和其它标签组成,所以是复合类型 -->  
    8.         <xs:complexType>  
    9.             <!-- 这是一个序列,包含所有的子标签-->  
    10.             <xs:sequence>  
    11.                 <!--叫name的子标签-->  
    12.                 <xs:element name="name"/>  
    13.                  <!--叫test的子标签,maxOccurs是出现的次数,这里不定,能多次出现-->  
    14.                 <xs:element name="test" maxOccurs="unbounded">  
    15.                     <!--又是一个复合类型-->  
    16.                     <xs:complexType>  
    17.                         <xs:sequence>  
    18.                             <xs:element name="name"/>  
    19.                         </xs:sequence>  
    20.                         <!--test的id属性-->  
    21.                         <xs:attribute name="id"/>  
    22.                     </xs:complexType>  
    23.                 </xs:element>  
    24.             </xs:sequence>  
    25.             <!--testsde id和name属性-->  
    26.             <xs:attribute name="id"/>  
    27.             <xs:attribute name="name"/>  
    28.         </xs:complexType>  
    29.     </xs:element>  
    30. </xs:schema>  
    <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.javaeye.com" xmlns:tns="http://www.javaeye.com" elementFormDefault="qualified"> <!-- 一个tests标签--> <xs:element name="tests"> <!-- 由于tests标签由属性和其它标签组成,所以是复合类型 --> <xs:complexType> <!-- 这是一个序列,包含所有的子标签--> <xs:sequence> <!--叫name的子标签--> <xs:element name="name"/> <!--叫test的子标签,maxOccurs是出现的次数,这里不定,能多次出现--> <xs:element name="test" maxOccurs="unbounded"> <!--又是一个复合类型--> <xs:complexType> <xs:sequence> <xs:element name="name"/> </xs:sequence> <!--test的id属性--> <xs:attribute name="id"/> </xs:complexType> </xs:element> </xs:sequence> <!--testsde id和name属性--> <xs:attribute name="id"/> <xs:attribute name="name"/> </xs:complexType> </xs:element> </xs:schema>
    很简单吧!其实就是个xml文件而已!敲一遍就知道是什么意思了!
    将Schema添加到xml文件里面,如下:
    Xml代码 复制代码
    1. <?xml version="1.0" encoding="gb2312"?>  
    2. <tests id="1" name="2"  xmlns="http://www.stsoft.com.cn" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    3.     xsi:schemaLocation="http://www.javaeye.com ../test.xsd">  
    4.    <name>Ivan</name>  
    5.    <test id="test1">  
    6.      <name>hi</name>  
    7.    </test>  
    8.    <test id="test2">  
    9.      <name>hello</name>   
    10.    </test>  
    11. </tests>  
    <?xml version="1.0" encoding="gb2312"?> <tests id="1" name="2" xmlns="http://www.stsoft.com.cn" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.javaeye.com ../test.xsd"> <name>Ivan</name> <test id="test1"> <name>hi</name> </test> <test id="test2"> <name>hello</name> </test> </tests>
    这样就可以了,../test.xsd是相对路径,如果test.xsd和xml在同一目录下,直接写名字就可以了。
    应该没多少人会用到Schema,知识积累,以备后用
  • 相关阅读:
    Win10+Ubuntu18.04 UEFI启动模式SSD+HDD
    Chap1:全景图[Computer Science Illuminated]
    [IDE] ECLIPSE取消自动更新
    [Unit Test] Unit Test Brief Introduction
    [ English ] 俚语 “Ping me=打我电话”
    Some Useful Resources for the Future Usage
    python错误记录
    django-用户认证模型
    Djiango-富文本编辑器
    Djiango-建立模型抽象基类
  • 原文地址:https://www.cnblogs.com/danghuijian/p/4400832.html
Copyright © 2011-2022 走看看