DTD and Schemas

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

XML 

DTD
An XML document with correct syntax is called "Well Formed".

An XML document validated against a DTD is both "Well Formed" and "Valid".

What is a DTD?
DTD stands for Document Type Definition.

A DTD defines the structure and the legal elements and attributes of an XML
document.

Valid XML Documents


A "Valid" XML document is "Well Formed", as well as it conforms to the rules of a
DTD:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "Note.dtd">
<note>
<to>Tove</to>
<from>Taniya</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!
</body>
</note>

The DOCTYPE declaration above contains a


reference to a DTD file. The content of the DTD file is shown
and explained below.
XML DTD
The purpose of a DTD is to define the structure and the legal elements and
attributes of an XML document:

Note.dtd:
<!DOCTYPE note
[
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>

The DTD above is interpreted like this:

 !DOCTYPE note -  Defines that the root element of the document is note
 !ELEMENT note - Defines that the note element must contain the elements:
"to, from, heading, body"
 !ELEMENT to - Defines the to element to be of type "#PCDATA"
 !ELEMENT from - Defines the from element to be of type "#PCDATA"
 !ELEMENT heading  - Defines the heading element to be of type "#PCDATA"
 !ELEMENT body - Defines the body element to be of type "#PCDATA"

Tip: #PCDATA means parseable character data.

Differences between an XML Schema Definition (XSD) and Document Type Definition (DTD)
include:

 XML schemas are written in XML while DTD are


derived from SGML syntax.
 XML schemas define datatypes for elements and
attributes while DTD doesn't support datatypes.
 XML schemas allow support for namespaces while
DTD does not.
 XML schemas define number and order of child
elements, while DTD does not.
 XML schemas can be manipulated on your own
with XML DOM but it is not possible in case of
DTD.
 using XML schema user need not to learn a new

language but working with DTD is difficult for a


user.
 XML schema provides secure data communication

i.e sender can describe the data in a way that


receiver will understand, but in case of DTD data
can be misunderstood by the receiver.
 XML schemas are extensible while DTD is not

extensible.
Not all these bullet points are 100% accurate, but
you get the gist.
On the other hand:

 DTD lets you define new ENTITY values for use in your XML file.
 DTD lets you extend it local to an individual XML file.

XML Schema
An XML Schema describes the structure of an XML document, just like a DTD.

An XML document with correct syntax is called "Well Formed".

An XML document validated against an XML Schema is both "Well Formed" and
"Valid".

XML Namespaces
XML Namespaces provide a method to avoid element name conflicts.

Name Conflicts
In XML, element names are defined by the developer. This often results in a conflict
when trying to mix XML documents from different XML applications.
This XML carries HTML table information:

<table>
  <tr>
    <td>Apples</td>
    <td>Bananas</td>
  </tr>
</table>

This XML carries information about a table (a piece of furniture):

<table>
  <name>African Coffee Table</name>
  <width>80</width>
  <length>120</length>
</table>

If these XML fragments were added together, there would be a name conflict. Both
contain a <table> element, but the elements have different content and meaning.

Namespace Declaration
A Namespace is declared using reserved attributes. Such an
attribute name must either be xmlns or begin with xmlns: shown as
below −
<element xmlns:name = "URL">
Syntax
 The Namespace starts with the keyword xmlns.
 The word name is the Namespace prefix.
 The URL is the Namespace identifier.

Example
Namespace affects only a limited area in the
document. An element containing the declaration and
all of its descendants are in the scope of the
Namespace. Following is a simple example of XML
Namespace −
<?xml version = "1.0" encoding = "UTF-8"?>
<cont:contact xmlns:cont = "www.ggits.com/profile">
<cont:name>Tanmay Patil</cont:name>
<cont:company>GGITS</cont:company>
<cont:phone>(0761)234567</cont:phone>
</cont:contact>

Solving the Name Conflict Using a Prefix


Name conflicts in XML can easily be avoided using a name prefix.

This XML carries information about an HTML table, and a piece of furniture:

<h:table>
  <h:tr>
    <h:td>Apples</h:td>
    <h:td>Bananas</h:td>
  </h:tr>
</h:table>

<f:table>
  <f:name>African Coffee Table</f:name>
  <f:width>80</f:width>
  <f:length>120</f:length>
</f:table>

In the example above, there will be no conflict because the two <table> elements
have different names.

XML Namespaces - The xmlns Attribute


When using prefixes in XML, a namespace for the prefix
must be defined.

The namespace can be defined by an xmlns attribute in the


start tag of an element.

The namespace declaration has the following syntax.


xmlns:prefix="URI".
<root>

<h:table xmlns:h="http://www.w3.org/TR/html4/">
  <h:tr>
    <h:td>Apples</h:td>
    <h:td>Bananas</h:td>
  </h:tr>
</h:table>

<f:table xmlns:f="https://www.w3schools.com/furniture">
  <f:name>African Coffee Table</f:name>
  <f:width>80</f:width>
  <f:length>120</f:length>
</f:table>

</root>

In the example above:

The xmlns attribute in the first <table> element gives the h: prefix
a qualified namespace.

The xmlns attribute in the second <table> element gives the f:


prefix a qualified namespace.

When a namespace is defined for an element, all child elements with


the same prefix are associated with the same namespace.

Namespaces can also be declared in the XML root element:

<root xmlns:h="http://www.w3.org/TR/html4/"
xmlns:f="https://www.w3schools.com/furniture">

<h:table>
  <h:tr>
    <h:td>Apples</h:td>
    <h:td>Bananas</h:td>
  </h:tr>
</h:table>

<f:table>
  <f:name>African Coffee Table</f:name>
  <f:width>80</f:width>
  <f:length>120</f:length>
</f:table>
</root>

Note: The namespace URI is not used by the parser to look up information.

The purpose of using an URI is to give the namespace a unique name.

However, companies often use the namespace as a pointer to a web page


containing namespace information.

Uniform Resource Identifier (URI)


A Uniform Resource Identifier (URI) is a string of characters which identifies an
Internet Resource.

The most common URI is the Uniform Resource Locator (URL) which identifies


an Internet domain address. Another, not so common type of URI is the Uniform
Resource Name (URN).

Default Namespaces
Defining a default namespace for an element saves us from using prefixes in all the
child elements. It has the following syntax:

xmlns="namespaceURI"

This XML carries HTML table information:

<table xmlns="http://www.w3.org/TR/html4/">
  <tr>
    <td>Apples</td>
    <td>Bananas</td>
  </tr>
</table>
This XML carries information about a piece of furniture:

<table xmlns="https://www.w3schools.com/furniture">
  <name>African Coffee Table</name>
  <width>80</width>
  <length>120</length>
</table>

Namespaces in Real Use


XSLT is a language that can be used to transform XML documents into other
formats.

The XML document below, is a document used to transform XML into HTML.

The namespace "http://www.w3.org/1999/XSL/Transform" identifies XSLT


elements inside an HTML document:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/
Transform">

<xsl:template match="/">
<html>
<body>
  <h2>My CD Collection</h2>
  <table border="1">
    <tr>
      <th style="text-align:left">Title</th>
      <th style="text-align:left">Artist</th>
    </tr>
    <xsl:for-each select="catalog/cd">
    <tr>
      <td><xsl:value-of select="title"/></td>
      <td><xsl:value-of select="artist"/></td>
    </tr>
    </xsl:for-each>
  </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

XML Schema
XML Schema is an XML-based alternative to DTD:

<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>

The Schema above is interpreted like this:

 <xs:element name="note"> defines the element called "note"


 <xs:complexType> the "note" element is a complex type
 <xs:sequence> the complex type is a sequence of elements
 <xs:element name="to" type="xs:string"> the element "to" is of type string
(text)
 <xs:element name="from" type="xs:string"> the element "from" is of type
string
 <xs:element name="heading" type="xs:string"> the element "heading" is of
type string
 <xs:element name="body" type="xs:string"> the element "body" is of type
string

XML Schemas are More Powerful than DTD


 XML Schemas are written in XML
 XML Schemas are extensible to additions
 XML Schemas support data types
 XML Schemas support namespaces
Why Use an XML Schema?
With XML Schema, your XML files can carry a description of its own format.

With XML Schema, independent groups of people can agree on a standard for
interchanging data.

With XML Schema, you can verify data.

XML Schemas Support Data Types


One of the greatest strengths of XML Schemas is the support for data types:

 It is easier to describe document content


 It is easier to define restrictions on data
 It is easier to validate the correctness of data
 It is easier to convert data between different data types

XML Schemas use XML Syntax


Another great strength about XML Schemas is that they are written in XML:

 You don't have to learn a new language


 You can use your XML editor to edit your Schema files
 You can use your XML parser to parse your Schema files
 You can manipulate your Schemas with the XML DOM
 You can transform your Schemas with XSLT

DTD vs XSD
There are many differences between DTD (Document Type Definition) and XSD (XML
Schema Definition). DTD provides less control on XML structure whereas XSD (XML schema)
provides more control.

The important differences are given below:

No. DTD XSD


1) DTD stands for Document Type Definition. XSD stands for XML Schema Definition.
2) DTDs are derived from SGML (Standard XSDs are written in XML.
Generalized Markup Language) syntax.
3) DTD doesn't support datatypes. XSD supports datatypes for elements
and attributes.
4) DTD doesn't support namespace. XSD supports namespace.
5) DTD doesn't define order for child elements. XSD defines order for child elements.

