使用 xs:extension 忽略元素的顺序 [英] Ignore order of elements using xs:extension

查看:23
本文介绍了使用 xs:extension 忽略元素的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设计我的 xsd 以忽略元素序列?

How can I design my xsd to ignore the sequence of elements?

<root> <a/> <b/> </root>

<root> <b/> <a/> </root>

<小时>

我需要使用 extension 来生成代码原因,所以我使用 all 尝试了以下操作:


I need to use extension for code generation reasons, so I tried the following using all:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.example.com/test"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:t="http://www.example.com/test" >

    <xs:complexType name="BaseType">
        <xs:all>
            <xs:element name="a" type="xs:string" />
        </xs:all>
    </xs:complexType>

    <xs:complexType name="ExtendedType">
        <xs:complexContent>
            <xs:extension base="t:BaseType">
                <xs:all> <!-- ERROR -->
                    <xs:element name="b" type="xs:string" />
                </xs:all>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:element name="root" type="t:ExtendedType"></xs:element>
</xs:schema>

<小时>

尽管此 xsd 无效,但在 处报告以下错误:

cos-all-limited.1.2:一个所有模型组必须出现在一个粒子中,{min出现}={max出现}=1,并且该粒子必须是构成{内容类型}的一对的一部分一个复杂的类型定义.

cos-all-limited.1.2: An all model group must appear in a particle with {min occurs} = {max occurs} = 1, and that particle must be part of a pair which constitutes the {content type} of a complex type definition.

cos-all-limited.1.2 说:

1.2 粒子的 {term} 属性,其中 {max 发生}=1,它是构成复杂类型定义的 {content type} 的对的一部分.

1.2 the {term} property of a particle with {max occurs}=1 which is part of a pair which constitutes the {content type} of a complex type definition.

我真的不明白这个(既不是 xsd 也不是英语母语 :)).

I don't really understand this (neither xsd nor English native speaker :) ).

我做错了,我做错了正确的事情,还是没有办法做到这一点?

Am I doing the wrong thing, am I doing the right thing wrong, or is there no way to achieve this?

推荐答案

MAJOR EDIT 最初我错过了您需要使用 xsd:extension 的要求.请注意,xsd:extension 的工作方式与 xsd:sequence 的基本类型内容后接扩展类型的内容一样.正如 XML Schema Primer 所说:

MAJOR EDIT Originally I missed the requirement that you need to use xsd:extension. Note that xsd:extension works as if there was xsd:sequence with contents of the base type followed by contents of the extended type. As XML Schema Primer puts it:

当一个复杂类型由扩展,其有效的内容模型是基本类型的内容模型加上中指定的内容模型类型推导.此外,该两个内容模型被视为两个连续组的孩子.

When a complex type is derived by extension, its effective content model is the content model of the base type plus the content model specified in the type derivation. Furthermore, the two content models are treated as two children of a sequential group.

因此,似乎使这项工作的唯一方法是拥有一个空的基类型并将整个替代方案存储在扩展类型中,反之亦然(基中的所有内容和空扩展).像这样:

Therefore, it seems that the only way to make this work is to have an empty base type and store the whole alternative in the extended type, or vice versa (everything in the base and an empty extension). Like this:

<xsd:complexType name="ExtendedType">
   <xsd:complexContent>
      <xsd:extension base="BaseType">
         <xsd:choice>
            <xsd:sequence>
               <xsd:element name="a" type="xsd:string"/>
               <xsd:element name="b" type="xsd:string"/>
            </xsd:sequence>
            <xsd:sequence>
               <xsd:element name="b" type="xsd:string"/>
               <xsd:element name="a" type="xsd:string"/>
            </xsd:sequence>
         </xsd:choice>
      </xsd:extension>
   </xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="BaseType"/>

<xsd:element name="root" type="ExtendedType"/>

这篇关于使用 xs:extension 忽略元素的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