首页 > 代码库 > xml schema xmlns xmlns:xsi xsi:schemaLocation targetnamespace

xml schema xmlns xmlns:xsi xsi:schemaLocation targetnamespace

先上一段xml文档

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:batch="http://www.springframework.org/schema/batch"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/batch
                           http://www.springframework.org/schema/batch/spring-batch.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd
                             ">
    <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
        <property name="jobRepository" ref="jobRepository" />
    </bean>
    <bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"></bean>
    <bean id="transactionManager"
        class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
    <batch:job id="iconvJob">
        <batch:step id="iconvStep">
            <batch:tasklet transaction-manager="transactionManager">
                <batch:chunk reader="iconvItemReader" writer="iconvItemWriter"
                    processor="iconvItemProcessor" commit-interval="1" />
            </batch:tasklet>
        </batch:step>
    </batch:job>
    <context:property-placeholder location="classpath:files.properties" />
    <bean id="iconvItemReader" class="com.inetpsa.batch.iconv.IconvItemReader">
        <property name="input" value="file:${input.file}" />
        <property name="charset" value="UTF-8" />
    </bean>
    <bean id="iconvItemWriter" class="com.inetpsa.batch.iconv.IconvItemWriter">
        <property name="output" value="file:${output.file}" />
        <property name="charset" value="UTF-8" />
    </bean>
    <bean id="iconvItemProcessor" class="com.inetpsa.batch.iconv.IconvItemProcessor" />
</beans>

从头开始解释:

1.xmlns

xmlns="http://www.springframework.org/schema/beans"

指定了该xml的默认的namespace是:xmlns="http://www.springframework.org/schema/beans",对,这是一个URI,(至于URI和URL的区别,引用知乎上的一句话解释:URI 和 URL 都定义了 what the resource is。URL 还定义了 how to get the resource。),这个URI在后面的 xsi:schemaLocation 中需要用到,来引入该namespace对应的schema的URL:

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

其中红色的两部分用空格分隔,前面是该namespace的值,该值和前面用xmlns定义的是一致的,后半部分定义了该namespace对应的schema的地址,可以用浏览器打开看一下,就是一个schema文档,这个文档其实就是Spring XML Beans Schema。所以本文一开始的xml立案,<bean>标签不需要加什么前缀。

2.xmls:xsi

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

主要是上面这个地方用到了xsi,到底是什么意思呢,其实该前缀的名称也是可以变的,比如改成xsii也可以,只不过一定该属性的值是http://www.w3.org/2001/XMLSchema-instance

---

xml schema xmlns xmlns:xsi xsi:schemaLocation targetnamespace