XML DTD Xmlschemas XSLT Json Dom
XML DTD Xmlschemas XSLT Json Dom
DTDs
What is a DTD
It tells the parser that the xml document contains elements of the type
defined in DTD, and these elements are also not designed to contain
text.
<?xml version=‘1.0’ encoding=‘UTF-8’?>
<!—
DTD for a list of toys
-- >
<!ELEMENT PRODUCTDATA (PRODUCT+)>
The PRODUCTDATA
? – Optional (zero or one) CONTAINS One or More
* - Zero or more elements of type
+ - One or more <PRODUCT>
An internal DTD
<?xml version=“1.0”?>
<!DOCTYPE invoice [
<!ELEMENT invoice (sku, qty, desc, price) >
<!ELEMENT sku (#PCDATA) >
<!ELEMENT qty (#PCDATA) >
<!ELEMENT desc (#PCDATA) >
<!ELEMENT price (#PCDATA) >
}>
<invoice>
<sku>12345</sku>
<qty>55</qty>
<desc>Left handed monkey wrench</desc>
<price>14.95</price>
</invoice>
An referenced external DTD
<?xml version=“1.0”>
<invoice>
<sku>12345</sku>
<qty>55</qty>
<desc>Left handed monkey wrench</desc>
<price>14.95</price>
</invoice>
An external DTD (invoice.dtd)
<?xml version=“1.0”?>
<!ELEMENT invoice (sku, qty, desc, price) >
<!ELEMENT sku (#PCDATA) >
<!ELEMENT qty (#PCDATA) >
<!ELEMENT desc (#PCDATA) >
<!ELEMENT price (#PCDATA) >
Attributes
< <
The following entities are predefined in XML: > >
& &
" “
' ‘
Declaring an Element
• <?xml version="1.0"?>
• <!DOCTYPE note [ <!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#CDATA)>
• <!ELEMENT from (#CDATA)>
• <!ELEMENT heading (#CDATA)>
• <!ELEMENT body (#CDATA)> ]>
• <note>
• <to>Tove</to>
• <from>Jani</from>
• <heading>Reminder</heading>
• <body>Don't forget me this weekend</body>
• </note>
TV Scedule DTD
<!DOCTYPE TVSCHEDULE [
<!ELEMENT TVSCHEDULE (CHANNEL+)>
<!ELEMENT CHANNEL (BANNER, DAY+)>
<!ELEMENT BANNER (#PCDATA)>
<!ELEMENT DAY ((DATE, HOLIDAY) | (DATE, PROGRAMSLOT+))+>
<!ELEMENT HOLIDAY (#PCDATA)>
<!ELEMENT DATE (#PCDATA)>
<!ELEMENT PROGRAMSLOT (TIME, TITLE, DESCRIPTION?)>
<!ELEMENT TIME (#PCDATA)>
<!ELEMENT TITLE (#PCDATA)>
<!ELEMENT DESCRIPTION (#PCDATA)>
<!ATTLIST TVSCHEDULE NAME CDATA #REQUIRED>
<!ATTLIST CHANNEL CHAN CDATA #REQUIRED>
<!ATTLIST PROGRAMSLOT VTR CDATA #IMPLIED>
<!ATTLIST TITLE RATING CDATA #IMPLIED>
<!ATTLIST TITLE LANGUAGE CDATA #IMPLIED>
]>
XML Schema
• http://www.w3.org/TR/xmlschema-1/10/2000
• generalizes DTDs
• uses XML syntax
• two documents: structure and datatypes
• http://www.w3.org/TR/xmlschema-1
• http://www.w3.org/TR/xmlschema-2
• XML-Schema is very complex
• often criticized
• some alternative proposals
example
• <?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
purpose of an XML Schema
<xsd:complexType name=“ttt”>
[define here the type ttt]
</xsd:complexType>
Global types: can be reused in other elements
Local v.s. Global Elements in
XML Schema
• Local element:
<xsd:complexType name=“ttt”>
<xsd:sequence>
<xsd:element name=“address” type=“...”/>...
</xsd:sequence>
</xsd:complexType>
• Global element:
<xsd:element name=“address” type=“...”/>
<xsd:complexType name=“ttt”>
<xsd:sequence>
<xsd:element ref=“address”/> ...
</xsd:sequence>
</xsd:complexType>
Introduction
• DOM presents the xml document as a tree structure having the root
node as the parent element and the elements, attributes and text
defined as the child nodes.
With the Document Object Model, programmers can create and build
documents, navigate their structure, and add, modify, or delete
elements and content.
• There are two main models for reading an XML document: the event-
based model and the tree-based model. The Simple API for XML
(SAX) is an serial – parser API for reading XML documents.
• The event-based model for reading XML works as follows. There are
two interacting actors in the process: the parser, a program that
reads the XML document, and the client application, that invoked the
parser and waits for the information collected by the parser.
• XML Comments -
These events are fired at the start and end of each of these XML node,
instruction or comments whenever they are encountered
Example
• Similarities:
• Both are human readable
• Both have very simple syntax
• Both are hierarchical
• Both are language independent
• Both can be used by Ajax
• Both supported in APIs of many programming languages
• Differences:
• Syntax is different
• JSON is less verbose
• JSON can be parsed by JavaScript’s eval method
• JSON includes arrays
• Names in JSON must not be JavaScript reserved words
• XML can be validated
Content Model
Element
definition
Declaring Attributes
• XML example:
• <square width="100"></square>
Document Type Declarations
• Parenthesis (samp1, samp2) - The element must contain the sequence samp1 and samp2
• Comma (samp1,samp2,samp3) - The element must contain samp1,samp2 and samp3 in that order
HTML Examples:
<h1> align=“center”>An XML Example<h1>
<table width=page> </table>
• #REQUIRED
• The attribute must have an explicitly specified value for every occurrence of the element in the
document
• #IMPLIED
• The attribute value is not required and no default value is provided. If a value is not specified the
XMP processor must proceed without one.
• “value”
• An attrubute can be given any legal value as a default. The attribute value is not required on each
element of the document, and if it is not present it will appear to be the specified default
• #FIXED “value”
• An attribute declaration may specify that an attribute has a fixed value. In this case, the attribute is
not required, but if it occurrs, it must have the specified value. If it is not present, it will appear to
be the specified defualt
A Code sample
• Attributes
• cannot contain multipe values
• cannot be validated
• cannot describe structures like child elements can
• It is recommended to use attributes sparingly
• The following code would not be good form: