×

XML Tutorial

XML Practice

XML Attributes

By IncludeHelp Last updated : December 25, 2024

What Are XML Attributes?

An XML attribute is used to provide additional metadata about an element. Attributes allow you to define the properties of a component in a more precise and structured manner. They are always included within the opening tag of an element.

Syntax

<element_name 
  attribute1="value1" 
  attribute2="value2">
  Contents ...
</element_name>

Here,

  • element_name: The name of the element.
  • attribute1, attribute2: Attributes with unique names.
  • value1, value2: Corresponding values of the attributes enclosed in single (') or double (") quotes.

Why use Attributes in XML?

  • Attributes provide additional metadata, making XML elements more descriptive and informative.
  • They help categories elements, enabling better organization and grouping within the document.
  • Attributes eliminate the need for creating new elements by distinguishing between elements of the same name.
  • They simplify the representation of properties by using compact name-value pairs within the element's start tag.
  • Attributes enhance data structure and facilitate easier parsing and processing in applications.

Rules for Creating XML Attributes

  1. An attribute name must be unique within the same element.
  2. Attribute values must be enclosed in quotes (" or ').
  3. Attributes must be declared in the Document Type Definition (DTD) if used.
  4. Attribute values cannot contain entity references to external entities.

Types of XML Attributes

1. String Type Attributes

These attributes accept any literal string as their value. CDATA is an example of a string-type attribute, where character data is used.

<book title="XML Basics" author="ABC" />

Explanation

  • The title and author attributes are of string type and accept literal strings as their values.
  • title="XML Basics" assigns the book's title, and author="ABC" specifies the author's name.

2. Enumerated Type Attributes

These attributes have a predefined set of values that they must match.

Notation Type

Refers to a notation declared elsewhere in the XML document.

<!DOCTYPE image [
   <!ELEMENT image EMPTY>
   <!ATTLIST image format NOTATION (jpeg | png | gif) #REQUIRED>
]>
<image format="jpeg" />

Here,

  • The format attribute is of notation type, and its value must match one of the predefined notations (jpeg, png, or gif).
  • In this example, the image format is set to jpeg.

3. Tokenized Type Attributes

ID

Used to mark an element as unique.

<employee id="E001">Raman Sharma</employee>

Here,

  • The id attribute uniquely identifies the element.
  • No other employee element in the document can have id="E001".

IDREF

References the ID of another element.

<employee id="E001">Raman Sharma</employee>
<project manager="E001">Project Alpha</project>

Here, the manager attribute in the project element references the unique ID of the employee element.

IDREFS

References multiple IDs.

<employee id="E001">Raman Sharma</employee>
<employee id="E002">Smriti Singh</employee>
<project team="E001 E002">Project Beta</project>

Here, the team attribute lists multiple id values separated by a space, referencing both employees.

NMTOKEN/NMTOKENS

Similar to CDATA but with restrictions on the allowed characters.

<user role="admin-user" />

Here,

  • The role attribute is an NMTOKEN type, which only allows alphanumeric characters, hyphens, or underscores.
  • admin-user is valid as it follows these rules.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.