【问题标题】:Abstraction in an xsdxsd 中的抽象
【发布时间】:2010-06-27 11:33:29
【问题描述】:

我正在开发 XSD。我想要一个 container 元素(复杂类型),其中包含基本类型为 component 的任何元素。

一种方法是……

<complexType name="Container">
    <sequence>
        <element name="Child" type="am:Component"></element>
    </sequence>
</complexType>

但问题是我的组件被称为子组件。假设我有 3 个组件,foobarbaz。我希望能够制作一个看起来像...的文档。

<container>
    <foo fooTag="foo"/>
    <foo fooTag="foo"/>
    <baz bazTag="baz"/>
    <bar barTag="bar"/>
</container>

第一种方法我最终会...

<container>
    <child fooTag="foo"/>
    <child fooTag="foo"/>
    <child bazTag="baz"/>
    <child barTag="bar"/>
</container>

我可以简单地使用xs:any 元素,但我会失去我的断言,即孩子必须是component。有什么方法可以得到我想要的吗?

【问题讨论】:

    标签: xsd


    【解决方案1】:

    最终你需要能够说“类型 Foo 由一个名为 foo 的元素表示”,这就是 xs:element 的 name 属性所做的。你不能做彻底的抽象(就像你在编程语言中所做的那样),因为类型只是定义并且在你给它一个特定的元素名称之前没有特定的元素名称。

    您需要列出序列中所有可能的子类型。

    <xs:sequence>
        <xs:choice>
             <xs:element name="type1" type="Type1" />
             <xs:element name="type2" type="Type2" />
             <xs:element name="type3" type="Type3" />
             <xs:element name="type4" type="Type4" />
        </xs:choice>
    </xs:sequence>
    

    您还可以全局定义元素名称并像这样引用它们,但最终您仍然需要指出哪些子元素名称在您的容器元素中是有效的。

    <xs:sequence>
        <xs:any>
             <xs:element ref="type1" />
             <xs:element ref="type2" />
             <xs:element ref="type3" />
             <xs:element ref="type4" />
        </xs:any>
    </xs:sequence>
    

    还有其他地方:

    <xs:element name="type1" type="Type1" />
    <xs:element name="type2" type="Type2" />
    <xs:element name="type3" type="Type3" />
    <xs:element name="type4" type="Type4" />
    

    【讨论】:

    • 我害怕那个。感谢您的精彩回答和示例。
    • 是的......不幸的是,从编程的角度来看,这种行为对于任何人来说都是违反直觉的。尽管这种限制在 .NET Web 服务中得到了证明 - WSDL 生成器不够智能,无法处理子类型,除非您明确告诉它给定类型的哪些子类型是有效的(通过 XmlIncludeAttribute)。
    猜你喜欢
    • 2023-03-29
    • 1970-01-01
    • 2013-05-19
    • 2011-07-23
    • 1970-01-01
    • 2012-09-16
    • 2016-03-12
    • 2012-09-21
    • 2015-08-02
    相关资源
    最近更新 更多