XSD - <schema> 元素

本文介绍如何在 XML 文档中使用与引用 XML Schema,包括 schema 的基本结构、属性说明及其与 XML 实例文档之间的关联方式。

<schema> 元素是每一个 XML Schema 的根元素。

<schema> 元素

<schema> 元素是每一个 XML Schema 的根元素:

<?xml version="1.0"?>

<xs:schema>

...
...

</xs:schema>

<schema> 元素可包含属性。一个 schema 声明往往看上去类似这样:

<?xml version="1.0"?>
 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3school.com.cn"
xmlns="http://www.w3school.com.cn"
elementFormDefault="qualified">

...
...
</xs:schema>

代码解释:

下面的片断:

xmlns:xs="http://www.w3.org/2001/XMLSchema"

显示 schema 中用到的元素和数据类型来自命名空间 "http://www.w3.org/2001/XMLSchema"。同时它还规定了来自命名空间 "http://www.w3.org/2001/XMLSchema" 的元素和数据类型应该使用前缀 xs:

这个片断:

targetNamespace="http://www.w3school.com.cn" 

显示被此 schema 定义的元素 (note, to, from, heading, body) 来自命名空间: "http://www.w3school.com.cn"。

这个片断:

xmlns="http://www.w3school.com.cn" 

指出默认的命名空间是 "http://www.w3school.com.cn"。

这个片断:

elementFormDefault="qualified" 

指出任何 XML 实例文档所使用的且在此 schema 中声明过的元素必须被命名空间限定。

在 XML 文档中引用 Schema

此 XML 文档含有对 XML Schema 的引用:

<?xml version="1.0"?>

<note xmlns="http://www.w3school.com.cn"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3school.com.cn note.xsd">

<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

代码解释:

下面的片断:

xmlns="http://www.w3school.com.cn" 

规定了默认命名空间的声明。此声明会告知 schema 验证器,在此 XML 文档中使用的所有元素都被声明于 "http://www.w3school.com.cn" 这个命名空间。

一旦您拥有了可用的 XML Schema 实例命名空间:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

您就可以使用 schemaLocation 属性了。此属性有两个值。第一个值是需要使用的命名空间。第二个值是供命名空间使用的 XML schema 的位置:

