xml中xsd、xsi、xmlns的含义
Maven的pom.xml文件頭信息如下:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
其中,xmlns的值是默認的命名空間;
xmlns:xsi的值是以xsi為前綴的標簽屬性的命名空間,xsi:schemaLocation就是該命名空間下的屬性;
xsi:schemaLocation的值是xml文件書寫需要遵循的xsd約束,它包括兩部分:前面是xsd文件對應的命名空間,后面是xsd文件的地址。
打開地址http://maven.apache.org/xsd/maven-4.0.0.xsd"包含如下頭信息:
<xs:schemaxmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://maven.apache.org/POM/4.0.0"
elementFormDefault="qualified"
targetNamespace="http://maven.apache.org/POM/4.0.0">
targetNamespace表示該xsd只對該目標命名空間生效,即xsd文件對應該命名空間。在pom.xml中使用xsi:schemaLocation引入xsd文件的時候,在xsd文件的前面就是該xsd對應的命名空間。
在一個xml文件中可以定義多個命名空間,比如在springmvc-servlet.xml文件中,它的文件頭信息如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
springmvc-servlet.xml的每個命名空間對應不同的xsd約束文件。如圖1,以命名空間為前綴的標簽/屬性受該命名空間對應的xsd文件約束。
圖1 spring-servlet.xml中的多個命名空間
再回到pom.xml文件中。xml文件中頭信息中包含的xmlns:xsi可以看出,該xml文件可以看成是受xml-schema-instance的約束,xml-schema-instance就是xsd。在http://maven.apache.org/xsd/maven-4.0.0.xsd中,文件頭信息xmlns:xs="http://www.w3.org/2001/XMLSchema"表明xsd文件受xml-schema文件約束。如下圖所示:
xml-Schema → xml-schema-instance(xsd)→ xml 注: “→“”表示約束關系
這在“https://www.w3.org/TR/xmlschema-1/#ref-xml-namespaces”里也有描述:
The purpose ofXML Schema: Structuresis to define the nature of XML schemas and their component parts, provide an inventory of XML markup constructs with which to represent schemas, and define the application of schemas to XML documents.
The purpose of anXML Schema: Structuresschema is to define and describe a class of XML documents by using schema components to constrain and document the meaning, usage and relationships of their constituent parts: datatypes, elements and their content and attributes and their values. Schemas may also provide for the specification of additional document information, such as normalization and defaulting of attribute and element values. Schemas have facilities for self-documentation. Thus,XML Schema: Structurescan be used to define, describe and catalogue XML vocabularies for classes of XML documents.
xml的約束有兩種方式,除了上面提到的xsd,還有dtd約束文件。在mybatis-config.xml中就使用的dtd約束,它的文件頭信息如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "http://mybatis.org/dtd/mybatis-3-config.dtd" "mybatis-3-config.dtd" >
總結
以上是生活随笔為你收集整理的xml中xsd、xsi、xmlns的含义的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Excel表怎么设计工资条 Excel表
- 下一篇: Java和C或C++的数据类型对照表