根据元素值扩展XSD类型?(Extend XSD Type based on element value?)

编程入门 行业动态 更新时间:2024-10-28 00:17:03
根据元素值扩展XSD类型?(Extend XSD Type based on element value?)

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

例如:

<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元素。

对于文本字段,它可以有一个Length元素,但Date不能。

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

如果可能,我想从常见类型扩展Text和Date字段。

注意:我假设在XSD 1.0中无法实现上述功能

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

For example:

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

Both Field elements share common Title and Type elements.

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

The Date Field can have Format, but Text cannot.

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

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

最满意答案

不,元素的类型不能取决于XSD 1.0或XSD 1.1中另一个元素的值。

替代方案

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

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

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

备选方案#1是首选,可以在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.

Alternative Solutions

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>

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)

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

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.

更多推荐

<Field>,XSD,Text,Field,Date,电脑培训,计算机培训,IT培训"/> <meta name=&

本文发布于:2023-08-05 18:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1436051.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   类型   XSD   Extend   based

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!