Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add the same attribute multiple times to an Element Tag in XML

Tags:

xml

schema

xsd

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

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 ?

like image 934
Noobie Avatar asked Sep 30 '12 19:09

Noobie


People also ask

How can attributes take multiple values in XML?

attributes cannot contain multiple values (elements can) attributes cannot contain tree structures (elements can) attributes are not easily expandable (for future changes)

Can an XML element have multiple attributes?

An element can have multiple unique attributes. Attribute gives more information about XML elements. To be more precise, they define properties of elements. An XML attribute is always a name-value pair.

How do you add an element to an attribute in XML?

Get the element node and use SetAttribute to add an attribute to the attribute collection of that element. Create an XmlAttribute node using the CreateAttribute method, get the element node, then use SetAttributeNode to add the node to the attribute collection of that element.


1 Answers

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>
like image 88
Quentin Avatar answered Sep 25 '22 10:09

Quentin