Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 44

Unit 3: Extensible Markup Language

(XML)
Overview
• Introduction to XML
• XML Namespaces
• Document Type Definition(DTD)
• XML Schemas
• Transforming XML into XSLT
Introduction to XML
• Xml (eXtensible Markup Language) is a mark
up language.
• XML is designed to store and transport data.
• Xml was released in late 90’s. it was created to
provide an easy to use and store self
describing data.
• XML became a W3C Recommendation on
February 10, 1998.
Introduction to XML
• XML is not a replacement for HTML.
• XML is designed to be self-descriptive.
• XML is designed to carry data, not to display
data.
• XML tags are not predefined. You must define
your own tags.
• XML is platform independent and language
independent.
Introduction to XML
Use of XML?
– It allows you to invent your own tags
– XML documents can be easily parsed
– XML is portable
Example :
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Features and Advantages of XML

1) XML separates data from HTML


• XML, data can be stored in separate XML files. This way you
can focus on using HTML/CSS for display and layout, and be
sure that changes in the underlying data will not require any
changes to the HTML.
2) XML simplifies data sharing
• In the real world, computer systems and databases contain data
in incompatible formats.
• XML data is stored in plain text format. This provides a
software- and hardware-independent way of storing data.
• This makes it much easier to create data that can be shared by
different applications.
Features and Advantages of XML

3) XML simplifies data transport


• One of the most time-consuming challenges for developers is to
exchange data between incompatible systems over the Internet.
• Exchanging data as XML greatly reduces this complexity, since
the data can be read by different incompatible applications.
4) XML simplifies Platform change
• Upgrading to new systems (hardware or software platforms), is
always time consuming. Large amounts of data must be
converted and incompatible data is often lost.
• XML data is stored in text format. This makes it easier to expand
or upgrade to new operating systems, new applications, or new
browsers, without losing data.
Features and Advantages of XML

5) XML increases data availability


• Different applications can access your data, not only in HTML pages, but
also from XML data sources.
• With XML, your data can be available to all kinds of "reading machines"
(Handheld computers, voice machines, news feeds, etc), and make it
more available for blind people, or people with other disabilities.
6) XML can be used to create new internet languages
• A lot of new Internet languages are created with XML.
• Here are some examples:
– XHTML WSDL for describing available web services
– WAP and WML as markup languages for handheld devices
– RSS languages for news feeds
– SMIL for describing multimedia for the web
HTML vs XML
1. Markup language 1. Meta-markup language to
define markup language
2. HTML Displays the data 2. XML describes the data
3. Focus: How the layout 3. Focus: meaning (what) of
looks data
4. Method of data display 4. Method of data exchange
5. Does not care about the 5. Content must be well
meaning of contents structured
6. Predefined tags 6. No predefined tags.
7. HTML is not case 7. XML is case sensitive.
sensitive.
Structure of an XML file
• XML declaration
– Value version
• Indicates the XML version to which the document conforms
• Root element
– Element that encompasses every other elements
• Container element
– Any element contains other elements
• Child elements
– Elements inside a container element
• Empty element flag
– Does not contain any text
• DTD documents
– End with .dtd extension
Example of an XML file
Comments in XML
• Comments are displayed as <!—comment–> .
It is similar to HTML. It can be used for a single
line or multiple lines.
Well Formed XML Documents
• An XML document with correct syntax is called "Well Formed".
• The syntax rules :
– If the XML declaration is present in the XML, it must be placed as
the first line in the XML document.
– XML documents must have a root element
– XML elements must have a closing tag
– XML tags are case sensitive
– XML elements must be properly nested
– XML attribute values must be quoted
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
XML Namespaces
• 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. E.g. tel, phone, mobile
• This Naming collisions Name can easily be avoided using a
name prefix.
• When using prefixes in XML, a so-called namespace for the
prefix must be defined.
• The namespace is defined by the xmlns attribute in the start
tag of an element.
• The namespace declaration has the following syntax.
xmlns:prefix="URI”
• Uniform resource identifier (URI)
– Uniquely identifies the namespace
– A string of text for differentiating names
Naming collisions
Two different elements have same name

