0% found this document useful (0 votes)
6 views18 pages

Understanding XML DTD and Schema

Uploaded by

kalyanneelam123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views18 pages

Understanding XML DTD and Schema

Uploaded by

kalyanneelam123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

XML –DTD (Document Type Definition),

• A Document Type Definition (DTD) describes the tree


structure of a document and something about its data. It is a
set of markup affirmations that actually define a type of
document for the SGML family, like GML, SGML, HTML, XML.
• A DTD can be declared inside an XML document as inline or
as an external recommendation. DTD determines how many
times a node should appear, and how their child nodes are
ordered.
There are 2 data types, PCDATA and CDATA
• PCDATA is parsed character data.
• CDATA is character data, not usually parsed.
XML Schema
• XML Schema is commonly known as XML
Schema Definition (XSD). It is used to describe
and validate the structure and the content of
XML data. XML schema defines the elements,
attributes and data types. Schema element
supports Namespaces. It is similar to a
database schema that describes the data in a
database.
Purpose of XML Schema
• The main goal of XML Schema is to formally
describe what an XML document can contain,
similar to how a database schema describes
the structure and constraints of tables.
• It ensures that XML data follows specific rules,
including which fields an element can include,
child element order and quantity, and what
type of content (e.g., string, integer, date) is
permitted in elements and attributes.
Key Features
• Elements and Attributes: XML Schema specifies the allowed elements, attributes,
nesting, and their order within an XML document.
• Data Types: Includes support for simple (integer, string, date) and complex
(containers of nested elements) types, allowing validation and restriction of data.
• Validation: An XML document conforming to its schema is considered "Valid." All
XML documents must be syntactically well-formed, but only those that pass
schema validation are valid for data processing.
• Extensibility and Reuse: Schema definitions are written in XML, permitting
extensibility and reuse in other schemas, as well as the derivation of new data
types or referencing multiple schemas in a single document.
• Normalization and Defaults: XML Schema can define normalization rules and
default values for elements and attributes, enhancing interoperability and data
clarity.
• Constraint Enforcement: XSD can express semantic constraints like uniqueness
and referential integrity, ensuring strict data consistency.
Example Structure
<xs:schema xmlns:xs="[Link]
<xs:element name="contact">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="company" type="xs:string" />
<xs:element name="phone" type="xs:int" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
• This schema describes an XML document in which "contact" contains "name,"
"company," and "phone" elements, strictly specifying their order and data
types.
Document Object Model (DOM)
• The Document Object Model (DOM) is the foundation of XML.
XML documents have a hierarchy of informational units
called nodes; DOM is a way of describing those nodes and the
relationships between them.
• A DOM document is a collection of nodes or pieces of
information organized in a hierarchy. This hierarchy allows a
developer to navigate through the tree looking for specific
information. Because it is based on a hierarchy of information,
the DOM is said to be tree based.
• The XML DOM, on the other hand, also provides an API that
allows a developer to add, edit, move, or remove nodes in the
tree at any point in order to create an application.
XML DOM EXAMPLE
<!DOCTYPE html>
<html>
<body>

<h2>XML DOM Example</h2>

<div>
<b>Name:</b> <span id="name"></span><br>
<b>Company:</b> <span id="company"></span><br>
<b>Phone:</b> <span id="phone"></span>
</div>

<script>
// Example XML data as a string
var xmlString = `
<contact-info>
<name>Tanmay Patil</name>
<company>TutorialsPoint</company>
<phone>(011) 123-4567</phone>
</contact-info>`;

// Parse the string into an XML DOM object


var parser = new DOMParser();
var xmlDoc = [Link](xmlString, "text/xml");

// Access elements using DOM methods and display their content


[Link]("name").innerHTML = [Link]("name")[0].childNodes[0].nodeValue;
[Link]("company").innerHTML = [Link]("company")[0].childNodes[0].nodeValue;
[Link]("phone").innerHTML = [Link]("phone")[0].childNodes[0].nodeValue;
</script>

