Extensible Markup Language (XML) Tutorial

Multiplicity, Attributes & Entities

Multiplicity of XML Elements

The ELEMENT declaration defines the content of your tags. You can specify how many times an element may appear, and the order. The following shows the multiplicity symbols used with DTD's to define how many occurrences of an element may appear in the XML document.

Multiplicity Keyword Example Description
None EMPTY
<!ELEMENT membership EMPTY>
If nothing is provided for the element, a closing tag must be provided with the start tag. <membership />
Order ,
<!ELEMENT membership (name,email)>
The comma sign determines the order the elements must appear. In this example, the user element must have one name and one email, and email must follow name.
At least once +
<!ELEMENT membership (name+,email+)>
The plus sign by the name and email elements indicates that they must occur at least once in the user element.
Zero or more *
<!ELEMENT user (name+,alias*,email+)>
The asterisk sign by the alias element indicates that it may occur zero or more times in the user element.
Optional ?
<!ELEMENT user (name+,alias?,email+)>
The question mark by the alias element indicates that it is optional in the user element.
Selection |
<!ELEMENT user ((name | nickname),email+)>
This example states that the user element may consist of either a name or nickname element, and an email element.
Mixture | and *
<!ELEMENT user ((name | alias | email)*)>
This example states that the user element may consist of zero or more of any of the elements, name, alias and email.
Any ANY
<!ELEMENT membership ANY>
This example specifies the order and content of membership are not important.

Attributes of Elements in an XML DTD

The DTD may also contain an attribute list for each element. The attributes must be unique for each element.

Example Description
<!ELEMENT membership EMPTY>
   <!ATTLIST membership level CDATA "member">

Defines an attribute of level for the membership element. If level attribute is omitted, the default value is "member".

<membership level="moderator"/>
<!ELEMENT membership EMPTY>
   <!ATTLIST membership level (member | moderator) "member">

Enumerated attribute values are used when an attribute value may be selected from a set of predefined values. The enumerated values should not be in quotes.

<!ELEMENT membership EMPTY>
   <!ATTLIST membership level CDATA #FIXED "member">

The #FIXED keyword is used when the attribute may only have one value. Any other value produces an error.

<!ELEMENT membership EMPTY>
   <!ATTLIST membership level CDATA #IMPLIED>

The #IMPLIED keyword is used when no default value exists and the attribute is optional.

<!ELEMENT membership EMPTY>
   <!ATTLIST membership level CDATA #REQUIRED>

The #REQUIRED keyword is used when no default value exists but the attribute must be provided.

<!ELEMENT user (#PCDATA)>
   <!ATTLIST user userid ID #REQUIRED>

The ID keyword specifies the attribute value must be a unique identifier. In this example, every user should have a unique userid. The value of the identifier must start with a letter, and each element may only specify one ID attribute.

<user userid="u1">guest</user>
<!ELEMENT entry (#PCDATA)>
   <!ATTLIST entry userid IDREF #REQUIRED>

The IDREF keyword specifies the attribute value must correspond to an existing ID. In this example, the userid attribute of the entry element corresponds to the user element with a corresponding userid attribute.

<entry userid="u1">Hello</entry>

XML Entities

Entities can be used in the DTD to create abbreviations and special characters. An entity is a chunk of text that may be as small as a single character, or as large as the content of a book. Entities are given names, and may be made use of using an entity reference to insert the entity into a document. The XML parser replaces the entity reference, with the entity replacement text. The following table shows the types of entities that may be defined.

Specifying XML Entities
ENTITYKeywordExampleDescription
General <!ENTITY rights "All rights reserved"> General entities define text to be expanded in the XML document. In this example, &rights; would be expanded with the text, All right reserved.
SyetemSYSTEM<!ENTITY logo SYSTEM "http://www.juicystudio.com/xml/logo.gif"> System entities allow external entities to be declared. In this example, &logo; would be expanded with the Juicy Studio URL.
Parameter%<!ENTITY % boolean ("yes | No")>

The entity can now be used each place it is required.

<!ATTLIST membership paid (%boolean;) #IMPLIED>

If you later decide to change boolean values from yes/no to true/false, the change is just made to the parameter entity.

Parameter Entities may only be used in the DTD. Parameter entities are useful when several attributes use the same attribute values, as it only requires defining once. This is particularly useful if you decide to change the values, as they will only require changing in the Entity. In this example, %boolean; is expanded to yes | no.

You might also like...

Comments

About the author

Gez Lemon United Kingdom

I'm available for contract work. Please visit Juicify for details.

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.” - Donald Knuth