Part 2: Legal Building Blocks of XML
Part 2: Legal Building Blocks of XML
Valid XML:
<square width="100" />
• 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.
#REQUIRED
Syntax
• <!ATTLIST element-name attribute-name attribute-type
#REQUIRED>
Example
• DTD:
<!ATTLIST person number CDATA #REQUIRED>
Valid XML:
<person number="5677" />
Invalid XML:
<person />
• Use the #REQUIRED keyword if you don't have an option for a
default value, but still want to force the attribute to be present.
#IMPLIED
Syntax
• <!ATTLIST element-name attribute-name attribute-type #IMPLIED>
Example
• DTD:
<!ATTLIST contact fax CDATA #IMPLIED>
Valid XML:
<contact fax="555-667788" />
Valid XML:
<contact />
• Use the #IMPLIED keyword if you don't want to force the author to
include an attribute, and you don't have an option for a default
value.
#FIXED
Syntax
• <!ATTLIST element-name attribute-name attribute-type #FIXED
"value">
Example
• DTD:
<!ATTLIST sender company CDATA #FIXED "Microsoft">
Valid XML:
<sender company="Microsoft" />
Invalid XML:
<sender company="W3Schools" />
• Use the #FIXED keyword 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>
Example
DTD:
<!ATTLIST payment type (check|cash) "cash">
XML example:
<payment type="check" />
or
<payment type="cash" />
• Use enumerated attribute values when you want the
attribute value to be one of a fixed set of legal values.
•
DTD - Entities
Entities are used to define shortcuts to special characters.
• Entities can be declared internal or external.
• An Internal Entity Declaration
• Syntax
• <!ENTITY entity-name "entity-value">
• Example
• DTD Example:
<!ENTITY writer "Donald Duck.">
<!ENTITY copyright "Copyright W3Schools.">
XML example:
<author>&writer;©right;</author>
• Note: An entity has three parts: an ampersand (&), an entity
name, and a semicolon (;).
An External Entity Declaration
• Syntax
• <!ENTITY entity-name SYSTEM "URI/URL">
• Example
• DTD Example:
XML example:
<author>&writer;©right;</author>
Some Example DTD Declarations
• Example 1: The Empty Element
<!ELEMENT Bool (EMPTY)> <!--DTD declaration of empty
element-->
<Bool Value="True"></Bool> <!--Usage with attribute
in XML file-->