`
hougbin
  • 浏览: 492217 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

xsi:schemaLocation详解

    博客分类:
  • XML
阅读更多

在实例中引用模式文档

XML Schema提供了两个在实例文档中使用的特殊属性,用于指出模式文档的位置。这两个属性是:xsi:schemaLocation和xsi:noNamespaceSchemaLocation,前者用于声明了目标名称空间的模式文档,后者用于没有目标名称空间的模式文档,它们通常在实例文档中使用。

4.5.7.1  xsi:schemaLocation属性

xsi:schemaLocation属性的值由一个URI引用对组成,两个URI之间以空白符分隔。第一个URI是名称空间的名字,第二个URI给出模式文档的位置,模式处理器将从这个位置读取模式文档,该模式文档的目标名称空间必须与第一个URI相匹配。我们看例4-28。

例4-28  book6.xml

<?xml version="1.0" encoding="GB2312"?>
<book xmlns="http://www.sunxin.org/book"   ①
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  ②
xsi:schemaLocation="http://www.sunxin.org/book http://www.sunxin.org/ 
book.xsd">  ③
<title>《Struts 2深入详解》</title>
<author>孙鑫</author>
</book>

① 声明默认的名称空间(http://www.sunxin.org/book)。

② 声明XML Schema实例名称空间(http://www.w3.org/2001/XMLSchema-instance),并将xsi前缀与该名称空间绑定,这样模式处理器就可以识别xsi:schemaLocation属性。XML Schema实例名称空间的前缀通常使用xsi。

③ 使用xsi:schemaLocation属性指定名称空间http://www.sunxin.org/book和模式位置http://www.sunxin.org/book.xsd相关。要注意,在这个例子中,book.xsd中声明的目标名称空间要求是http://www.sunxin.org/book

一个可能的模式文档book.xsd如例4-29所示。

例4-29  book.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns="http://www.sunxin.org/book" 
targetNamespace="http://www.sunxin.org/book" 
elementFormDefault="qualified">

<xs:element name="book" type="bookType"/>
<xs:complexType name="bookType">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
</xs:sequence>  
</xs:complexType>
</xs:schema>

实际上,xsi:schemaLocation属性的值也可以由多个URI引用对组成,每个URI引用对之间使用空白符分隔。例4-30的实例文档使用了多个名称空间,xsi:schemaLocation属性的值包含了两对URI。

例4-30  books.xml

<?xml version="1.0" encoding="GB2312"?>
<books xmlns="http://www.sunxin.org/bks" xmlns:p="http://www.sunxin.org/people"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sunxin.org/bks bks.xsd
http://www.sunxin.org/people people.xsd">
<book>
<title>JSP深入编程</title>
<author>
<p:name>张三</p:name>
<p:title>作家</p:title>
</author>
</book>
<book>
<title>XML从入门到精通</title>
<author>
<p:name>李四</p:name>
<p:title>教师</p:title>
</author>
</book>
</books>

XML Schema推荐标准中指出,xsi:schemaLocation属性可以在实例中的任何元素上使用,而不一定是根元素,不过,xsi:schemaLocation属性必须出现在它要验证的任何元素和属性之前。

此外,要注意的是,XML Schema推荐标准并没有要求模式处理器必须要使用xsi:schemaLocation属性,某些模式处理器可以通过其他的方式来得到模式文档的位置,而忽略xsi:schemaLocation属性。

xsi:noNamespaceSchemaLocation属性

xsi:noNamespaceSchemaLocation属性用于引用没有目标名称空间的模式文档。与xsi:schemaLocation属性不同的是,xsi:noNamespaceSchemaLocation属性的值是单一的值,只是用于指定模式文档的位置。例4-31显示了在实例文档中xsi:noNamespaceSchema Location属性的使用。

例4-31  book7.xml

<?xml version="1.0" encoding="GB2312"?>
<book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="book.xsd" 
isbn="978-7-121-06812-6" >
<title>《Struts 2深入详解》</title>
<author>孙鑫</author>
</book>

与xsi:schemaLocation属性一样,xsi:noNamespaceSchemaLocation属性也可以在实例中的任何元素上使用,而不一定是根元素,不过,xsi:noNamespaceSchemaLocation属性必须出现在它要验证的任何元素和属性之前。

此外,要注意的是,XML Schema推荐标准并没有要求模式处理器必须要使用xsi:noNamespaceSchemaLocation属性,某些模式处理器可以通过其他的方式来得到模式文档的位置,而忽略xsi:noNamespaceSchemaLocation属性。

分享到:
评论

相关推荐

    xml中的xmlns:、xmlns:xsi和xsi:schemaLocation.doc

    xml中的xmlns:、xmlns:xsi和xsi:schemaLocation。

    Dell 15R 5521安装WinXP

    Dell 15R 5521安装WinXP方法及驱动下载链接

    SpringMVC配置文件最全约束

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" ... xsi:schemaLocation=

    xwork-2.0.4-src

    Copyright (c) 2002-2006 by OpenSymphony &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:... xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt;

    SpringBoot整合Freemarker.docx

    修改pom文件,添加坐标freemarker启动器坐标 &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=... &lt;modelVersion&gt;4.0.0 &lt;groupId&gt;org.springframework.boot &lt;artifactId&gt;spring-boot-starter-...

    Spring实战2-Bean注入详解的源代码

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context ...

    java-selenium-maven-pom文件

    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt; &lt;modelVersion&gt;4.0.0 &lt;groupId&gt;test &lt;artifactId&gt;SeleniumJava &lt;packaging&gt;war &lt;version&gt;0.0.1-SNAPSHOT...

    springbootMaven配置文件,用于更改资源下载路径,避免初始化项目报错

    xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"&gt; &lt;localRepository&gt;D:\repository\mavenrepository &lt;!-- proxy | Specification for...

    lantern:一个用于自动执行安全扫描的python库-源码

    灯笼Lantern是使用Python编写...filelist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="https://analysiscenter.veracode.com/schema/2.0/filelist"xsi:schemaLocation="https://analysiscenter.vera

    SpringBoot整合jsp.docx

    修改pom文件加入两个坐标 jstl标签库 和 jasper &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=... &lt;modelVersion&gt;4.0.0 &lt;groupId&gt;org.springframework.boot &lt;artifactId&gt;spring-boot-starter...

    配置文件pom.xml

    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt; &lt;modelVersion&gt;4.0.0 &lt;groupId&gt;com.app&lt;/groupId&gt; &lt;artifactId&gt;AppTest &lt;version&gt;0.0.1-...

    Maven的pom.xml配置文件详细配置说明

    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd "&gt; &lt;!-- 父项目的坐标。如果项目中没有规定某个元素的值,那么父项目中的对应值即为项目的默认值。 坐标包括...

    c#设置xml内容不换行及属性xsi:nil=true的空节点添加

    c#设置xml内容不换行:添加属性为xsi:nil=true的空节点便可实现,感兴趣的你可以参考下本文,或许有意想不到的收获

    Spring Boot Socket实现与html页面的连接,案例讲解客户端与服务器端相互发消息.docx

    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"&gt; &lt;modelVersion&gt;4.0.0 &lt;groupId&gt;org.springframework.boot &lt;artifactId&gt;spring-boot-starter-parent ...

    项目源码java京东商城购物网站

    xsi:schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx ...

    聊天室(ActionScript 3.0实现)

    聊天室(ActionScript 3.0实现) -&lt;web-app xsi:schemaLocation=...

    spring框架配置bean的高级属性

    可以利用hibernate tools生成相关映射文件已经po对象、dao... xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"&gt; &lt;!--配置数据源--&gt;

    ImpalaJDBC42-2.6.17.1020.jar

    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt; &lt;!-- 有可能下载不到该Jar, 可以到该地址下载相应版本:...

    java自定义标签

    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"&gt; &lt;tlib-version&gt;1.0 &lt;short-name&gt;jsps &lt;name&gt;aaaa &lt;tagclass&gt;com.Syste&lt;/tagclass&gt; &lt;bodycontent&gt;JSP&lt;/body...

    springmvc-config.xml

    xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/mvc ...

Global site tag (gtag.js) - Google Analytics