Recent Posts

Friday, 23 September 2016

DTD in XML


What is cardinality operator in DTD?
     An XML element can occur zero or more times in the XML document. Cardinality operator specifies the no. of times an XML element can occur in XML document. In a DTD we have three cardinality operators.
? (0 or 1)
+ (1 or n)
* (O or n)
Note
     If a cardinality operator is not associated with the XML element, that element should occur exactly once in the XML document.

Declaring only one occurrence of the same element
<!ELEMENT element-name (child-name)>
E.g
<!ELEMELJT note (message)>
     The example declaration above declares that the child element message can only occur one time inside the note element.

Declaring minimum one occurrence of the same element
<! ELEMENT element-name (child-name+) >
E.g
<! ELEMENT note (child-name+) >
     The + sign in the above example declares that the child element message must occur one or more times inside the note element.

Declaring zero or more occurrence of the same element
<! ELEMENT element-name (child-name*) >
E.g
<! ELEMENT note (child-name*) >
     The * sign in the above example declares that the child element message must occur zero or more times inside the note element.

DTD - Attributes
Declaring Attributes
     In the DTD, XML element attributes are declared with an ATTLIST declaration. An attribute declaration has the following syntax
<!ATTLIST element-name attribute-name attribute-type default-value>
     As you can see from the syntax above, the ATTLIST declaration defines the element which can have the attribute, the name of the attribute, the type of the attribute, and the default attribute value. The attribute-type can be one of the following:
Type                 Description
CDATA          The value is character data
(en1|en2|..)   The value must be one from an enumerated list
ID             The value is a unique id
IDREF          The value is the id of another element
IDREFS         The value is a list of other ids
NMTOKEN        The value is a valid XML name
NMTOKENS       The value is a list of valid XML names
ENTITY         The value is an entity
ENTITIES       The value is a list of entities
NOTATION       The value is a name of a notation
xml            The value is a predefined xml value
     The attribute-default-value can have the following values
Value                Explanation
value         The default value of the attribute
#REQUIRED The attribute is required
#IMPLIED The attribute is optional
#FIXED value The attribute value is fixed
Attribute declaration example
DTD:

<!ELEMENT square EMPTY>
<!ATTLIST square width CDATA "0">

Valid XML:
<square width="100"></square>
     In the example above, the "square" element is defined to be an empty element with a "width" attribute of  type CDATA. If no width is specified, it has a default value of 0.

Default attribute value
Syntax
<!ATTLIST element-name attribute-name CDATA "default-value">
DTD example
<!ATTLIST payment type CDATA "check">
XML example
<payment type="check">
     Specifying a default value for an attribute, assures that the attribute will get a value even if the author of the XML document didn't include it.

Implied attribute
Syntax
<!ATTLIST element-name attribute-name attribute-type #IMPLIED>
DTD example
<!ATTLTST contact Fax CDATA #IMPLIED>
Valid XML
<contact fax="555-667788" />
Valid XML
<contact/>
     Use an implied attribute if you don't want to force the author to include an attribute and you don't have an option for a default value either.

Required attribute
Syntax
<!ATTLIST element-name attribute_name attribute-type ##REQUIRED>
DTD example
<!ATTLIST person number CDATA ##REQUIRED>
XML example
<person number="1234">
Invalid XML
<person/>
     Use a required attribute if you don't have an option for a default value, but still ant to force the attribute to be present.

Fixed attribute value
Syntax
<!ATTLIST element-name attribute_name attribute-type ##FIXED "value">
DTD example
<!ATTLIST sender company CDATA ##FIXED "Microsoft">
XML example
<sender company="Microsoft">
Invalid XML
<sender company="ashok" />
     Use a fixed attribute value when you want an attribute to have a fixed value without allowing the author to change it. If an author I includes another value, the XML parser will return an error.

Enumerated attribute values
Syntax
<!ATTLIST element-name attribute_name (eval|eval|..) default-value>
DTD example
<!ATTLIST payment type (check|cash) "cash">
XML example
<payment type="check">
or
<sender type="cash" />
     Use enumerated attribute values when you want the attribute values to be one of a fixed set of legal values.


Next Tutorial  DTD Entities Tutorial

Previous Tutorial  XML Parser Tutorial

No comments:

Post a Comment