为什么我的 XSD 没有验证某些元素?

Why is my XSD not validating some elements?

我正在尝试为递归数据结构定义一个 XML 模式。基本上,一个元素应该能够具有 children 和属性,这是一个元素列表,而这些元素又可能具有 children 和属性。 "element" 可能是一系列类型中的一种,除了一些额外数据外,所有类型都可能具有 children 和属性。因此,元素类型的排列方式继承自 BaseElementType 并在顶部添加自己的数据。

我取得了一些进展,但 运行 遇到了一个问题,即验证器在向下移动结构时几乎接受任何东西。

我的架构如下:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.example.org/ElementSchema" xmlns="http://www.example.org/ElementSchema">


    <xs:element name="BaseElement" type="BaseElementType" />
    <xs:element name="DoubleElement" type="DoubleElementType" />
    <xs:element name="BoundedDoubleElement" type="BoundedDoubleElementType" />


    <xs:element name="Properties" type="ElementList" />
    <xs:element name="Children" type="ElementList" />

    <xs:complexType name="ElementList">
        <xs:choice minOccurs="1" maxOccurs="unbounded">
            <xs:element name="BaseElement" />
            <xs:element name="DoubleElement" />
            <xs:element name="BoundedDoubleElement" />
        </xs:choice>
    </xs:complexType>

    <xs:complexType name="BaseElementType">
        <xs:sequence>
            <xs:element name="Children" type="ElementList" minOccurs="0" />
            <xs:element name="Properties" type="ElementList"
                minOccurs="0" />
        </xs:sequence>
        <xs:attribute name="ID" type="xs:positiveInteger" use="required" />
        <xs:attribute name="Key" type="xs:string" use="required" />
    </xs:complexType>

    <xs:complexType name="DoubleElementType">
        <xs:complexContent>
            <xs:extension base="BaseElementType">
                <xs:sequence>
                    <xs:element name="Value" type="xs:float" />
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="BoundedDoubleElementType">
        <xs:complexContent>
            <xs:extension base="DoubleElementType">
                <xs:sequence>
                    <xs:element name="DesiredValue" type="xs:float" />
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
</xs:schema>

这是我要验证的 XML 文件。我在几个地方插入了标签 <wrongAccepted><wrongRejected> 以演示验证在何处按预期运行以及在何处接受非法标签。

<?xml version="1.0" encoding="utf-8"?>
<t:BoundedDoubleElement ID="12" Key="test"
    xmlns:t="http://www.example.org/ElementSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.example.org/ElementSchema ElementSchema.xsd">
    <Children>
        <BaseElement ID="13" Key="child">
            <Properties>
                <DoubleElement ID="10" Key="Null">
                    <Value>-INF</Value>
                    <wrongAccepted></wrongAccepted>
                </DoubleElement>
                <wrongAccepted></wrongAccepted>
            </Properties>
        </BaseElement>
        <wrongRejected></wrongRejected>
    </Children>
    <Properties>
        <wrongRejected></wrongRejected>
        <DoubleElement ID="10" Key="Null" Role="LowerBound" Tag="-1">
            <Value>-INF</Value>
            <wrongAccepted></wrongAccepted>
        </DoubleElement>
        <DoubleElement ID="11" Key="Null" Role="UpperBound" Tag="-1">
            <Value>INF</Value>
        </DoubleElement>
    </Properties>
    <Value>10</Value>
    <DesiredValue>10</DesiredValue>
    <wrongRejected></wrongRejected>
</t:BoundedDoubleElement>

那么如何让它工作以拒绝无效标签?

注意:我正在 Eclipse 中对此进行测试。我最初在验证它时遇到了一些问题,但它现在似乎可以正常工作了。此外,Notepad++ 中的验证器会产生完全相同的结果。

改变

        <xs:element name="DoubleElement" />

        <xs:element name="DoubleElement" type="DoubleElementType"/>

否则,您实际上允许 DoubleElement 允许任何内容,因为 xs:element/@type 的默认设置(缺少 child simplyType、child complexType,并且 @substitutionGroup 属性)是 ur-type definitionanyType)。

您可能想对 ElementList 的其他 children 做同样的事情。