XML Schema 具有不同类型的相同元素在不同序列中的选择导致类型错误

标签 xml schema

我继承了 2004 年最后一次更新的一些 XML 模式。从那时起它一直在工作,但软件更新导致了一些问题。 XML 不再在 XMLspy 中验证。

无论如何,问题似乎是当前通过定义多个具有不同名称的不同 simpleType 来定义元素的方法是不正确的。我不明白如何解决这个问题。

使用 XMLspy 2004 打开文件,xml 验证正常。

用 lates XMLspy 打开文件我得到以下错误:

Element 'Element' is not consistent with element 'Element'.
    Details:
         cos-element-consistent.2: Both type definitions ('type2' and 'type1') must have the same name

注意:以下 XML/Schema 不是实际 xml。我重新创建了它并对其进行了概括,以防止出现任何类型的问题(在这里发布实时程序的内部工作有点不受欢迎)。所以,如果您看到错别字,那是我的错,而不是架构的错。

XML:

<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <FirstElement></FirstElement>
    <SecondElement>
        <Element></Element>
        <Parameters>
            <Param value="#Value#">name1</Param>
        </Parameters>
    </SecondElement>
</Package>

我得到的产生错误的 XML 模式:

<?ml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="Package">
        <xs:complexType>
            <xs:choice>
                <xs:sequence>
                    <xs:element name="FirstElement"/>
                    <xs:element name="SecondElement">
                        <xs:complexType>
                            <xs:group ref="Group1"/>
                        </xs:complexType>
                    </xs:element>
                </xs:sequence>
                <xs:sequence>
                    <xs:element name="FirstElement"/>
                    <xs:element name="SecondElement">
                        <xs:complexType>
                            <xs:group ref="Group2" />
                        </xs:complexType>
                    </xs:element>
                </xs:sequence>
            </xs:choice>
        </xs:complextype>
    </xs:element>
    <xs:group name="Group1">
        <xs:choice>
            <xs:sequence>
                <xs:element name="Element" type="type1"/>
            </xs:sequence>
            <xs:sequence>
                <xs:element name="Element" type="type2"/>
            </xs:sequence>
            <xs:sequence>
                <xs:element name="Element" type="type3"/>
            </xs:sequence>
        </xs:choice>
    </xs:group>
    <xs:group name="Group2">
        <xs:choice>
            <xs:sequence>
                <xs:element name="Element" type="type1"/>
            </xs:sequence>
            <xs:sequence>
                <xs:element name="Element" type="type4"/>
                <xs:element name="Parameters" minOccurs="0">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="Param">
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:enumeration value="TRUE"/>
                                        <xs:enumeration value="FALSE"/>
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:choice>
    </xs:group>

    <xs:simpleType name="type1">
        <xs:restriction base="xs:string">
            <xs:enumeration value="ONE"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="type2">
        <xs:restriction base="xs:string">
            <xs:enumeration value="TWO"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="type3">
        <xs:restriction base="xs:string">
            <xs:enumeration value="THREE"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="type4">
        <xs:restriction base="xs:string">
            <xs:enumeration value="FOUR"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

如果有人能给我一些关于如何在使 XML 有效的同时保持逻辑模式检查的见解,那就太棒了。

谢谢!

最佳答案

张贴 David W 的评论作为答案,以增加新成员或访客的可读性。

You're into the complex world of "co-occurrence constraints". It's where you want to restrict an element based on the element's siblings. There are a few methods: XSL error parsing, Schematron, and XML Schema 1.1. The easiest thing to do is put the constraints as business logic in the application.
And Here is a wiki Link for further info.

关于XML Schema 具有不同类型的相同元素在不同序列中的选择导致类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13706124/

相关文章:

xml - 如何摆脱 XSLT 输出中的 xmlns =""(无命名空间)属性

Java XML 解析和原始字节偏移量

arrays - 使用 mongoose 将对象添加到数组

sql-server - 为多个表中的每一行创建全局 ID 以用于通知

mysql - 不确定如何设计有条件的一对多关系

xml - 了解 xsd :choice with minOccurs attribute

php - 从 XML 标签生成表单字段

Android最快的列表行绘图布局

Excel: "Cannot Load the specified XML or schema source"

Django 和 postgresql 模式