L07 XML DTD XSD
L07 XML DTD XSD
● <cust>56A</cust>
● <prod>72Q</prod>
● <qty>26</qty>
● <price>7.85</price>
XML
● Using XML Document Type Definition (DTD), or XML Schema Definition (XSD),
different parties can agree on a standard XML format for interchanging data.
{
"firstName": "John",
"lastName": "Smith",
"email": "[email protected]",
"mobile": "0211223344"
}
● In most web applications, XML and JSON are used to store or transport data,
while HTML and XSLT are used to transform and display the data.
XML:
The first example of XML:
● An XML document must contain one root element that is the parent of all
other elements
<rootElement>
<child>
<subchild>.....</subchild>
</child>
</rootElement>
XML: root element
This is NOT a well-formed XML document because it has no root element
</tag>
<student_list>
...
</student_list>
or
<studentList>
...
</studentList>
XML: attribute
<tag attribute1="..." attribute2="...">
</tag>
<dailyTransaction date='24/02/2015'>
<person staffDbId="103" operation="update">
<firstName>John</firstName>
<lastName>Smith</lastName>
<mobile>0211223344</mobile>
</person>
</dailyTransaction>
XML: relationship between elements
<parent>
<child>
<subchild>.....</subchild>
</child>
</parent>
● An XML tree starts at a root element and branches from the root to child
elements.
● The terms parent, child, and sibling are used to describe the relationships
between elements.
○ Parent have children. Children have parents.
○ Siblings are children on the same level
XML: attribute vs child element
Any attribute can be defined as a child element.
Metadata (data about data) should be stored as attributes, and the data itself
should be stored as elements.
<person gender="M">
<firstName>John</firstName>
<lastName>Smith</lastName>
<email>[email protected]</email>
</person>
<person>
<firstName>John</firstName>
<lastName>Smith</lastName>
<email>[email protected]</email>
<gender>M</gender> this is better
</person>
XML: attribute vs child element
Any attribute can be defined as a child element, so when should we use attribute
and when should we use element?
Metadata (data about data) should be stored as attributes, and the data itself
should be stored as elements.
<person>
<firstName>John</firstName>
<lastName>Smith</lastName>
<mobile>0211223344</mobile>
<staffDbId>103</staffDbId>
<operation>update</operation> this is better
</person>
XML: empty element and self-closing tag
In HTML, some elements might work well, even with a missing closing tag:
<br>
<hr>
<p>
<input ...>
<student>
<firstName>John</firstName>
<lastName>Smith</lastName>
<email>[email protected]</email>
</student>
XML: entity reference
If we place a character like < inside an XML element, it will generate an error.
In this case, we need to use the entity reference <
Entity references
● Using a DTD, different parties can agree on a standard XML format for
interchanging data.
The DTD can be declared inside the XML file, or it can be defined in a separate
file:
● Internal DTD
● External DTD
DTD: internal DTD
An element is everything from the element's start tag to the element's end tag:
<firstName>John</firstName>
<lastName>Smith</lastName>
Example:
<!ELEMENT studentList (student*)>
● elementName specifies the name of the element to which the attribute applies,
● is required
<!ATTLIST elementName attributeName attributeType #REQUIRED>
● is implied: if the attribute has no default value, has no fixed value, and is not
required, then it must be declared as implied
<!ATTLIST elementName attributeName attributeType #IMPLIED>
DTD: Attribute declaration
<?xml version="1.0" ?>
<dailyTransaction date="24/02/2015">
<person staffDbId="103" operation="update">
<firstName>John</firstName>
<lastName>Smith</lastName>
<mobile>0211223344</mobile>
</person>
<person staffDbId="-1" operation="add">
<firstName>Mary</firstName>
<lastName>Jane</lastName>
<mobile>0244556677</mobile>
</person>
</dailyTransaction>
CDATA = unparsed character
data which may contain
unescaped character
● XML Schema Definition (XSD) is another way to define the legal building
blocks of an XML document. It defines the document structure with a list of
legal elements and attributes.
● Using a XSD, different parties can agree on a standard XML format for
interchanging data.
<xsd:element name="student">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="firstName" type="xsd:string"/>
<xsd:element name="lastName" type="xsd:string"/>
<xsd:element name="email" type="xsd:string"/>
<xsd:element name="mobile" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
XSD: student example
XML file:
<?xml version="1.0" ?>
<student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="student.xsd">
<firstName>John</firstName>
<lastName>Smith</lastName> elements and data types used in the schema
<email>[email protected]</email> come from the namespace
<mobile>0211223344</mobile> http://www.w3.org/2001/XMLSchema
</student>
<xsd:element name="student">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="firstName" type="xsd:string"/>
<xsd:element name="lastName" type="xsd:string"/>
<xsd:element name="email" type="xsd:string"/>
<xsd:element name="mobile" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
XSD: student example
XML file:
<?xml version="1.0" ?>
<student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="student.xsd">
<firstName>John</firstName>
<lastName>Smith</lastName> the elements and data types that come from
<email>[email protected]</email> the namespace
<mobile>0211223344</mobile> http://www.w3.org/2001/XMLSchema
</student> should be prefixed with xsd
<xsd:element name="student">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="firstName" type="xsd:string"/>
<xsd:element name="lastName" type="xsd:string"/>
<xsd:element name="email" type="xsd:string"/>
<xsd:element name="mobile" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
XSD: student example
XML file:
<?xml version="1.0" ?>
<student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="student.xsd">
<firstName>John</firstName>
<lastName>Smith</lastName>
<email>[email protected]</email>
<mobile>0211223344</mobile>
</student>
<xsd:element name="student">
Complex type <xsd:complexType>
<xsd:sequence>
<xsd:element name="firstName" type="xsd:string"/>
<xsd:element name="lastName" type="xsd:string"/>
Simple type <xsd:element name="email" type="xsd:string"/>
<xsd:element name="mobile" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
XSD: element
<result>
<mark>85</mark>
<grade>A</grade>
</result>
<xsd:element name="result">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="mark" type="xsd:integer"/>
<xsd:element name="grade" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
XSD: complex type containing element and attribute
● Element contains other elements and attributes → complexType
<scan schedule="hourly">
<start>2018-06-20T13:00:00</start>
<finish>2018-06-20T13:01:47</finish>
<virusFound>true</virusFound>
</scan>
The attribute declarations
must always come last
<xsd:element name="scan">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="start" type="xsd:dateTime" />
<xsd:element name="finish" type="xsd:dateTime" />
<xsd:element name="virusFound" type="xsd:boolean" />
</xsd:sequence>
<xsd:attribute name="schedule" type="xsd:string" />
</xsd:complexType>
</xsd:element>
XSD: complex type containing attributes only
<price promotionCode="FAMILYDEAL">39.50</price>
<xsd:element name="price">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:decimal">
<xsd:attribute name="promotionCode" type="xsd:string" />
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
XSD: simple type containing no element, no attribute
● Element contains no elements, no attributes → simpleType
<website>http://www.uow.edu.au/student</website>
<lastDayToEnrol>2000-03-24</lastDayToEnrol>
<favouriteColor>blue</favouriteColor>
<grade>B</grade>
Without restriction:
<xsd:element name="grade" type="xsd:string" />
With restriction:
<xsd:element name="grade">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="A"/>
<xsd:enumeration value="B"/>
<xsd:enumeration value="C"/>
<xsd:enumeration value="D"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
XSD: simple type with restriction
<mark>84</mark>
Without restriction:
<xsd:element name="mark" type="xsd:integer" />
With restriction:
<xsd:element name="mark">
<xsd:simpleType>
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="0"/>
<xsd:maxInclusive value="100"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<?xml version="1.0" ?>
XSD: studentList example
<studentList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="studentList.xsd">
<student>
<firstName>John</firstName>
<lastName>Smith</lastName>
<email>[email protected]</email>
</student>
<student>
<firstName>Mary</firstName>
<lastName>Jane</lastName>
<email>[email protected]</email>
</student>
</studentList>
<xsd:element name="dailyTransaction">
<xsd:complexType>
<xsd:sequence>
...
</xsd:sequence>
<xsd:attribute name="date" type="xsd:string" />
</xsd:complexType>
</xsd:element>
XSD: dailyTransaction example
<dailyTransaction date="24/02/2015">
<person staffDbId="103" operation="update">
...
</person>
<person staffDbId="-1" operation="add">
...
</person>
</dailyTransaction>
<xsd:element name="dailyTransaction">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="person" minOccurs="0" maxOccurs="unbounded">
...
</xsd:element>
</xsd:sequence>
<xsd:attribute name="date" type="xsd:string" />
</xsd:complexType>
</xsd:element>
XSD: dailyTransaction example
<person staffDbId="103" operation="update">
<firstName>John</firstName>
<lastName>Smith</lastName>
<mobile>0211223344</mobile>
</person>