<tiger>bengal tiger</tiger>
<tiger> glucose tiger </tiger>

Namespaces

Differentiate elements that have same name


<animal: tiger>bengal tiger</animal: tiger>
<biscuit: tiger> glucose tiger </biscuit: tiger>

animal and biscuit are namespace prefixes

• Prepended to elements and attribute names


• Tied to uniform resource identifier (URI)
• Series of characters for differentiating names
Creating namespaces

Use xmlns keyword


xmlns:animal= “urn:sxc:animaInfo”
xmlns: biscuit = “urn:sxc:foodInfo”

Creates two namespace prefixes animal and biscuit

urn:sxc:animaInfo is URI for prefix animal


urn:sxc:foodInfo is URI for prefix biscuit
EXAMPLE OF NAMESPACE
<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>
<?xml version = "1.0"?>
<!-- Fig. 20.4 : namespace.xml -->
<!-- Demonstrating Namespaces -->

<text:directory xmlns:text = "urn:sxc:textInfo"


xmlns:image = "urn:sxc:imageInfo">

<text:file filename = "book.xml">


<text:description>A book list</text:description>
</text:file>

<image:file filename = "funny.jpg">


<image:description>A funny picture</image:description>
<image:size width = "200" height = "100"/>
</image:file>
</text:directory>
Document Validation
• A "well formed" XML document is not the same as a
"valid" XML document.
• A "valid" XML document must be well formed. In
addition, it must conform to a document type definition.
• There are two different document type definitions that
can be used with XML:
1. DTD - The original Document Type Definition
2. XML Schema - An XML-based alternative to DTD
• A document type definition defines the rules and the
legal elements and attributes for an XML document.
Document Type Definition (DTD)
• DTDs check vocabulary and validity of the structure of XML
documents against grammatical rules of appropriate XML
language.
• An XML DTD can be either specified inside the document, or it
can be kept in a separate document and then liked separately.
• Allow independent user groups to check structure and
exchange data in standardized format
• Expresses set of rules for structure using EBNF (Extended
Backus–Naur Form) grammar
• There are 2 data types, PCDATA and CDATA
– PCDATA is parsed character data.
– CDATA is character data, not usually parsed.
DTD Structure
• DTD Start- The DOCTYPE declaration has an exclamation mark
(!) at the start of the element name. The DOCTYPE informs the
parser that a DTD is associated with this XML document.
<!DOCTYPE address [
• DTD Body − The DOCTYPE declaration is followed by body of
the DTD, where you declare elements, attributes, entities, and
notations.
<!ELEMENT address (name,company,phone)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT company (#PCDATA)>
<!ELEMENT phone_no (#PCDATA)>
<!ELEMENT payment (#PCDATA)>
<!ATTLIST payment type (check|cash) "cash">
DTD Structure conti..
– <!ELEMENT name (#PCDATA)> defines the
element name to be of type "#PCDATA". Here
#PCDATA means parse-able text data.
– ATTLIST attribute-list declaration Defines an
attribute
• End Declaration − Finally, the declaration
section of the DTD is closed using a closing
bracket and a closing angle bracket (]>). This
effectively ends the definition, and thereafter,
the XML document follows immediately.
XML document with an inline DTD
<?xml version="1.0"?>
<!DOCTYPE note [
main.xml
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend</body>
</note>
XML document with an inline DTD

The DTD in previous slide is interpreted like this:

• !DOCTYPE note defines that the root element of this document is


note
• !ELEMENT note defines that the note element must contain four
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"
External DTD
main.xml
<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

note.dtd
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
XML SCHEMAS Documents
• XML Schema is an XML-based (and more powerful) alternative to
DTD. It Do not use EBNF grammar
• The XML Schema language is also referred to as XML
Schema Definition (XSD).
• One of the greatest strength of XML Schemas is the
support for data types.
– It is easier to describe allowable document content
– It is easier to validate the correctness of data
– It is easier to define data facets (restrictions on data)
– It is easier to define data patterns (data formats)
– It is easier to convert data between different data types
XML SCHEMAS Documents
• XML Schemas is that are written in XML so you don't
have to learn a new language.
• You can reuse your Schema in other Schemas, Create
your own data types derived from the standard types,
reference multiple schemas in the same document
• XML Schemas Secure Data Communication.
• Well-formed documents can also contain errors, and
those errors can have serious consequences. With
XML Schemas, most of the errors can be caught by
your validating software.
XML Schema Documents
• Root element schema
– Contains elements that define the XML
document structure
– targetNamespace
• Namespace of XML vocabulary the schema defines
– element tag
• Defines element to be included in XML document
structure
– name and type attributes
• Specify element’s name and data type respectively
– Built-in simple types
• date, int, double, time, etc
Simple example of a XML Schema
<?xml version = "1.0" encoding = "UTF-8"?>
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema">
<xs:element name = "contact"> Complex types
<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" /> Simple types
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XML schema element Definition Types
• Two categories of data types
1. Simple types
• Cannot contain attributes or child elements
• Can be used only in the context of the text.
• Some of the predefined simple types are: xs:integer,
xs:boolean, xs:string, xs:date.
2. Complex types
• May contain attributes and child elements
• This allows you to specify which child elements an
element can contain and to provide some structure
within your XML documents.
<?xml version="1.0"?>
<note
note.xml
xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/
XMLSchema-instance"
xsi:schemaLocation="http://
www.w3schools.com note.xsd">
<?xml version="1.0"?>
<to>Tove</to> <xs:schema
<from>Jani</from> xmlns:xs="http://www.w3.org/2001/XMLSchema"
<heading>Reminder</heading> targetNamespace="http://www.w3schools.com"
<body>Don't forget me this xmlns="http://www.w3schools.com"
weekend!</body> elementFormDefault="qualified">
</note>
<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"/>
note.xsd <xs:element name="body" type="xs:string"/>
</xs:sequence>
that defines the elements of the XML </xs:complexType>
document above ("note.xml"): </xs:element>

</xs:schema>
Difference Between XML Schema and DTD

1. XML Schema is namespace aware, while DTD is not.


2. XML Schemas are written in XML, while DTDs are not.
3. XML Schema is strongly typed, while DTD is not.
4. XML Schema has a wealth of derived and built-in data
types that are not available in DTD.
5. XML Schema does not allow inline definitions, while DTD
does.
Transforming XML into XSLT
• XSLT (EXtensible Stylesheet Language
Transformation)
• XSLT is the most important part of XSL
• XSLT transforms an XML document into another
XML document
• XSLT uses XPath to navigate in XML documents
• XSLT is a W3C Recommendation
• The XSL code is written within the XML document
with the extension of (.xsl).
Transforming XML into XSLT
• XSLT is used to transform an XML document into another
XML document, or another type of document that is
recognized by a browser, like HTML and XHTML. Normally
XSLT does this by transforming each XML element into an
(X)HTML element.
• 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.
• A common way to describe the transformation process is
to say that XSLT transforms an XML source-tree into an
XML result-tree.
How Does transformation Work?

• In the transformation process, XSLT uses XPath


to define parts of the source document that
should match one or more predefined
templates.
• When a match is found, XSLT will transform
the matching part of the source document
into the result document.
Extensible Stylesheet Language
(XSL)
• Specify how programs should render XML
document data
– XSL-FO (XSL Formatted Objects)
• Vocabulary for specifying formatting
– XSLT (XSL Transformation)
• Source tree
• Result tree
– Xpath
• Locate parts of the source tree document that
match templates defined in the XSL stylesheet
Xpath

• XPath is a W3C recommendation.


• It is the syntax for defining parts of an XML
document.
• It uses path expressions to navigate in the XML
documents.
• XPath contains a standard function library.
• XPath is a major element of the XSLT standard.
• It is used to navigate through attributes and
elements in an XML document.
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" <xsl:stylesheet version="1.0"
href="Rule.xsl" ?> xmlns:xsl="http://www.w3.org/1999/XSL/Transform
<student> ">
<s> <xsl:template match="/">
<name> Divyank Singh Sikarwar </name> <html> <body>
<branch> CSE</branch> <h1 align="center">Students Basic Details</h1>
<age>18</age> <table border="3" align="center" >
<city> Agra </city> <tr> <th>Name</th>
</s> <th>Branch</th>
<s> <th>Age</th>
<name> Aniket Chauhan </name> <th>City</th> </tr>
<branch> CSE</branch> <xsl:for-each select="student/s">
<age> 20</age> <tr>
<city> Shahjahanpur </city> <td><xsl:value-of select="name"/></td>
</s> <td><xsl:value-of select="branch"/></td>
<s> <td><xsl:value-of select="age"/></td>
<name> Simran Agarwal</name> <td><xsl:value-of select="city"/></td>
<branch> CSE</branch> </tr>
<age> 23</age> </xsl:for-each>
<city> Buland Shar</city> </table>
</s> </body>
</html>
</xsl:template>
</xsl:stylesheet>
XML Data file main.xml
Continue on next slide … XSLT Stylesheet file Rule.xsl
<html> Continue ..
<body>
<h1 align="center">Students Basic Details</h1>
<table border="3" align="center"> Transformed File
<tr>
<th>Name</th>
<th>Branch</th>
<th>Age</th>
<th>City</th>
</tr>
<tr>
<td> Divyank Singh Sikarwar </td>
<td> CSE</td>
<td>18</td>
Output :
<td> Agra </td>
</tr>
<tr>
<td> Aniket Chauhan </td>
<td> CSE</td>
<td> 20</td>
<td> Shahjahanpur </td>
</tr>
<tr>
<td> Simran Agarwal</td>
<td> CSE</td>
<td> 23</td>
<td> Buland Shar</td>
</tr>
</table>
</body>
</html>
Commonly used XSL style sheet elements
Element Description
<xsl:apply-templates> Applies the templates of the XSL document to the children of the
current node.
<xsl:apply-templates match Applies the templates of the XSL document to the children of
= expression. The value of the attribute match (i.e., expression)
"expression"> must be some XPath expression that specifies elements.
<xsl:template> Contains rules to apply when a specified node is matched.
<xsl:value-of select = Selects the value of an XML element and adds it to the output tree of
"expression"> the transformation. The required select attribute contains an
XPath expression.
<xsl:for-each select = Implicitly contains a template that is applied to every node selected
"expression"> by the XPath specified by the select attribute.
<xsl:sort select = Used as a child element of an <xsl:apply-templates> or
"expression"> <xsl:for-each> element. Sorts the nodes selected by the
<apply-template> or <for-each> element so that the nodes
are processed in sorted order.
<xsl:output> Has various attributes to define the format (e.g., xml, html),
version (e.g., 1.2, 2.0), document type and media type of the output
document. This tag is a top-level element, which means that it can
be used only as a child element of a stylesheet.
<xsl:copy> Adds the current node to the output tree.
Question Bank
•Explain the role of XML in web application?
•Write a note on advantages of XML?
•Write a note on the features of XML?
•What is XML? Explain structure of XML.
•Give the differences between HTML and XML
•What are Namespaces? Why is it useful?
•Explain the use of DTD for structuring the XML document
Question Bank
•Explain the use of Schema for structuring the XML document
•State the differences between XML Schema and DTD
•What are presentation technologies of XML? Explain with
example briefly.
•What is Xpath?
•Explain the use of XSLT?
•What is a well formed and valid document? Explain a way to
validate a XML document
•Write a note on Transforming XML into XSLT.
Thank you 

You might also like