我有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实例(我想将“项目”元素中的文本与子元素“顺序”和“ ID”混合):

<?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 here...
  <order>2</order>
  <id>2</id>
  </item>  
</content>

我做的一切验证仍然说taht都有一个错误:

派生类型的内容类型及其基础的内容类型必须混合或仅是元素。键入“ itemType”是混合的,但其基本类型不是。

但是我可以看到两种类型 - itemType和itemTypebase混合了!

非常感谢

有帮助吗?

解决方案

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

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

在您当前的模式中 itemTypeBase 根据尊重 <xs:simpleContent> 和派生类型 itemType 尊重 <xs:complexContent> 不允许。您要么不允许子元素并使用 <xs:simpleContent> 或者您确实使用子元素并使用 <xs:complexContent>.

我个人不喜欢也不使用混合类型。如果我理解您是正确的,您想在内容中对文本进行一些限制。您想具有1到64个字符的内容长度。但 <order>2</order>, <id>2</id> 所有的空格(包括新系列字符)也是内容的一部分。如果你想要那个 <item> 有简单的内容,然后您无法将子元素插入内部。

因此,务实的解决方案将脱离混合模型,并使用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>

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>

一切都会非常简单明了。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top