eclipse导入本地xsd文件(新手慎入)

警告:以下内容如果对xml schema不了解可能会感觉不适。

eclipse使用xsd文件可以辅助编辑xml文件。如果我们自定义了schema文件,需要导入到xml catalog才可以生效。

如,自定义了logback配置文件的xsd,名为:logback.xsd

那么导入Eclipse中的方法有两种。

导入Namespace name key

Preferences -> XML -> xml Catalog;

点击 Add 按钮,分别输入如下内容:

Location:选择项目或文件系统中的xsd文件。
Key type: Namespace name
Key: http://logback.qos.ch/logback.xsd

那么logback.xml文件如下设置:

<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns="http://logback.qos.sh/logback"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</configuration>

schemaLocation中填写前面指定的”Key”的字符串。

导入Schema Location key

使用命名空间

见多了Spring中的配置文件可能会发现,schemaLocation是如下的形式指定的:

xsi:schemaLocation="[namespace] [schemalocation]"

如果要使用这种方式,需要创建SchemaLocation。

Preferences -> XML -> xml Catalog;

点击 Add 按钮,分别输入如下内容:

Location:选择项目或文件系统中的xsd文件。
Key type: Schema Location
Key: http://logback.qos.ch/logback.xsd

相应的配置文件如下:

<configuration xmlns="http://logback.qos.ch/logback"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://logback.qos.ch/logback http://logback.qos.ch/logback.xsd">

此处的命名空间是schema中定义的targetNamespace,而不是当前xml文件的xmlns。

不使用命名空间

如果schema定义中没有使用命名空间,那么使用noNamespaceSchemaLocation指定schema的key。

由于xsd中没有使用命名空间,那么xml也不要使用命名空间,即”xmlns”不要设置,否则无法在xmlns中找xsd定义的无命名空间的configuration节点。

<?xml version="1.0" encoding="UTF-8"?>
<configuration
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceschemaLocation="http://logback.qos.ch/logback0.xsd">

注:xsd文件也需要删除targetNameSpace。

如果eclipse提示:white space is required between publicid and systemid 可以在xml第二行添加:<!DOCTYPE configuration>,注意修改根节点名称。

测试发现一个问题,可以进行语法校验,但没有语法提示,或许eclipse的xml editor是根据ns去检索的?。

小结:

使用 “Namespace name”,在eclipse增加catalog为Namespace,那么xml中直接使用命名空间就可以使用xsd文件来控制xml。

使用 “Schema location”,分为两种情况:

规范的使用命名空间,xml中需要通过属性"xsi:schemaLocation"来指定"[namespace] [schemalocation]";

不使用命名空间,xml中需要通过属性"xsi:noNamespaceschemaLocation"来指定"Scheme location"。注:xsd和xml均不可使用命名空间。

xsd文件修改以后,需要在xml catalog中reload一下,xml文件也关闭重新打开一下。

实例

你希望为logback创建一个schema。

使用命名空间的schema

schema代码

限于篇幅,只写了appender节点。

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
 targetNamespace="http://logback.qos.sh/logback"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns: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

xml catalog中配置Namespace name
xml catalog配置项

Location:选择这个xsd文件
Key type: Namespace name
Key: http://logback.qos.sh/ns

注,这里的key没有强制要求必须是xsd中定义的targetNamespace,或者说这里可以覆盖缺省的targetNamespace。

xml引用Namespace name
<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns="http://logback.qos.sh/ns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</configuration>

xml中缺省命名空间,即xmlns与配置的Key相同,即可生效。

另外一种配置方式,我的xml中配置了自己的缺省命名空间,那么可以为schema指定别名:

<?xml version="1.0" encoding="UTF-8"?>
<logback:configuration xmlns="http://www.example.com/myapp" xmlns:logback="http://logback.qos.sh/ns">
</logback:configuration>

这是xml规范的基本知识,更多参考xml规范文档。

xml catalog中配置schema location
xml catalog配置项

Location:选择这个xsd文件
Key type: Schema location
Key: http://logback.qos.sh/logback.xsd

注,这里的key没有强制要求必须是xsd文件的物理uri,但是,为了便于你的用户深入了解细节,应该提供物理存在的uri。

xml引用Schema location
<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns="http://logback.qos.sh/logback"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://logback.qos.sh/logback http://logback.qos.ch/logback.xsd">
</configuration>

注:这里xmlns为schema文件中的targetNamespace。

同样有另一种别名的方式:

<?xml version="1.0" encoding="UTF-8"?>
<logback:configuration xmlns="http://www.example.com/myapp" xmlns:logback="http://logback.qos.sh/logback"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://logback.qos.sh/logback http://logback.qos.ch/logback.xsd">
</logback:configuration>

注:这里 xmlns:logback 为schema文件中的targetNamespace。

不使用命名空间的schema

schema代码
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns: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:前缀删除。

xml catalog中配置schema location
xml catalog配置项

Location:选择这个xsd文件
Key type: Schema location
Key: http://logback.qos.sh/logback_nons.xsd

注,这里的key没有强制要求必须是xsd文件的物理uri,但是,为了便于你的用户深入了解细节,应该提供物理存在的uri。

xml引用Schema location
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://logback.qos.sh/logback_nons.xsd">
<appender name="d" class="c">
    <Encoder>
        <pattern>a</pattern>
    </Encoder>
</appender>
</configuration>
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值