从混合类型派生时的 XML 架构和问题

发布于 2024-09-26 19:06:49 字数 2505 浏览 4 评论 0原文

我有以下 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”混合):

<?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>

无论我做了什么,验证仍然显示存在错误:

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

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

多谢 斯坦恩

I have following XML schema:

<?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>

which I use to validate this XML instance (I want to mix the text in the 'item' element with sub-elements 'order' and '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>

Whatever I did the validation still says taht there is an error:

The content type of a derived type and that of its base must both be mixed or both be element-only. Type 'itemType' is mixed, but its base type is not.

But I can see that both types - itemType and itemTypeBase are MIXED!!

Thanks a lot
STeN

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

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

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

在当前架构中,类型 itemTypeBase 是根据 定义的,派生类型 itemType 是根据 定义的。 > 这是不允许的。您要么不允许任何子元素并使用 ,要么使用子元素并使用

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

因此,实用的解决方案是放弃混合模型,并使用

<?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>

的 XML 文档。所有这些都会非常简单明了。

First of all the error which I see if I open your schema in Visual Studio 2010 is:

The derived type and the based type
must have the same content type.

In you current schema the type itemTypeBase is defined with respect of the <xs:simpleContent> and derived type itemType with the respect of <xs:complexContent> which is not allowed. Either you allow no sub-elements and use <xs:simpleContent> or you do use child elements and use <xs:complexContent>.

I personally don't like and don't use mixed types. If I understand you correct you want to make some restrictions in the text from the content. You want to have the content length between 1 and 64 characters. But <order>2</order>, <id>2</id> and all whitespace, inclusive the new line characters, are also a part of the content. If you want that <item> has simple content, then you can not insert child elements inside.

So the pragmatical solution would be go away from the mixed model and use the XML document in the form

<?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>

where Content.xsd is

<?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>

All will be very simple and clear.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文