如何分组类型 &XSD 中的子类型 [英] How to group types & subtypes in XSD

查看:26
本文介绍了如何分组类型 &XSD 中的子类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的 XML:

<type>
    <mainType>...</mainType>
    <subtype>...<subtype>
</type>

主要类型是受限制的 xs:strings(它们的列表).每个 mainType 都有一个可能的子类型列表.浏览器:
mainTypeA 具有以下可能的子类型:subtype1、subtype2、subtype3
mainTypeB 具有以下可能的子类型:subtype4、subtype5

The mainTypes are restricted xs:strings (a list of them). And each mainType has a list of possible subtypes. IE:
mainTypeA has possible subtypes of: subtype1, subtype2, subtype3
mainTypeB has possible subtyes of: subtype4, subtype5

我如何在 XSD 中表示这一点,以便每个子类型枚举都专门链接到它们的主要类型?有没有更好的方法来表示这两个字段以使 XSD 更简单?我不反对改变 XML 文档结构.

How can I represent this in XSD so that each of subtype enumerations are linked specifically to their main type? Is there a better way to represent those 2 fields to make the XSD simpler? I am not opposed to changing the XML document structure.

推荐答案

(这本来是@hugh jadick 的回答中的附加评论,但对于这样的评论太长了.)

(This would have been a additional comment in @hugh jadick's answer but was too long for such.)

您的初始设计中有两个问题使结构复杂化:

You have two problems in your initial design which complicates the structure:

  1. 您有一些可以被视为共现约束的东西,因为 只能在 存在时出现
  2. 元素声明不一致,因为你有
    • ...在相同的上下文中( 的孩子)
    • ...具有相同名称的元素 ()
    • ...但具有不同的类型(不同的值和相关的 元素).
  1. you have something that can be seen as a co-occurrence constraint because <subType> can appear only when <mainType> is present
  2. element declarations are inconsistent because you have
    • ...in the same context (children of <type>)
    • ...elements with same name (<mainType>)
    • ...but with different type (different value and related <subType> elements).

在 XML Schema 1.0 中,没有使这些特性成为可能的通用方法,尽管它们可以在某些情况下和/或通过某些方法实现.模糊类型问题的一种可能解决方法是在实例文档中使用 xsi:type 属性来确定使用的模式类型(示例 1).

In XML Schema 1.0 there is no general method to make those features possible although they can be achieved in some cases and/or with some methods. One possible workaround for ambiguous type problem would be to use xsi:type attribute in the instance document to determinate the used schema type (Example 1).

与其使用一些可能的解决方法来解决这些问题,我建议您遵循@hugh jadick 的 answer 并为所有主要类型创建具有不同名称的元素,这些元素对于不同的子类型也将具有不同的元素.如果您不喜欢将类型名称作为元素名称(或者它对于 XML 元素名称无效),您可以将它放在一个属性中,可能使用默认值或固定值 (<myType1 typeName="类型 1 名称">)(示例 2).如果子类型是互斥的,您也可以将它们放在一个属性中 ().如果你真的想在元素内容中有类型名称(Type 1 name),那么最好有 元素作为以下兄弟元素而不是 的子元素,因为这会导致混合内容,容易导致与空格和文本节点位置相关的问题(示例 3).

Instead of using some possible workarounds to solve those problems, I suggest that you follow @hugh jadick's answer and create elements with different names for all the main types which again will have different elements for different subtypes. If you don't like having the type name as the element name (or it is not a valid for an XML element name) you could put it in an attribute, possibly using a default or fixed value (<myType1 typeName="Type 1 name">)(Example 2). If subtypes are mutually exclusive, you could put also them in an attribute (<myType1 subType="subType2">). If you really want to have the type name in the element contents (<mainType1>Type 1 name</mainType1>), then it is probably better to have the <subType> elements as following siblings instead of children of <mainType1> because that would result in mixed content which easily causes whitespace and text node position related problems (Example 3).

是的,替换组也可以与@hugh jadick 的回答一起使用.您将拥有一个抽象的 元素,并且所有主要类型元素定义都具有 substitutionGroup="mainType" 属性.您仍然需要为不同的主要类型元素使用不同的名称,因为它们允许不同的允许元素/值集.这些元素的类型必须从它们所替代的抽象 元素的类型派生出来(示例 4).

Yes, substitution groups could also be used with @hugh jadick's answer. You would have an abstract <mainType> element and all main type element definitions had substitutionGroup="mainType" attribute. You would still need to have different names for different main type elements because they allow different set of allowed elements/values. The type of these elements must be derived from the type of the abstract <mainType> element that they substitute (Example 4).

<xs:element name="type">
    <xs:complexType>
        <xs:choice>
            <xs:element name="mainType" type="mainType1"/>
            <xs:element name="mainType" type="mainType2"/>
            <xs:element name="mainType" type="mainType3"/>
        </xs:choice>
    </xs:complexType>
</xs:element>

<type>
    <mainType xsi:type="mainType1"/>
</type>

示例 2

<xs:element name="mainType1">
    <xs:complexType>
        <xs:choice>
            <xs:element ref="subType1"/>
            <xs:element ref="subType2"/>
            <xs:element ref="subType3"/>
        </xs:choice>
        <xs:attribute name="typeName" type="xs:string"/>
    </xs:complexType>
</xs:element>

示例 3

<xs:element name="type">
    <xs:complexType>
        <xs:choice>
            <xs:sequence>
                <xs:element name="mainType" type="mainType1"/>
                <xs:choice>
                    <xs:element ref="subType1"/>
                    <xs:element ref="subType2"/>
                    <xs:element ref="subType3"/>
                </xs:choice>
            </xs:sequence>
            <!-- repeat similarily for other main types -->
            <xs:sequence>
                <!-- ... -->
            </xs:sequence>
        </xs:choice>
    </xs:complexType>
</xs:element>

示例 4

<xs:element name="type">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="mainType"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:element name="mainType" type="mainType" abstract="true"/>
<xs:element name="mainType1" type="typeForMainType1" substitutionGroup="mainType"/>
<xs:element name="mainType2" type="typeForMainType2" substitutionGroup="mainType"/>

<xs:complexType name="mainType">
<!-- definition for mainType -->
</xs:complexType>

<xs:complexType name="typeForMainType1">
    <xs:complexContent>
        <xs:restriction base="mainType">
            <!-- definition for mainType1 -->
        </xs:restriction>
    </xs:complexContent>
</xs:complexType>

<xs:complexType name="typeForMainType2">
    <xs:complexContent>
        <xs:restriction base="mainType">
            <!-- definition for mainType1 -->
        </xs:restriction>
    </xs:complexContent>
</xs:complexType>

这篇关于如何分组类型 &amp;XSD 中的子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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