XSD 架构中的元素强制属性声明:
我想声明一个要包含在复杂类型声明中的元素,并且该元素具有强制属性:“option=MyOption”,但是“option”属性的值可以是任何值,具体取决于上下文。
也就是说:在任何使用包含该元素的复杂类型的文档中,具有未知值的属性“option”应该是强制的。
示例:
<xs:element name="SpecialOption" type="xs:string"/>
<xs:complexType name="SpecialOptions">
<xs:sequence>
<xs:element ref="SpecialOption" minOccurs="1" maxOccurs="100"/>
<xs:element ref="XXX"/>
</xs:sequence>
</xs:complexType>
在这种情况下,复杂类型“SpecialOptions”中的“SpecialOption”元素应具有此强制属性。
我不知道如何在 XSD 中声明元素的强制属性,或者如何指定该属性必须具有未知的值。
I want to declare an element to be included in a complex type declaration, and the element has a mandatory attribute: "option=MyOption", but the value of the "option" attribute could be anything, depending on the context.
That is: the attribute "option" with some unknown value should be mandatory in any document using the complex type containing this element.
Example:
<xs:element name="SpecialOption" type="xs:string"/>
<xs:complexType name="SpecialOptions">
<xs:sequence>
<xs:element ref="SpecialOption" minOccurs="1" maxOccurs="100"/>
<xs:element ref="XXX"/>
</xs:sequence>
</xs:complexType>
In this case the "SpecialOption" element in the complex type "SpecialOptions" should have this mandatory attribute.
I don't know how to declare a mandatory attribute for an element in XSD, or how to specify that the attribute must have a value that is not yet known.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要修改“SpecialOption”元素的定义以包含所需的属性。将此代码更新
为
:通过此更改,您的复杂类型将在“SpecialOptions”复杂类型中的“SpecialOption”元素的所有实例上包含所需的“Option”属性。将“Option”属性声明为
xs:string
类型将允许在此字段中传递任何值。You need to modify the definition of the "SpecialOption" element to include the required attribute. Update this code:
to this:
With this change your complex type will contain the required "Option" attribute on all instances of the "SpecialOption" element in the "SpecialOptions" complex type. Declaring the "Option" attribute to be of type
xs:string
will allow any value to be passed in this field.1) 这是一个简单的必需字符串属性
2) 精确地要求允许值列表中的一个:
3) 可以使用范围作为限制,如下例所示。
4) 下面,属性被声明为包含十进制值的列表。这允许属性包含指定值的子集,例如Option="6 77 95"。
5) 此处该属性被声明为可选,但提供了默认值(“test”),有时这就足够了:
1) This is a simple required string attribute
2) To require exactly one of a list of allowed values:
3) One can use a range as a restriction, like in the example below.
4) Below, the attribute is declared as a list containing decimal values. This allows an attribute to contain a subset of the specified values, e.g. Option="6 77 95".
5) Here the attribute is declared optional, but provided with a default value ("test"), which is sometimes sufficient:
要将属性标记为强制属性,请使用
。至于类型,您可以选择内置的 XSD 类型(xs:string 等),也可以定义自己的
并使用它。更新
我不确定您所说的属性必须具有未知的值是什么意思。这是否意味着该值是一个字符串,但可以是任何字符串?还是小数?
因为我们正在谈论它是一个属性值,所以您只能使用内置 XSD 类型,或者基于其中一种内置类型定义您自己的
xs:simpleType
类型。您可以在此处对允许的值应用更严格的规则,例如通过扩展xs:string
并向允许的值添加正则表达式约束。但是,如果完全无法知道将使用什么值,那么您就会遇到众所周知的时间悖论,即您无法在设计时将某些内容限制为仅在运行时知道的值。在这种情况下,肯定只需要指定该属性至少必须存在吧?
希望这能更清楚地回答您的问题。
To mark an attribute as mandatory you use
<xs:attribute use="required" />
.As for type, you have a choice of the built-in XSD types (xs:string etc), or you can define your own
<xs:simpleType />
and use that.UPDATE
I am not certain what you mean by the attribute must have a value that is not yet known. Does this mean that the value is a string, but can be any string? Or a decimal?
Because it's an attribute value we are talking about you are restricted to using the built-in XSD types, or defining your own
xs:simpleType
type based on one of the built-in types. This is where you can apply more stringent rules to the allowed value, for example by extendingxs:string
and adding a regular expression constraint to allowed values.However, if there is absolutely no way of knowing what value will be used then you have the well known temporal paradox whereby you cannot restrict something at design-time to a value you only know at run-time. In this instance, surely it is only necessary to specify that the attribute must at least be present?
<xs:attribute use="required" />
Hope this answers your question a little more clearly.
您只需按照以下代码执行此操作即可
强制在 xml 标记上插入一个值,并且空白限制将处理以从 xml 标记中删除空白。
Simply you can do it as the following
by this code you enforce to insert a value on the xml tag and also the white space restriction will handle to remove the white space from the xml tag.