xsi:schemaLocation="http://www.w3school.com.cn note.xsd"
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="sapInfDataSource" class="com.bs.apps.core.datasource.wrapper.DataSourceWrapper"> <property name="dataSource"> <bean class="com.bs.apps.core.datasource.wrapper.CustomDataSource" destroy-method="close"> <property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property> <property name="url"><value>jdbc:oracle:thin:@10.216.109.93:1521:misdevdb</value></property> <property name="username"><value>mesinterface</value></property> <property name="password"><value>mesinterface12c</value></property> <property name="validationQuery"><value>select 1 from DUAL</value></property> </bean> </property> </bean> <bean id="sapInfSessionFactory" class="com.bs.apps.core.persistence.impl.DefaultSessionFactoryBean"> <property name="dataSource" ref="sapInfDataSource"/> <property name="mappingResources"> <list> <value>com/sap/apps/sapInterface/domain/SAPInfConfig.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <!-- 测试的时候HSQL的dialect --> <prop key="hibernate.dialect">com.bs.apps.core.dialect.CustOracle10gExtendedDialect</prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.query.substitutions">true 1, false 0</prop> <!-- “当前session”的范围和上下文 (scopeand context)的定义,session要绑定的上下文设置为由spring环境管理 --> <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop> </props> </property> <!-- DB中table名字的前缀配置 --> <!-- <property name="tablePrefix"> <value>sap_</value> </property> --> </bean> <bean id="sapInfConfigService" class="com.sap.apps.sapInterface.service.impl.SAPInfConfigServiceImpl"> <property name="sessionFactory" ref="sapInfSessionFactory"/> </bean> </beans>
最新发布
08-23
(2)据下列XML Schema文档定义的XML文档的结构及每个元素的数据类型,自行书写XML文档的数据内容,并设计该XML文档在页面中显示的样式,写出对映的XML文档及对映的xsl文件。 编写折半法查找的程序。 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:simpleType name="bookprice"> <xsd:restriction base="xsd:decimal"> <xsd:totalDigits value="6"/> <xsd:fractionDigits value="2"/> <xsd:minInclusive value="0.00"/> <xsd:maxInclusive value="100.00"/> <!-- 用fractionDigits,totalDigits时 数据 类型应写成 base="xsd:decimal" 写成 float 错误 --> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="category"> <xsd:restriction base="xsd:token"> <xsd:enumeration value="小说"/> <xsd:enumeration value="散文"/> <xsd:enumeration value="传记"/> <xsd:enumeration value="诗歌"/> <xsd:enumeration value="武侠"/> <xsd:enumeration value="纪实"/> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="categoylist"> <xsd:list itemType="category"/> </xsd:simpleType> <xsd:simpleType name="threeBookCate"> <xsd:restriction base="categoylist"> <xsd:length value="3"/> </xsd:restriction> </xsd:simpleType> <xsd:element name="booklist"> <xsd:complexType> <xsd:sequence> <xsd:element name="categoylist" type="categoylist"/> <xsd:element name="threeBookCategory" type="threeBookCate"/> <xsd:element ref="book"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="book"> <xsd:complexType> <xsd:sequence> <xsd:element name="title" type="xsd:string"/> <xsd:element name="author" type="xsd:string"/> <xsd:element name="price" type="bookprice"/> <xsd:element name="resume" type="xsd:string"/> <xsd:element name="recommendation" type="xsd:string"/> <xsd:element name="publish" minOccurs="0" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="publisher" type="xsd:string"/> <xsd:element name="pubdate" type="xsd:date"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="category" type="category"/> </xsd:sequence> <xsd:attribute name="isbn" type="xsd:string"/> </xsd:complexType> </xsd:element> </xsd:schema>
05-24
<?xml version="1.0" encoding="UTF-8"?> <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"> <modelVersion>4.0.0</modelVersion> <groupId>me.zhengjie</groupId> <artifactId>eladmin</artifactId> <packaging>pom</packaging> <version>1.1</version> <modules> <module>eladmin-common</module> <module>eladmin-logging</module> <module>eladmin-system</module> <module>eladmin-tools</module> <module>eladmin-generator</module> <module>nat-project</module> </modules> <name>ELADMIN 后台管理</name> <url>https://eladmin.vip</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.18</version> </parent> <properties> <logback.version>1.2.9</logback.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <fastjson2.version>2.0.54</fastjson2.version> <druid.version>1.2.19</druid.version> <commons-pool2.version>2.11.1</commons-pool2.version> </properties> <dependencies> <!--Spring boot Web容器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <!-- 去掉Jackson依赖,用fastjson --> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-json</artifactId> </exclusion> </exclusions> </dependency> <!--Spring boot 测试--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--Spring boot 安全框架--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <!-- spring boot 验证 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> <!-- spring boot 缓存 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <!--Spring boot Redis--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!--Spring boot redisson--> <dependency> <groupId>org.redisson</groupId> <artifactId>redisson-spring-boot-starter</artifactId> <version>3.17.1</version> </dependency> <!-- Spring boot websocket --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> <!-- mybatis --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.3.1</version> </dependency> <!--spring boot 集成redis所需common-pool2--> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>${commons-pool2.version}</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> <!--监控sql日志--> <dependency> <groupId>p6spy</groupId> <artifactId>p6spy</artifactId> <version>3.9.1</version> </dependency> <!-- Swagger UI 相关 --> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>3.0.3</version> <exclusions> <!-- 去掉 swagger-annotations 依赖,避免冲突 --> <exclusion> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> </exclusion> </exclusions> </dependency> <!-- 添加swagger-annotations依赖 --> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> <version>1.5.22</version> </dependency> <!--Mysql依赖包--> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <version>9.2.0</version> <scope>runtime</scope> </dependency> <!-- druid数据源驱动 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${druid.version}</version> </dependency> <!-- IP地址解析库 --> <dependency> <groupId>net.dreamlu</groupId> <artifactId>mica-ip2region</artifactId> <version>2.7.18.9</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.9.7</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.7</version> </dependency> <!--lombok插件--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!-- excel工具 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>5.4.0</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.4.0</version> </dependency> <dependency> <groupId>xerces</groupId> <artifactId>xercesImpl</artifactId> <version>2.12.2</version> </dependency> <!-- fastjson2 --> <dependency> <groupId>com.alibaba.fastjson2</groupId> <artifactId>fastjson2</artifactId> <version>${fastjson2.version}</version> </dependency> <dependency> <groupId>com.alibaba.fastjson2</groupId> <artifactId>fastjson2-extension-spring5</artifactId> <version>${fastjson2.version}</version> </dependency> <!-- Java图形验证码 --> <dependency> <groupId>com.github.whvcse</groupId> <artifactId>easy-captcha</artifactId> <version>1.6.2</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-text</artifactId> <version>1.13.0</version> </dependency> </dependencies> <build> <plugins> <!-- 打包时跳过测试 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> </plugins> </build> <repositories> <repository> <id>public</id> <name>aliyun nexus</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>public</id> <name>aliyun nexus</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </project> <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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>me.zhengjie</groupId> <artifactId>nat-project</artifactId> <version>1.1</version> </parent> <artifactId>nat-project</artifactId> <name>Archetype - nat-project</name> <url>http://maven.apache.org</url> </project> 这是父子pom看一下
07-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值