根据元素值扩展 XSD 类型? [英] Extend XSD Type based on element value?

查看:37
本文介绍了根据元素值扩展 XSD 类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以根据另一个元素的值扩展 XSD 1.1 中的元素?

Is it possible to extend an element in XSD 1.1 based on another element's value?

例如:

<Field>
    <Title>Text Field</Title>
    <Type>Text</Type>
    <Length>100</Length>
 </Field>

<Field>
    <Title>Date Field</Title>
    <Type>Date</Type>
    <Format>mm/dd/yyyy</Format>
<Field>

两个 Field 元素共享共同的 Title 和 Type 元素.

Both Field elements share common Title and Type elements.

对于文本字段,它可以有 Length 元素,但日期不能.

For the Text Field, it can have a Length element, but Date cannot.

日期Field可以有Format,但Text不能.

如果可能,我想从一个通用类型扩展文本和日期字段.

I'd like to extend both Text and Date fields, if possible, from a common type.

注意:我假设以上在 XSD 1.0 中是不可能的

Note: I'm assuming the above is not possible in XSD 1.0

推荐答案

不,一个元素的类型不能依赖于 XSD 1.0 或 XSD 1.1 中另一个元素的值.

No, an element's type cannot depend upon the value of another element in XSD 1.0 or XSD 1.1.

  1. 重新设计您的 XML.而不是具有具有通用 Type 的通用 Field 元素子元素,在每个元素的名称中包含类型:

  1. Redesign your XML. Rather than have a generic Field element with a generic Type child element, include the type in the name of each element:

<Text>
    <Title>Text Field</Title>
    <Length>100</Length>
</Text>

<Date>
    <Title>Date Field</Title>
    <Format>mm/dd/yyyy</Format>
</Date>

  • Type 从元素更改为属性并使用 XSD 1.1 的 条件类型分配.例如,请参阅如何制作类型取决于使用条件类型分配的属性值.(仅限 XSD 1.1)

  • Change Type from an element to an attribute and use XSD 1.1's Conditional Type Assignment. For an example, see How to make type depend on attribute value using Conditional Type Assignment. (XSD 1.1 only)

    通过 xs:assertion 表达您的约束.(仅限 XSD 1.1)

    Express your constraints via xs:assertion. (XSD 1.1 only)

    替代#1 是首选,并且可以在 XSD 1.0 或 XSD 1.1 中轻松实现.它还可以适应从公共基类型的扩展.

    Alternative #1 is prefered and can be easily implemented in both XSD 1.0 or XSD 1.1. It can also accommodate extension from a common base type.

    这篇关于根据元素值扩展 XSD 类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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