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

时间:2016-08-30 12:36:39

标签: xml xsd xsd-validation xml-validation

我正在尝试为递归数据结构定义XML Schema。基本上一个元素应该能够拥有子元素和属性,这是一个元素列表,可以反过来拥有子元素和属性。 “元素”可以是一系列类型中的一种,除了一些额外数据之外,所有类型都可能具有子项和属性。因此,元素类型是从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 ++中的验证器产生完全相同的结果。

1 个答案:

答案 0 :(得分:3)

更改

        <xs:element name="DoubleElement" />

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

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

您可能希望为ElementList的其他孩子执行相同操作。

相关问题