XML Basics - More on Elements
(Page 3 of 4 )
Some elements may appear several times and some may not appear. In DTD, this multiplicity can also be defined.
+ defines one or more times
* defines zero or more times
? defines an optional element, that is one or not at all
Some examples of multiplicity include:
<!ELEMENT Customer (Name, CardInfo+) > : A Customer can have a mandatory Name sub-element and one or more CardInfo sub-elements.
<!ELEMENT Name (FirstName, MiddleName?, LastName) > : FirstName and LastName will appear one time and MiddleName may appear or not.
<!ELEMENT Purchase (ItemNo, Title, Price)* > : A Purchase element may have several sub-elements which include ItemNo, Title and Price or none at all.
Here is the complete DTD declaration for our book example.
<!DOCTYPE bookstore [
<!ELEMENT bookstore (book)*>
<!ELEMENT book (title, author, year, price)>
<!ELEMENT title (#PCDATA)>
<!ATTLIST title id CDATA "0">
<!ELEMENT author (#PCDATA)>
<!ELEMENT year (#PCDATA)>
<!ELEMENT price (#PCDATA)>
]>
DTD can be internal or external. Internal DTD means the definition, like the one above, would be specified after the XML processing instruction -- <?xml version="1.0" ?>. External DTD is a reference to an external definition like the one below.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Next: XML Schema >>
More XML Articles
More By Mamun Zaman