6) DTD is not extensible. XSD is extensible.


7) DTD is not simple to learn. XSD is simple to learn because you
don't need to learn new language.
8) DTD provides less control on XML structure. XSD provides more control on XML
structure.

XML and XSLT
XSLT Introduction
XSL (eXtensible Stylesheet Language) is a styling language for XML.

XSLT stands for XSL Transformations.

XSLT Example
<?xml version="1.0"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
    <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <xsl:for-each select="catalog/cd">
        <tr>
          <td><xsl:value-of select="title"/></td>
          <td><xsl:value-of select="artist"/></td>
        </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>

With XSLT you can transform an XML document into HTML.

Displaying XML with XSLT


XSLT (eXtensible Stylesheet Language Transformations) is the recommended style
sheet language for XML.

XSLT is far more sophisticated than CSS. With XSLT you can add/remove elements
and attributes to or from the output file. You can also rearrange and sort elements,
perform tests and make decisions about which elements to hide and display, and a
lot more.

XSLT uses XPath to find information in an XML document.

XSLT Example
We will use the following XML document:

<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>

<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>Two of our famous Belgian Waffles with plenty of real maple
syrup</description>
<calories>650</calories>
</food>

<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>Light Belgian waffles covered with strawberries and whipped
cream</description>
<calories>900</calories>
</food>
<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>Light Belgian waffles covered with an assortment of fresh
berries and whipped cream</description>
<calories>900</calories>
</food>

<food>
<name>French Toast</name>
<price>$4.50</price>
<description>Thick slices made from our homemade sourdough
bread</description>
<calories>600</calories>
</food>

<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>Two eggs, bacon or sausage, toast, and our ever-popular hash
browns</description>
<calories>950</calories>
</food>

</breakfast_menu>

Use XSLT to transform XML into HTML, before it is displayed in a browser:

Example XSLT Stylesheet:


<?xml version="1.0" encoding="UTF-8"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<xsl:for-each select="breakfast_menu/food">
  <div style="background-color:teal;color:white;padding:4px">
    <span style="font-weight:bold"><xsl:value-of select="name"/> - </span>
    <xsl:value-of select="price"/>
    </div>
  <div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
    <p>
    <xsl:value-of select="description"/>
    <span style="font-style:italic"> (<xsl:value-of select="calories"/> calor
ies per serving)</span>
    </p>
  </div>
</xsl:for-each>
</body>
</html>

You might also like