Xsd XML模式和从混合类型派生时的问题

Xsd XML模式和从混合类型派生时的问题,xsd,xsd-validation,Xsd,Xsd Validation,我有以下XML模式: <?xml version="1.0" encoding="UTF-8" standalone="no"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="content" type="contentType"/> <xs:complexType name="contentType"> &l

我有以下XML模式:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="content" type="contentType"/>

    <xs:complexType name="contentType">
        <xs:complexContent>
            <xs:extension base="versionedElementType">  
                <xs:sequence>   
                    <xs:element name="item" type="itemType" minOccurs="1" maxOccurs="unbounded" />
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="itemType" mixed="true"> 
        <xs:complexContent>
            <xs:extension base="itemTypeBase">
                    <xs:sequence>   
                        <xs:element name="order" type="xs:unsignedInt"/>
                        <xs:element name="id" type="xs:string"/>
                    </xs:sequence>
            </xs:extension> 
        </xs:complexContent>
    </xs:complexType>

    <!-- Simple type convert to complex type -->
    <xs:complexType name="itemTypeBase" mixed="true">
        <xs:simpleContent>  
            <xs:extension base="itemDescriptionType">
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- Simple type -string restriction -->
    <xs:simpleType name="itemDescriptionType" >
        <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
            <xs:maxLength value="64"/>
        </xs:restriction>   
    </xs:simpleType>


    <xs:complexType name="versionedElementType">
        <xs:attribute name="version" type="xs:string" use="required"/>
    </xs:complexType>

</xs:schema>

我使用它来验证这个XML实例(我想将“item”元素中的文本与子元素“order”和“id”混合在一起):


描述在这里。。。
2.
2.
无论我做了什么,验证仍然表明存在错误:

派生类型的内容类型及其基的内容类型必须是混合的,或者两者都是元素。类型“itemType”是混合的,但其基类型不是。

但是我可以看到这两种类型-itemType和itemTypeBase是混合的

非常感谢
STeN

首先,如果在Visual Studio 2010中打开您的架构,我会看到以下错误:

派生类型和基于类型 必须具有相同的内容类型

在当前架构中,类型
itemTypeBase
是针对
定义的,派生类型
itemType
是针对
定义的,这是不允许的。要么不允许使用子元素并使用
,要么使用子元素并使用

我个人不喜欢也不使用混合类型。如果我理解你是正确的,你想从内容上对文本进行一些限制。您希望内容长度介于1到64个字符之间。但是
2
2
和所有空格(包括新行字符)也是内容的一部分。如果希望
具有简单的内容,则不能在其中插入子元素

因此,实用的解决方案是摆脱混合模型,在表单中使用XML文档

<?xml version="1.0" encoding="utf-8"?>
<content xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="Content.xsd"
         version ="1.0">
    <item>
        <description>Description here...</description>
        <order>2</order>
        <id>2</id>
    </item>
</content>

描述在这里。。。
2.
2.
其中Content.xsd是

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

    <xs:element name="content" type="contentType"/>

    <xs:complexType name="contentType">
        <xs:sequence>   
            <xs:element name="item" type="itemType"
                        minOccurs="1" maxOccurs="unbounded" />
        </xs:sequence>
        <xs:attribute name="version" type="xs:string" use="required"/>
    </xs:complexType>

    <xs:complexType name="itemType"> 
        <xs:sequence>
            <xs:element name="description" type="itemDescriptionType"/>
            <xs:element name="order" type="xs:unsignedInt"/>
            <xs:element name="id" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>

    <xs:simpleType name="itemDescriptionType" >
        <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
            <xs:maxLength value="64"/>
        </xs:restriction>   
    </xs:simpleType>

</xs:schema>


所有这些都将非常简单和清晰。

首先,如果我在Visual Studio 2010中打开您的架构,我看到的错误是:

派生类型和基于类型 必须具有相同的内容类型

在当前架构中,类型
itemTypeBase
是针对
定义的,派生类型
itemType
是针对
定义的,这是不允许的。要么不允许使用子元素并使用
,要么使用子元素并使用

我个人不喜欢也不使用混合类型。如果我理解你是正确的,你想从内容上对文本进行一些限制。您希望内容长度介于1到64个字符之间。但是
2
2
和所有空格(包括新行字符)也是内容的一部分。如果希望
具有简单的内容,则不能在其中插入子元素

因此,实用的解决方案是摆脱混合模型,在表单中使用XML文档

<?xml version="1.0" encoding="utf-8"?>
<content xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="Content.xsd"
         version ="1.0">
    <item>
        <description>Description here...</description>
        <order>2</order>
        <id>2</id>
    </item>
</content>

描述在这里。。。
2.
2.
其中Content.xsd是

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

    <xs:element name="content" type="contentType"/>

    <xs:complexType name="contentType">
        <xs:sequence>   
            <xs:element name="item" type="itemType"
                        minOccurs="1" maxOccurs="unbounded" />
        </xs:sequence>
        <xs:attribute name="version" type="xs:string" use="required"/>
    </xs:complexType>

    <xs:complexType name="itemType"> 
        <xs:sequence>
            <xs:element name="description" type="itemDescriptionType"/>
            <xs:element name="order" type="xs:unsignedInt"/>
            <xs:element name="id" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>

    <xs:simpleType name="itemDescriptionType" >
        <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
            <xs:maxLength value="64"/>
        </xs:restriction>   
    </xs:simpleType>

</xs:schema>


所有这些都将非常简单和清晰。

您好,首先我想将文本与元素混合,但由于我无法找到适合它的XML模式,因此我采用了您在那里展示的类似解决方案。我必须百分之百地同意你的意见,那就是它要简单明了得多。感谢您的时间和良好的回复。BR STeNHi,首先我想将文本与元素混合,但是因为我无法找到适合它的XML模式,所以我使用了您在那里展示的类似解决方案。我必须百分之百地同意你的意见,那就是它要简单明了得多。感谢您的时间和良好的回复。布尔斯滕