Upload
evelyn-allen
View
213
Download
0
Embed Size (px)
Citation preview
XML
Document Type Definitions
and the
Document object model
Document Type Definition(DTD)
• A DTD defines the legal building blocks of an XML document.
• It defines the document structure with a list of legal elements and attributes
• A DTD can be declared inside an XML file or as an external file
DTD as an external file
• XML document<?xml version=“1.0”?>
<!DOCTYPE note SYSTEM “note.dtd”>
<note>
<to>Jimmy</to>
<from>Joey</from>
<heading>Message</heading>
<body> Hello</body>
</note>
• The DOCTYPE definition defines the external DTD
The DTD
• The file note.dtd contains the DTD. The content of the file are as follows
<!ELEMENT note (to, from, heading, body)><!ELEMENT to (#PCDATA)><!ELEMENT from (#PCDATA)><!ELEMENT heading (#PCDATA)><!ELEMENT body (#PCDATA)>
• Ordering matters here• PCDATA stands for parsed character data. This data is
parsed by the XML parser• The # is a reserved word indicator
DTD inside XML document
<?xml version=“1.0”?><!DOCTYPE note [
<!ELEMENT note (to, from, heading, body)><!ELEMENT to (#PCDATA)><!ELEMENT from (#PCDATA)><!ELEMENT heading (#PCDATA)><!ELEMENT body (#PCDATA)>
]><note>
<to>Jimmy</to><from>Joey</from><heading>Message</heading><body> Hello</body>
</note>
Why use a DTD?
• An XML file can carry a description of its own format
• If independent groups of people agree to use a standard DTD(e.g the DTD for MathML) then these groups can interchange data freely
• Your application can use a DTD to verify data from an external source
• You can verify your own data with a DTD
DTD Structure 1
• A DTD is composed of a number of declarations• Each declaration conforms to the markup
declaration format <! …..>• The components of a DTD are:
– Elements – Attributes– Entities– PCDATA– CDATA
DTD Structure 2(Elements)
• Declaring elements e.g.<!ELEMENT to (#PCDATA)><!ELEMENT emptyelt EMPTY>
• Elements with children<!ELEMENT note (to, from, heading, body)>
• Declaring exactly one occurrence of an element<!ELEMENT note (message)>
• Declaring at least one occurrence of an element<!ELEMENT note (message+)>
• Declaring zero or more occurrences of an element<!ELEMENT note (message*)>
• Declaring zero or one occurrence of an element<!ELEMENT note (message?)>
DTD Structure 3(Elements)
• Declaring either/or content<!ELEMENT note (to,from,header,(message|body))>
• Declaring mixed content<!ELEMENT note (#PCDATA|to|from|header|message)*>
DTD Structure 4 (Attributes)
• Declaring Attributes<!ATTLIST element-name attribute-name attribute-
type default-value>• Recall from the recipe.xml example from previous
lecture <ingredient amount="3" unit="cups">Flour</ingredient>
<!ATTLIST ingredient amount CDATA #REQUIRED>
<!ATTLIST ingredient unit CDATA “cups”>
DTD Structure 5 (Attributes)
• There are other attribute types but we will not consider them here
• Alternative default values– #IMPLIED– #FIXED value
Sample DTDs
• The following DTDs describe the data in the XML files considered from the previous lecture.
• These files are provided here for completeness
XML file: Book.xml
<?xml version = “1.0”?>
<!-- A first XML Example-->
<book>
<booktitle>The XML Companion</booktitle>
<author>Neil Bradley</author>
<price>$99.00</price>
<ISBN>0201674866</ISBN>
</book>
DTD: book.dtd
<!ELEMENT book (booktitle, author, price, ISBN)>
<!ELEMENT booktitle (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ELEMENT ISBN (#PCDATA)>
XML file: article.xml
<?xml version = “1.0”?><!– Format of a newspaper artice--><article>
<title>XML in Action</title><date>April 2, 2007</date><author>
<fname>Joe</fname><lname>Bloggs</lname>
</author><summary>Example of XML</summary><content>XML is a Markup Language that allows its users to specify their own tags</content>
</article>
DTD: article.dtd
<!ELEMENT article (title, date, author, summary, content)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT date (#PCDATA)>
<!ELEMENT author (fname, lname)>
<!ELEMENT fname (#PCDATA)>
<!ELEMENT lname (#PCDATA)>
<!ELEMENT summary (#PCDATA)>
<!ELEMENT content (#PCDATA)>
XML file: recipe.xml<?xml version="1.0" encoding="UTF-8"?> <recipe name="bread" prep_time="5 mins" cook_time="3 hours">
<title>Basic bread</title> <ingredient amount="3" unit="cups">Flour</ingredient> <ingredient amount="0.25“ unit="ounce"> Yeast</ingredient> <ingredient amount="1.5" unit="cups" state="warm">Water</ingredient> <ingredient amount="1" unit="teaspoon"> Salt</ingredient> <instructions>
<step>Mix all ingredients together, and knead thoroughly.</step> <step>Cover with a cloth, and leave for one hour in warm
room.</step> <step>Knead again, place in a tin, and then bake in the
oven.</step> </instructions>
</recipe>
DTD: recipe.dtd
<!ELEMENT recipe (title, ingredient+, instructions)>
<!ATTLIST recipe name CDATA #REQUIRED>
<!ATTLIST recipe prep_time CDATA #REQUIRED>
<!ATTLIST recipe cook_time CDATA #REQUIRED>
<!ELEMENT title (#PCDATA)>
<!ELEMENT ingredient (#PCDATA)>
<!ATTLIST ingredient amount CDATA #REQUIRED>
<!ATTLIST ingredient unit CDATA “cups”>
<!ELEMENT instructions (step+)>
<!ELEMENT step (#PCDATA)>
Document Object Model(DOM)
• The XML DOM defines a standard way for accessing and manipulating XML documents
• The DOM presents an XML document as a tree structure.
• Everything in a XML document is treated as a node in the DOM
Rootelement:<book>
Element:<title>
Element:<author>
Element:<Price>
Element:<ISBN>
Text:The XML
Companion
Text:Neil Bradley
Text:$99.00
Text:0201674866