</body>
</html>
Displaying XML file using CSS : CSS can be used to display the
contents of the XML document in a clear and precise manner.
It gives the design and style to whole XML document.
• Basic steps in defining a CSS style sheet for XML : For
defining the style rules for the XML document, the following
things should be done :-
– Define the style rules for the text elements such as font-size, color,
font-weight, etc.
– Define each element either as a block, inline or list element, using
the display property of CSS.
– Identify the titles and bold them.
Linking XML with CSS : In order to display the XML file using
CSS, link XML file with CSS. Below is the syntax for linking the
XML file with CSS:<?xml-stylesheet type="text/css"
href="name_of_css_file.css"?>
EXAMPLE
[Link]
books {
color: white;
[Link] background-color: gray;
<?xml version="1.0" encoding="UTF-8"?> width: 100%;
<?xml-stylesheet type="text/css" href=“[Link]"?> padding: 10px;
}
<books>
<heading>Welcome To XML</heading> heading {
<book> color: green;
font-size: 40px;
<title>Title -: Web Programming</title> background-color: powderblue;
<author>Author -: Chrisbates</author> padding: 10px;
<publisher>Publisher -: Wiley</publisher> }

<edition>Edition -: 3</edition> heading, title, author, publisher, edition, price {


<price>Price -: 300</price> display: block;
</book> margin: 5px 0;
}
<book>
<title>Title -: Internet world-wide-web</title> title {
<author>Author -: Ditel</author> font-size: 25px;
font-weight: bold;
<publisher>Publisher -: Pearson</publisher> }
<edition>Edition -: 3</edition>
<price>Price -: 400</price> author {
font-style: italic;
</book> }
</books>
price {
color: red;
font-weight: bold;
}
DOM (Document Object Model) Parser
• Parses the entire XML document into a tree structure
in memory.
• Allows random access and modification to any part of
the XML tree.
• Steps to use DOM parser:
– Create a DocumentBuilderFactory and DocumentBuilder.
– Parse the XML file or string to get a Document object.
– Access elements and attributes using methods
like getElementsByTagName().
• Suitable for small to medium XML documents.
DOM PARSER
DocumentBuilderFactory factory = [Link]();
DocumentBuilder builder = [Link]();
Document doc = [Link](new File("[Link]"));

Element root = [Link]();


NodeList books = [Link]("book");

for (int i = 0; i < [Link](); i++) {


Element book = (Element) [Link](i);
String title = [Link]("title").item(0).getTextContent();
String author =
[Link]("author").item(0).getTextContent();
[Link]("Title: " + title + ", Author: " + author);
}
SAX (Simple API for XML) Parser
• Parses XML document sequentially and
triggers events like startElement, endElement,
and characters.
• Does not load entire document; memory
efficient for large XML.
• Used for read-only, forward-only access.
• Requires implementing handler classes to
respond to parsing events.
SAX PARSER
import [Link];
import [Link];

public class SAXHandler extends DefaultHandler {


public void startElement(String uri, String localName, String qName, Attributes attributes) {
if ([Link]("book")) {
[Link]("Start Book:");
}
}
public void characters(char[] ch, int start, int length) {
[Link]("Characters: " + new String(ch, start, length));
}
public void endElement(String uri, String localName, String qName) {
if ([Link]("book")) {
[Link]("End Book");
}
}
}
DOM VS SAX
Extensible Stylesheet Language
Transformation
• XSLT stands for Extensible Stylesheet Language
Transformation. It is an integrated concept with an XML. It is
not used for Visual effects. However, it is used for extracting or
transforming data from XML and using the combination of
HTML and CSS to format them. It also has dynamic properties,
where you can do iteration and conditional statements upon a
static XML file.
• XSLT can be used for organizing large trees of XML elements.
So anyone can read it.
• It is used to transform XML - to - HTML
• Since XSLT is doing the transformation on the client side. The
server has to do the less work.
<!-- file name- [Link]-->
<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "[Link]"?>
<list>
<movie movieId = "01">
<moviename>Harry Potter and the Philosopher's Stone</moviename>
<genre>Fictional</genre>
<year>2001</year>
</movie>
<movie movieId = "02">
<moviename>Harry Potter and the Chamber of Secrets</moviename>
<genre>Fictional</genre>
<year>2002</year>
</movie>
<movie movieId = "03">
<moviename>Harry Potter and the Prisoner of Azkaban </moviename>
<genre>Fictional</genre>
<year>2004</year>
</movie>
</list>

You might also like