Please visit our sponsors !
DTD - Attributes
In a DTD, Attributes are declared with an ATTLIST declaration.
Declaring Attributes
An
attribute declaration has the following syntax:
<!ATTLIST element-name attribute-name
attribute-type default-value> example: DTD example:
<!ATTLIST payment type CDATA "check">
XML example:
<payment type="check" /> |
The attribute-type can have the following values:
Value |
Explanation |
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 default-value can have the following values:
Value |
Explanation |
value |
The attributes default value |
#DEFAULT value |
The attributes default value |
#REQUIRED |
The attribute value must be included in the element |
#IMPLIED |
The attribute does not have to be included |
#FIXED value |
The attribute value is fixed |
Attribute declaration example
DTD example:
<!ELEMENT square EMPTY>
<!ATTLIST square width CDATA "0">
XML example:
<square width="100" /> |
In the example above, the element square is defined to be an empty element with
a width attribute of type CDATA. If no width attribute is given, the width attribute
has a default
value of 0.
Default attribute value
Syntax:
<!ATTLIST element-name attribute-name
attribute-type "default-value">
DTD example:
<!ATTLIST payment type CDATA "check">
XML example:
<payment type="check" /> |
Specifying a default value for an attribute ensures 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:
<!ATTLIST contact fax CDATA #IMPLIED>
XML example:
<contact fax="555-667788" /> |
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.
Required attribute
Syntax:
<!ATTLIST element-name attribute_name
attribute-type #REQUIRED> DTD example:
<!ATTLIST person number CDATA #REQUIRED>
XML example:
<person number="5677" /> |
Use a required attribute if you don't have an option for a default value, but
still want 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" /> |
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 includes another value,
the XML parser will return an error.
Enumerated attribute values
Syntax:
<!ATTLIST element-name
attribute-name (en1|en2|..) default-value> DTD example:
<!ATTLIST payment type (check|cash) "cash">
XML example:
<payment type="check" />
or
<payment type="cash" /> |
Use enumerated attribute values when you want the attribute values to be one of
a fixed set of legal values.
Jump to: Top of Page
or HOME or
Printer friendly page
Search W3Schools:
What Others Say About Us
Does the world know about us? Check out these places:
Dogpile
Alta Vista
MSN
Google
Excite
Lycos
Yahoo
Ask Jeeves
We Help You For Free. You Can Help Us!
W3Schools is for training only. We do not warrant its correctness or its fitness for use.
The risk of using it remains entirely with the user. While using this site, you agree to have read and accepted our
terms of use and
privacy policy.
Copyright 1999-2002 by Refsnes Data. All Rights Reserved
|