XML约束
发布时间:2020-12-15 23:58:27 所属栏目:百科 来源:网络整理
导读:xml约束:规定在xml文件中可以写什么不可以写什么。 dtd约束 内部dtd:在xml内部定义dtd 外部dtd:在外部文件中定义dtd 本地dtd文件: !DOCTYPE students SYSTEM "student.dtd" 网络dtd文件: !DOCTYPE students PUBLIC "空间名称" "student.dtd" student.dtd
xml约束:规定在xml文件中可以写什么不可以写什么。 dtd约束
student.dtd <!ELEMENT students (student*) >
<!ELEMENT student (name,age,sex)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
<!ELEMENT sex (#PCDATA)>
<!ATTLIST student number ID #REQUIRED>
student.xml <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE students SYSTEM "student.dtd">
<students>
<student number="s0001">
<name>zs</name>
<age>abc</age>
<sex>yao</sex>
</student>
</students>
schema约束这个比dtd更加严谨,结构更加清晰。 student.xsd <?xml version="1.0"?>
<xsd:schema xmlns="http://www.itcast.cn/xml" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.itcast.cn/xml" elementFormDefault="qualified">
<xsd:element name="students" type="studentsType"/>
<xsd:complexType name="studentsType">
<xsd:sequence>
<xsd:element name="student" type="studentType" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="studentType">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="age" type="ageType" />
<xsd:element name="sex" type="sexType" />
</xsd:sequence>
<xsd:attribute name="number" type="numberType" use="required"/>
</xsd:complexType>
<xsd:simpleType name="sexType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="male"/>
<xsd:enumeration value="female"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="ageType">
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="0"/>
<xsd:maxInclusive value="256"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="numberType">
<xsd:restriction base="xsd:string">
<xsd:pattern value="itcast_d{4}"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
student.xml <?xml version="1.0" encoding="UTF-8" ?>
<!-- 1、编写根标签 2、引入实例名称空间 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3、引入默认的名称空间 xmlns="http://www.itcast.cn/xml" 4、引入名称空间 xsi:schemaLocation="http://www.itcast.cn/xml student.xsd" -->
<students xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.itcast.cn/xml" xsi:schemaLocation="http://www.itcast.cn/xml student.xsd" >
<student number="itcast_1001">
<name>tom</name>
<age>21</age>
<sex>male</sex>
</student>
</students>
xmlns,ns代表的就是namespace 实例空间名是w3c弄得规定,都这样写,有了XML Schema实例命名空间就可以使用schemaLocation属性了。此属性有两个值。第一个值是需要使用的命名空间。第二个值是供命名空间使用的XML schema的位置。 默认命名空间,此声明会告知schema验证器,在此xml文档中使用的所有元素都被声明于这个命名空间。 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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 http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<context:component-scan base-package="cn.e3mall.controller" />
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
其中如果在xmlns的冒号后边添加东西,在使用这个命名空间中的东西时前面都要加上这个前缀。 比如 xmlns:context="http://www.springframework.org/schema/context"
<context:component-scan base-package="cn.e3mall.controller" /> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |