创建一个 XML Schema(defined)文件,即xsd

本文通过一个具体的XML Schema实例,详细介绍了如何定义XML文档结构。包括使用标准命名空间、定义复合类型与简易类型元素、设置元素属性等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.w3school.com.cn/schema/schema_example.asp
(1)使用了标准的命名空间 (xs)

xs:schema表示xs的子元素schema;

里面xmlns:xs表示子元素schema的标准命名空间xs;

等号后面的"http://www.w3.org/2001/XMLSchema"是与标准命名空间xs相关联的URI,是子元素schema的语言定义。

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
...
...


</xs:schema>

(2)我们来自定义一个 "beans" 元素。此元素拥有一个属性,其中包含其他的元素,因此我们将它认定为复合类型。"beans" 元素的子元素被 xs:sequence 元素包围,定义了子元素的次序

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="beans">
    <xs:complexType>
       <xs:sequence>
              ...
       </xs:sequence>
    </xs:complexType>
</xs:element>

</xs:schema>

(3)我们来为"beans" 元素定义一个简易类型的子元素"simpleBean"

<xs:element name="simpleBean"></xs:element>

(4)我们来为"beans" 元素定义一个复合类型的子元素"complexBean"

<xs:element name="complexBean">
     <xs:complexType>
           <xs:sequence>
                  <xs:element name="property"></xs:element>             
           </xs:sequence>
     </xs:complexType>
</xs:element>

(5)我们来为子元素"complexBean"的子元素"property"定义几个属性吧

<xs:element name="property">
       <xs:complexType>
             <xs:attribute name="name" type="xs:string" use="required"/>
             <xs:attribute name="value" type="xs:string" use="required"/>
             <xs:attribute name="ref" type="xs:string"/>
       </xs:complexType>
</xs:element>

(6)最后的结果是

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="beans">
    <xs:complexType>
       <xs:sequence>

                <xs:element name="simpleBean"></xs:element>
                <xs:element name="complexBean">
                    <xs:complexType>
                         <xs:sequence>
                               <xs:element name="property">
                                       <xs:complexType>
                                            <xs:attribute name="name" type="xs:string" use="required"/>
                                            <xs:attribute name="value" type="xs:string" use="required"/>
                                            <xs:attribute name="ref" type="xs:string"/>
                                       </xs:complexType>
                               </xs:element>
                         </xs:sequence>
                         <xs:attribute name="id" type="xs:string" use="required"/>
                         <xs:attribute name="class" type="xs:string" use="required"/>
                    </xs:complexType>
              </xs:element>
       </xs:sequence>
    </xs:complexType>
</xs:element>

</xs:schema>

(7)我们将它改一下:

beans.xsd

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

	<xs:element name="beans">
		<xs:complexType>
			<xs:sequence>
				<xs:any maxOccurs="unbounded" minOccurs="0"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>


	<xs:element name="simpleBean"></xs:element>
	
	<xs:element name="complexBean">
		<xs:complexType>
			<xs:sequence>
			     <xs:any maxOccurs="unbounded" minOccurs="0"/>
			</xs:sequence>
			<xs:attribute name="id" type="xs:string" use="required" />
			<xs:attribute name="class" type="xs:string" use="required" />
		</xs:complexType>
	</xs:element>

	<xs:element name="property">
		<xs:complexType>
			<xs:attribute name="name" type="xs:string" use="required" />
			<xs:attribute name="value" type="xs:string"/>
			<xs:attribute name="ref" type="xs:string" />
		</xs:complexType>
	</xs:element>

</xs:schema>

使用它来写xml文件

beans.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<beans 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="spring-beans.xsd">
      <simpleBean></simpleBean>
      <complexBean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"></complexBean>
      <complexBean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
              <property name="dataSource" ref="dataSource"/>
              <property name="configLocation" value="classpath:mybatis-config.xml"/>
              <property name="mapperLocations" value="classpath:com/dao/*Mapper.xml"/>
      </complexBean>
</beans>



### Spring Framework XML Configuration Tutorial and Reference In the context of configuring applications using the Spring framework, one can opt for XML-based configurations as an alternative to JavaConfig. This method involves defining beans within XML files that are then loaded by a `ClassPathXmlApplicationContext` or similar application contexts. #### Defining Beans via XML To define beans through XML configuration, developers use `<bean>` elements inside an XML file typically named something like `applicationContext.xml`. Each bean definition includes attributes such as id (or name), class, scope, etc., along with property values set either directly or via references to other beans[^1]. ```xml <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"> <!-- Define a simple bean --> <bean id="exampleBean" class="com.example.ExampleBean"> <property name="message" value="Hello from XML config"/> </bean> </beans> ``` This snippet illustrates how to declare a single bean named `exampleBean`, which is instantiated based on the specified fully qualified class name (`com.example.ExampleBean`). The properties associated with this instance—such as its message—are configured here too. #### Loading Contexts Using XML Configurations For loading these XML-defined contexts into your application, instantiate classes derived from `AbstractRefreshableApplicationContext`, most commonly used being `FileSystemXmlApplicationContext` when dealing with filesystem paths or `ClassPathXmlApplicationContext` for resources located in the classpath: ```java // Load spring configuration file applicationContext.xml from classpath. ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); ExampleBean exampleBean = (ExampleBean)context.getBean("exampleBean"); System.out.println(exampleBean.getMessage()); ``` The above code demonstrates initializing an ApplicationContext object pointing towards our previously defined XML resource location before retrieving any desired managed objects out of it. #### Property Placeholder Support When working extensively with externalized settings across multiple environments, utilizing placeholders becomes beneficial. By employing `${}` syntax alongside `PropertyPlaceholderConfigurer`, dynamic replacement occurs during runtime without altering core logic[^2]: ```xml <context:property-placeholder location="classpath*:META-INF/*.properties"/> <!-- Use placeholder instead of hard-coded string --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> ``` Herein lies an excerpt showcasing integration between environment-specific variables stored externally while ensuring flexibility throughout different deployment scenarios.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值