警告:以下内容如果对 schema不了解可能会感觉不适。
eclipse使用xsd文件可以辅助编辑 文件。如果我们自定义了schema文件,需要导入到 catalog才可以生效。
如,自定义了logback配置文件的xsd,名为:logback.xsd
那么导入Eclipse中的方法有两种。
导入Namespace name key
Preferences -> -> Catalog; 点击 Add 按钮,分别输入如下内容: Location:选择项目或文件系统中的xsd文件。 Key type: Namespace name Key: http://logback.qos.ch/logback.xsd
那么logback. 文件如下设置:
<? version="1.0" encoding="UTF-8"?> <configuration ns="http://logback.qos.sh/logback" ns:xsi="http://www.w3.org/2001/ Schema-instance"> </configuration>
schemaLocation中填写前面指定的"Key"的字符串。
导入Schema Location key
使用命名空间
见多了Spring中的配置文件可能会发现,schemaLocation是如下的形式指定的:
xsi:schemaLocation="[namespace] [schemalocation]"
如果要使用这种方式,需要创建SchemaLocation。
Preferences -> -> Catalog; 点击 Add 按钮,分别输入如下内容: Location:选择项目或文件系统中的xsd文件。 Key type: Schema Location Key: http://logback.qos.ch/logback.xsd
相应的配置文件如下:
<configuration ns="http://logback.qos.ch/logback" ns:xsi="http://www.w3.org/2001/ Schema-instance" xsi:schemaLocation="http://logback.qos.ch/logback http://logback.qos.ch/logback.xsd">
此处的命名空间是schema中定义的targetNamespace,而不是当前 文件的 ns。
不使用命名空间
如果schema定义中没有使用命名空间,那么使用noNamespaceSchemaLocation指定schema的key。
由于xsd中没有使用命名空间,那么 也不要使用命名空间,即" ns"不要设置,否则无法在 ns中找xsd定义的无命名空间的configuration节点。
<? version="1.0" encoding="UTF-8"?> <configuration ns:xsi="http://www.w3.org/2001/ Schema-instance" xsi:noNamespaceschemaLocation="http://logback.qos.ch/logback0.xsd">
注:xsd文件也需要删除targetNameSpace。
如果eclipse提示:white space is required between publicid and systemid 可以在 第二行添加:<!DOCTYPE configuration>,注意修改根节点名称。
测试发现一个问题,可以进行语法校验,但没有语法提示,或许eclipse的 editor是根据ns去检索的?。
小结:
使用 "Namespace name",在eclipse增加catalog为Namespace,那么 中直接使用命名空间就可以使用xsd文件来控制 。
使用 "Schema location",分为两种情况:
规范的使用命名空间, 中需要通过属性"xsi:schemaLocation"来指定"[namespace] [schemalocation]"; 不使用命名空间, 中需要通过属性"xsi:noNamespaceschemaLocation"来指定"Scheme location"。注:xsd和 均不可使用命名空间。
xsd文件修改以后,需要在 catalog中reload一下, 文件也关闭重新打开一下。
实例
你希望为logback创建一个schema。
使用命名空间的schema
schema代码
限于篇幅,只写了appender节点。
<? version="1.0" encoding="UTF-8"?>
<schema ns="http://www.w3.org/2001/ Schema"
targetNamespace="http://logback.qos.sh/logback"
ns:xs="http://www.w3.org/2001/ Schema"
ns:tns="http://logback.qos.sh/logback" elementFormDefault="qualified" attributeFormDefault="qualified">
<element name="configuration" type="tns:ConfiurationType"></element>
<complexType name="ConfiurationType">
<sequence>
<element name="appender" type="tns:AppenderType"></element>
</sequence>
<attribute name="debug" type="boolean" default="false"></attribute>
<attributeGroup ref="tns:scanConf"></attributeGroup>
</complexType>
<attributeGroup name="scanConf">
<attribute name="scan" type="boolean" default="false"></attribute>
<attribute name="scanPeriod" type="string" default="60 seconds"></attribute>
</attributeGroup>
<complexType name="AppenderType">
<sequence>
<element name="Encoder" type="tns:EncoderType" maxOccurs="1" minOccurs="1"></element>
</sequence>
<attribute name="name" type="string" use="required"></attribute>
<attribute name="class" type="string" use="required"></attribute>
</complexType>
<complexType name="EncoderType">
<sequence>
<element name="pattern" type="string" maxOccurs="1" minOccurs="1"></element>
</sequence>
</complexType>
</schema>为xsd指定的目标命名空间为:http://logback.qos.sh/logback
catalog中配置Namespace name
catalog配置项
Location:选择这个xsd文件 Key type: Namespace name Key: http://logback.qos.sh/ns
注,这里的key没有强制要求必须是xsd中定义的targetNamespace,或者说这里可以覆盖缺省的targetNamespace。
引用Namespace name
<? version="1.0" encoding="UTF-8"?> <configuration ns="http://logback.qos.sh/ns" ns:xsi="http://www.w3.org/2001/ Schema-instance"> </configuration>
中缺省命名空间,即 ns与配置的Key相同,即可生效。
另外一种配置方式,我的 中配置了自己的缺省命名空间,那么可以为schema指定别名:
<? version="1.0" encoding="UTF-8"?> <logback:configuration ns="http://www.example.com/myapp" ns:logback="http://logback.qos.sh/ns"> </logback:configuration>
这是 规范的基本知识,更多参考 规范文档。
catalog中配置schema location
catalog配置项
Location:选择这个xsd文件 Key type: Schema location Key: http://logback.qos.sh/logback.xsd
注,这里的key没有强制要求必须是xsd文件的物理uri,但是,为了便于你的用户深入了解细节,应该提供物理存在的uri。
引用Schema location
<? version="1.0" encoding="UTF-8"?> <configuration ns="http://logback.qos.sh/logback" ns:xsi="http://www.w3.org/2001/ Schema-instance" xsi:schemaLocation="http://logback.qos.sh/logback http://logback.qos.ch/logback.xsd"> </configuration>
注:这里 ns为schema文件中的targetNamespace。
同样有另一种别名的方式:
<? version="1.0" encoding="UTF-8"?> <logback:configuration ns="http://www.example.com/myapp" ns:logback="http://logback.qos.sh/logback" ns:xsi="http://www.w3.org/2001/ Schema-instance" xsi:schemaLocation="http://logback.qos.sh/logback http://logback.qos.ch/logback.xsd"> </logback:configuration>
注:这里 ns:logback 为schema文件中的targetNamespace。
不使用命名空间的schema
schema代码
<? version="1.0" encoding="UTF-8"?>
<xs:schema ns:xs="http://www.w3.org/2001/ Schema"
ns:tns="http://logback.qos.sh/logback" elementFormDefault="qualified">
<xs:element name="configuration" type="ConfiurationType"></xs:element>
<xs:complexType name="ConfiurationType">
<xs:sequence>
<xs:element name="appender" type="AppenderType"></xs:element>
</xs:sequence>
<xs:attribute name="debug" type="xs:boolean" default="false"></xs:attribute>
<xs:attributeGroup ref="scanConf"></xs:attributeGroup>
</xs:complexType>
<xs:attributeGroup name="scanConf">
<xs:attribute name="scan" type="xs:boolean" default="false"></xs:attribute>
<xs:attribute name="scanPeriod" type="xs:string" default="60 seconds"></xs:attribute>
</xs:attributeGroup>
<xs:complexType name="AppenderType">
<xs:sequence>
<xs:element name="Encoder" type="EncoderType" maxOccurs="1" minOccurs="1"></xs:element>
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required"></xs:attribute>
<xs:attribute name="class" type="xs:string" use="required"></xs:attribute>
</xs:complexType>
<xs:complexType name="EncoderType">
<xs:sequence>
<xs:element name="pattern" type="xs:string" maxOccurs="1" minOccurs="1"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>与前面的比较就是删除了targetNamespace,同时将类型引用中的tns:前缀删除。
catalog中配置schema location
catalog配置项
Location:选择这个xsd文件 Key type: Schema location Key: http://logback.qos.sh/logback_nons.xsd
注,这里的key没有强制要求必须是xsd文件的物理uri,但是,为了便于你的用户深入了解细节,应该提供物理存在的uri。
引用Schema location
<? version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration> <configuration ns:xsi="http://www.w3.org/2001/ Schema-instance" xsi:noNamespaceSchemaLocation="http://logback.qos.sh/logback_nons.xsd"> <appender name="d" class="c"> <Encoder> <pattern>a</pattern> </Encoder> </appender> </configuration>
继续阅读与本文标签相同的文章
maven依赖的版本管理
-
新建SpringBoot项目pom文件第一行报错 Unknown error
2026-05-16栏目: 教程
-
eclipse 插件
2026-05-16栏目: 教程
-
java8出来都5年了,内部迭代你懂了吗?
2026-05-16栏目: 教程
-
java集合|遍历HashMap的四种方法
2026-05-16栏目: 教程
-
关于编译器与解释器
2026-05-16栏目: 教程
