zoukankan      html  css  js  c++  java
  • 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~

  • 相关阅读:
    设计模式学习总结系列应用实例
    【研究课题】高校特殊学生的发现及培养机制研究
    Linux下Oracle11G RAC报错:在安装oracle软件时报file not found一例
    python pro practice
    openstack python sdk list tenants get token get servers
    openstack api
    python
    git for windows
    openstack api users list get token get servers
    linux 流量监控
  • 原文地址:https://www.cnblogs.com/lzhh/p/8630977.html
Copyright © 2011-2022 走看看