How do we define general attributes

DTD attributes



In this chapter we will discuss about DTD attributes. Attribute gives further information about an element or, more precisely, defines a property of an element. An XML attribute is always in the form of a name-value pair. An element can have any number of unique properties.

Attribute declaration is very similar to element declarations in many ways, an exception; Instead of explaining allowed content for elements, explain a list of allowed attributes for each element. These lists are called the ATTLIST statement.

syntax

Basics of the syntax of DTD attributes explanation is as follows:

<!ATTLIST element-name attribute-name attribute-type attribute-value>

In the above syntax

  • The DTD attributes begin with

  • Element names gives the name of the element to which the attribute applies.

  • Attribute names gives the name of the attribute that is included with the element name.

  • Attribute type defines the type of attributes. We'll discuss more about this in the following sections.

  • Attribute value takes a fixed value that the attributes must define. We'll discuss more about this in the following sections.

example

below is a simple example of attribute declaration in DTD:

<?xml version = "1.0"?> <!DOCTYPE address [ <!ELEMENT address ( name )> <!ELEMENT name ( #PCDATA )> <!ATTLIST name id CDATA #REQUIRED> ]> <address> <name id="123">Tanmay Patil</name> </address>

Let's go through the code above:

  • Start with the XML declaration with the following explanation:

    <?xml version = "1.0"?>
  • Immediately following the XML header is the document type declaration, commonly referred to as the DOCTYPE:

    The DOCTYPE informs the parser that a DTD is linked to this XML document. The DOCTYPE declaration with an exclamation mark (!) At the beginning of the element names.

  • the following is Leib DTD. Here we have explained elements and attributes:

  • <!ELEMENT address ( name )> <!ELEMENT name ( #PCDATA )>
  • attribute id for the element Surname is defined as:

    <!ATTLIST name id CDATA #REQUIRED>

    Here is the attribute type CDATA and its worth is #REQUIRED.

Rules of attribute declaration

  • All attributes used in an XML document must be declared in the Document Type Definition (DTD) using an attribute list declaration

  • Attributes can only be displayed in startup or empty tags.

  • The ATTLIST keyword must be in capital letters

  • allowed within

Attribute types

When attributes are declared you can specify how the processor should handle the data that appears in the value. We can group attribute types into three main categories:

  • String type

  • tokenized type

  • Enumeration types

The following table summarizes the different attribute types:

Typedescription
CDATACDATA character data (text and non-markup). It is a String attribute type.
IDIt is a clear identification of the attribute. It shouldn't appear more than once. It is a Tokenized attribute type.
IDREFIt is used to refer to an ID of another element. It is used to make connections between elements. It is a Tokenized attribute type.
IDREFSIt is used to refer to multiple IDs. It is a Tokenized attribute type.
ENTITYIt represents an external entity in the document. It is aTokenized attribute type.
ENTITIESIt represents a list of external entities in the document. It is a Tokenized attribute type.
NMTOKENIt is similar to CDATA and the attribute value consists of a valid XML name. It is a Tokenized attribute type.
NMTOKENSIt is similar to CDATA and the attribute value consists of a list of valid XML names. It is a Tokenized attribute type.
NOTATIONAn element will be referenced to a notation explained in the DTD document. It is one Enumeration attribute type.
EnumerationIt allows you to define a specific list of values ​​where one of the values ​​must match. It is one Enumeration attribute type.

Attribute value explanation

In each attribute declaration, you must specify how the value will be displayed in the document. You can specify if you have an attribute:

  • can have a default value

  • can have a fixed value

  • required

  • is suggested

Default values

It contains the default value. The values ​​can be enclosed in single quotation marks (') or double quotation marks (")

syntax

the following is the syntax value:

<!ATTLIST element-name attribute-name attribute-type "default-value">

Where Default values is the attribute value defined.

example

the following is a simple example of an attribute declaration with a default value:

<?xml version = "1.0"?> <!DOCTYPE address [ <!ELEMENT address ( name )> <!ELEMENT name ( #PCDATA )> <!ATTLIST name id CDATA "0"> ]> <address> <name id="123"> Tanmay Patil </name> </address>

In this example we have Surname Element with the attribute id whose default value is 0. The default value will have been inside the quotation marks.

Fixed values

#FIXED keyword followed by the fixed value is used when you specify that the attribute value is constant and cannot be changed. A common use of fixed attribute is the specification of version numbers.

syntax

The following is the syntax of fixed values:

<!ATTLIST element-name attribute-name attribute-type #FIXED "value" >

where #FIXED is an attribute value defined.

example

the following is a simple example of an attribute declaration with a fixed value:

<?xml version="1.0"?> <!DOCTYPE address [ <!ELEMENT address (company)*> <!ELEMENT company (#PCDATA)> <!ATTLIST company name NMTOKEN #FIXED "tutorialspoint"> ]> <address> <company name="tutorialspoint">we are a free online teaching faculty</company> </address>

In this example we have the keyword #FIXED where it shows that the value "tutorialspoint" is the only value used for the attributeSurname of element . If we try to change the attribute value then it gives an error.

the following is an invalid DTD:

<?xml version="1.0"?> <!DOCTYPE address [ <!ELEMENT address (company)*> <!ELEMENT company (#PCDATA)> <!ATTLIST company name NMTOKEN #FIXED "tutorialspoint"> ]> <address> <company name="abc">we are a free online teaching faculty</company> </address>

required values

whenever you want to indicate that an attribute is required, use the #REQUIRED keyword.

syntax

The following is the syntax of #REQUIRED:

<!ATTLIST element-name attribute-name attribute-type #REQUIRED>

where #REQUIRED is an attribute type defined.

example

the following is a simple example of DTD attribute declaration with #REQUIRED keyword:

<?xml version = "1.0"?> <!DOCTYPE address [ <!ELEMENT address ( name )> <!ELEMENT name ( #PCDATA )> <!ATTLIST name id CDATA #REQUIRED> ]> <address> <name id="123"> Tanmay Patil </name> </address>

In this example we used the #REQUIRED keyword to indicate that the attribute id must be made available for the element name Surname

IMPLICIT values

When declaring attributes you always need a declaration value. If the attribute you declare has no default value, has no fixed value, and is not required, you must declare that attribute as implies. Keyword #IMPLIED is used to specify an attribute as implies.

syntax

the following is the syntax of #IMPLIED:

<!ATTLIST element-name attribute-name attribute-type #IMPLIED>

where #IMPLIED is an attribute type defined.

example

the following is a simple example of #IMPLIED

<?xml version = "1.0"?> <!DOCTYPE address [ <!ELEMENT address ( name )> <!ELEMENT name ( #PCDATA )> <!ATTLIST name id CDATA #IMPLIED> ]> <address> <name /> </address>

In this example we have used the keyword #IMPLIED as we do not want to specify all attributes to be included in the elementSurname. It's optional.