在实例中引用模式文档
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属性必须出现在它要验证的任何元素和属性之前(这里的意思是说xsi:schemaLocation属性可以在xml文件中的其他元素使用,例如上面例子的book元素也可以使用该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。
Dell 15R 5521安装WinXP方法及驱动下载链接
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=
Copyright (c) 2002-2006 by OpenSymphony <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">
修改pom文件,添加坐标freemarker启动器坐标 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=... <modelVersion>4.0.0 <groupId>org.springframework.boot <artifactId>spring-boot-starter-...
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 ...
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0 <groupId>test <artifactId>SeleniumJava <packaging>war <version>0.0.1-SNAPSHOT...
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <localRepository>D:\repository\mavenrepository <!-- proxy | Specification for...
灯笼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
修改pom文件加入两个坐标 jstl标签库 和 jasper <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=... <modelVersion>4.0.0 <groupId>org.springframework.boot <artifactId>spring-boot-starter...
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0 <groupId>com.app</groupId> <artifactId>AppTest <version>0.0.1-...
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd "> <!-- 父项目的坐标。如果项目中没有规定某个元素的值,那么父项目中的对应值即为项目的默认值。 坐标包括...
c#设置xml内容不换行:添加属性为xsi:nil=true的空节点便可实现,感兴趣的你可以参考下本文,或许有意想不到的收获
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0 <groupId>org.springframework.boot <artifactId>spring-boot-starter-parent ...
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实现) -<web-app xsi:schemaLocation=...
可以利用hibernate tools生成相关映射文件已经po对象、dao... xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--配置数据源-->
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <!-- 有可能下载不到该Jar, 可以到该地址下载相应版本:...
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"> <tlib-version>1.0 <short-name>jsps <name>aaaa <tagclass>com.Syste</tagclass> <bodycontent>JSP</body...
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 ...