如何多次向XML中的元素标记添加相同的属性

时间:2021-04-23 10:03:09

In an XML Schema (XSD) I am writing, I need to define an attribute which can occur multiple times inside its parent element.

在我写的XML Schema(XSD)中,我需要定义一个可以在其父元素内多次出现的属性。

Just to clear it with an example : the parent element represent events, and it supports different attributes like a title and an occurrence date for instance. One of the attributes called department is the organizing department. An event may be organized by one, or many departments.

只是用一个例子清除它:父元素代表事件,它支持不同的属性,例如标题和出现日期。其中一个称为部门的属性是组织部门。事件可以由一个或多个部门组织。

I want to know if XSD can handle multiple instances of the same attribute in an element or if this is beyond the scope of XML Standard ?

我想知道XSD是否可以处理元素中同一属性的多个实例,或者这是否超出了XML Standard的范围?

1 个解决方案

#1


11  

You can't. Attribute names are unique per element.

你不能。每个元素的属性名称是唯一的。

If you need to have multiple bits of data under the same name, then the usual solutions are either a space separated list or child elements.

如果您需要在同一名称下拥有多个数据位,那么通常的解决方案是空格分隔列表或子元素。

<event department="foo bar baz" />

or

要么

<event>
    <department>foo</department>
    <department>bar</department>
    <department>baz</department>
</event>

#1


11  

You can't. Attribute names are unique per element.

你不能。每个元素的属性名称是唯一的。

If you need to have multiple bits of data under the same name, then the usual solutions are either a space separated list or child elements.

如果您需要在同一名称下拥有多个数据位,那么通常的解决方案是空格分隔列表或子元素。

<event department="foo bar baz" />

or

要么

<event>
    <department>foo</department>
    <department>bar</department>
    <department>baz</department>
</event>