为什么使用eclipse对多个xsd文件验证xml文件时,所包含的元素名称为何未得到验证?

问题描述 投票:0回答:1

我正在使用Eclipse编写xml模式,并且在验证方面存在小问题。

我有两个模式和一个xml文件。

Main.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.example.org/Main"
    xmlns:tns="http://www.example.org/Main"
    elementFormDefault="qualified">

    <complexType name="mainType">
        <sequence>
            <any namespace="##other" processContents="lax" />
        </sequence>
    </complexType>

    <element name="main" type="tns:mainType" />
</schema

Sub.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.example.org/Sub"
    xmlns:tns="http://www.example.org/Sub"
    elementFormDefault="qualified">

    <complexType name="subType">
        <attribute name="name" type="string"
            use="required" />
    </complexType>

    <element name="sub" type="tns:subType" />
</schema>

Sample.xml:

<?xml version="1.0" encoding="UTF-8"?>
<tns:main xmlns:tns="http://www.example.org/Main"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:sub="http://www.example.org/Sub"
    xsi:schemaLocation="
        http://www.example.org/Main Main.xsd 
        http://www.example.org/Sub Sub.xsd">
    <sub:sub name="test" />
</tns:main>

如果更改子元素的名称属性,则验证将按预期失败:

Validation fails as expected

但是如果我更改子元素的元素名称,则验证成功:

Validation succeeds

我希望此验证也会失败。什么不是?

xml eclipse xsd schema
1个回答
0
投票

我想这是因为XML模式定义中对lax的定义:

lax:如果该项目具有唯一确定的声明,则它关于该声明必须“有效”,即“有效”如果可以的话,请不要担心。

换句话说,由于没有可用的<sub:subx>元素声明,因此有效。

© www.soinside.com 2019 - 2024. All rights reserved.