XML 之 命名空间详解

最近学xml 遇到了点小问题qaq 找了n多的博客大佬,反复看了半小时终于明白了,可能我太蠢了。。。

基础的知识就不赘述,直接放“栗子”,切重点:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:lzh="http://info.school.edu.cn" targetNamespace="http://info.school.edu.cn" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="schools">
        <xs:annotation>
            <xs:documentation>Comment describing your root element</xs:documentation>
        </xs:annotation>
        <xs:complexType>
            <xs:sequence>
                <xs:element name="school" maxOccurs="10">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="sch_name" type="xs:string"/>
                            <xs:element name="sch_level">
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:enumeration value="一本"/>
                                        <xs:enumeration value="二本"/>
                                        <xs:enumeration value="大专"/>
                                        <xs:enumeration value="中专"/>
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>
                            <xs:element name="sch_location" type="xs:string"/>
                            <xs:element name="area">
                                <xs:simpleType>
                                    <xs:restriction base="xs:decimal">
                                        <xs:totalDigits value="6"/>
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>
                            <xs:element name="principal_name" type="xs:string"/>
                        </xs:sequence>
                        <xs:attribute name="create_time">
                            <xs:simpleType>
                                <xs:restriction base="xs:string">
                                    <xs:pattern value="[0-9]{4}-[0-1][0-9]-[0-3][0-9]"/>
                                </xs:restriction>
                            </xs:simpleType>
                        </xs:attribute>
                        <xs:attribute name="country" default="中国"/>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
<?xml version="1.0" encoding="UTF-8"?>
<lzh:schools xmlns:lzh="http://info.school.edu.cn" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://info.school.edu.cn file:///C:/Users/del/Desktop/lzh.xsd">
    <lzh:school create_time="1985-01-02">
        <lzh:sch_name>淮海工学院</lzh:sch_name>
        <lzh:sch_level>二本</lzh:sch_level>
        <lzh:sch_location>连云港市</lzh:sch_location>
        <lzh:area>2162.78</lzh:area>
        <lzh:principal_name>宁晓明</lzh:principal_name>
    </lzh:school>
    <lzh:school>
        <lzh:sch_name>清华大学</lzh:sch_name>
        <lzh:sch_level>一本</lzh:sch_level>
        <lzh:sch_location>北京市</lzh:sch_location>
        <lzh:area>392.4</lzh:area>
        <lzh:principal_name>邱勇</lzh:principal_name>
    </lzh:school>
</lzh:schools>

首先知道,targetNameSpace。targetNamespace定义了Schema定义的新元素与属性的名称空间。而"http://www.w3.org/2001/XMLSchema"名称空间则定义了element, attribute, complexType, group, simpleType等元素。若自身并不使用重用组件,仅供外部使用的话,则只定义targetNameSpace就可以,不用指定别名。

然后呢,xmlns:lzh="http://info.school.edu.cn" targetNamespace="http://info.school.edu.cn" 这里的URI是一样的,因为xmlns:后的lzh是一个别名一个命名空间的前缀,方便下面的element在遇到elementFormDefault="qualified"的情况下的使用。

over~好久没写博客,快要去上课了就先酱紫吧emmm~

 

posted @ 2018-03-23 16:19  叶碎夜  阅读(384)  评论(0编辑  收藏  举报