Extensible Markup Language Store and Transport Data
Extensible Markup Language Store and Transport Data
Ex:
<message>
<to>Meena</to>
<from>Deepa</from>
<heading>Invitation Reminder</heading>
</message>
In above example,
● HTML works with predefined tags like <p>, <h1>, <table>, etc.
● With XML, the author must define both the tags and the document structure.
● Because of this, with XML, there is a full separation between data and
presentation.
XML documents form a tree structure that starts at "the root" and branches to
"the leaves".
An XML tree starts at a root element(parent) and branches from the root to child
elements.
The terms parent, child, and sibling are used to describe the relationships between
elements.
Self-Describing Syntax:
XML uses a much self-describing syntax.
prolog defines the XML version and the character encoding:
<bookstore>
<book category="cooking">
The <book> elements have 4 child elements: <title>, <author>, <year>, <price>.
</book>
XML documents must contain one root element that is the parent of all other
elements:
<root>
<child>
<subchild>.....</subchild>
</child>
</root>
The XML prolog is optional. If it exists, it must come first in the document.
XML documents can contain international characters, like Norwegian øæå or French
êèé.
To avoid errors, you should specify the encoding used, or save your XML files as UTF-8.
Note: The XML prolog does not have a closing tag! This is not an error. The prolog is not
a part of the XML document.
Opening and closing tags must be written with the same case:
<message>This is correct</message>
<note date="12/11/2007">
<to>Tove</to>
<from>Jani</from>
</note>
Entity References
Some characters have a special meaning in XML.
If you place a character like "<" inside an XML element, it will generate an error because
the parser interprets it as the start of a new element.
XML Elements
An XML element is everything from (including) the element's start tag to (including) the
element's end tag.
● text
● attributes
● other elements
● or a mix of the above
<bookstore>
<book category="children">
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
<title>, <author>, <year>, and <price> have text content because they contain text (like
29.99).
<bookstore> and <book> have element contents, because they contain elements.
<element></element>
● Create short and simple names, like this: <book_title> not like this:
<the_title_of_the_book>.
● Avoid "-". If you name something "first-name", some software may think you
want to subtract "name" from "first".
● Avoid ".". If you name something "first.name", some software may think that
"name" is a property of the object "first".
Naming Conventions
Camel <firstName> Uppercase first letter in each word except the first (commonly
case used in JavaScript)
XML Attributes
XML elements can have attributes, just like HTML.
For a person's gender, the <person> element can be written like this:
<person gender="female">
or like this:
<person gender='female'>
If the attribute value itself contains double quotes you can use single quotes, like in this
example:
<person gender="female">
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>
<person>
<gender>female</gender>
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>
In the first example, gender is an attribute. In the last example, gender is an element.
Both examples provide the same information.
There are no rules about when to use attributes or when to use elements in XML
Features and Advantages of XML
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.
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.
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.
XML Namespaces:
XML Namespaces provide a method to avoid element name conflicts.
Declaration:
An XML namespace is declared using the reserved XML attribute. This
attribute name must be started with "xmlns".
<f:table xmlns:f="https://www.abc.com/furniture">
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
</root>
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"
<table xmlns="http://www.w3.org/TR/html4/">
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>
<table xmlns="https://www.w3schools.com/furniture">
<name>African Coffee Table</name>
<width>80</width>
<length>120</length>
</table>
XML DTD
DTD stands for Document Type Definition.
A DTD defines the structure and the legal elements and attributes of an XML document.
1.A Document Type Declaration is a statement embedded in an XML document whose
purpose is to acknowledge the existence and location of Document Type
Definition(DTD).
2.Document Type Definition is a set of rules that defines the structure of an XML
document where as a DTD is a statement that tells the parser which DTD to use for
checking and validation.
● !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"
<?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
● Elements
● Attributes
● Entities
● PCDATA
● CDATA
Elements
● Elements are the main building blocks of both XML and HTML documents.
● Examples of HTML elements are "body" and "table". Examples of XML elements
could be "note" and "message". Elements can contain text, other elements, or be
empty. Examples of empty HTML elements are "hr", "br" and "img".
Examples:
<body>some text</body>
<message>some text</message>
Attributes
Attributes provide extra information about elements.
Attributes are always placed inside the opening tag of an element. Attributes always
come in name/value pairs. The following "img" element has additional information
about a source file:
<img src="computer.gif" />
PCDATA
PCDATA means parsed character data.
Think of character data as the text found between the start tag and the end tag of an
XML element.
PCDATA is text that WILL be parsed by a parser. The text will be examined by the
parser for entities and markup.
Tags inside the text will be treated as markup and entities will be expanded.
However, parsed character data should not contain any &, <, or > characters; these need
to be represented by the & < and > entities, respectively.
CDATA
CDATA means character data.
CDATA is text that will NOT be parsed by a parser. Tags inside the text will NOT be
treated as markup and entities will not be expanded.
Declaring Attributes
An attribute declaration has the following syntax:
DTD example:
XML example:
Value Explanation
#REQUIRED
Syntax
<!ATTLIST element-name attribute-name attribute-type #REQUIRED>
#IMPLIED
Syntax
<!ATTLIST element-name attribute-name attribute-type #IMPLIED>
#FIXED
Syntax
<!ATTLIST element-name attribute-name attribute-type #FIXED "value">
Example
DTD:
<!ATTLIST payment type (check|cash) "cash">
XML example:
<payment type="check" />
or
<payment type="cash" />
Use enumerated attribute values when you want the attribute value to be one of a fixed
set of legal values.
XML Schema
An XML schema is used to define the structure of an XML document. It is like DTD
but provides more control on XML structure.
1. simpleType
2. complexType
simpleType
The simpleType allows you to have text-based elements. It contains less attributes,
child elements, and cannot be left empty.
complexType
The complexType allows you to hold multiple attributes and elements. It can
contain additional sub elements and can be left empty.
ex:
<xs:element name="note">
<xs:complexType>
<xs:sequence>
</xs:sequence>
</xs:complexType>
</xs:element>
<?xml version="1.0"?>
<xs:schema>
...
...
</xs:schema>
ex:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="https://www.library.com"
xmlns="https://www.library.com"
elementFormDefault="qualified">
...
...
</xs:schema>
XML Schema creates the new elements and attributes and puts it in a namespace
called {target namespace}. But what if we don't specify a {target namespace} in
the schema? When we don't specify the attribute targetNamespace at all, no
{target namespace} exists which is legal but specifying an empty URI in the
targetNamespace attribute is "illegal."
1.xmlns:xs="http://www.w3.org/2001/XMLSchema"
indicates that the elements and data types used in the schema come from the
"http://www.w3.org/2001/XMLSchema" namespace. It also specifies that the
elements and data types that come from the
"http://www.w3.org/2001/XMLSchema" namespace should be prefixed with xs:
2.targetNamespace="https://www.library.com"
indicates that the elements defined by this schema (note, to, from, heading,
body.) come from the "https://www.library.com" namespace.
3.xmlns="https://www.library.com"
4.elementFormDefault="qualified"
indicates that any elements used by the XML instance document which were
declared in this schema must be namespace qualified.
Defining a schema:
Schemas are written from a namespace(schema of schemas): The name of this
namespace is
http://www.w3.org/2001/XMLSchema
element, schema, sequence and string are some names from this namespace
Every XML
The schema element must specify the namespace for the schema of schemas
from which the schema‘s
elements and its attributes will be drawn. It often specifies a prefix that will be
used for the names in the
xmlns:xsd = http://www.w3.org/2001/XMLSchema
Every XML schema itself defines a tag set like DTD, which must be named with
the targetNamespace
targetNamespace = http://cs.uccs.edu/planeSchema
Every top-level element places its name in the target namespace If we want to
include nested elements(ie if
the names of the elements and attributes that are not defined directly in the
schema element), we must set the
elementFormDefault = “qualified”
The default namespace which is source of the unprefixed names in the schema is
given
xmlns = "http://cs.uccs.edu/planeSchema “
<xsd:schema
xmlns:xsd = http://www.w3.org/2001/XMLSchema
xmlns = http://cs.uccs.edu/planeSchema.
One alternative to the preceding opening tag would be to make the XMLSchema
names the default so that
they do not need to be prefixed in the schema. Then the names in the target
namespace would need to be
targetNamespace = “http://cs.uccs.edu/planeSchema”
xmlns:plane =“http://cs.uccs.edu/planeSchema”
elementformdefault= “qualified”>
where xxx is the name of the element and yyy is the data type of the element.
XML Schema has a lot of built-in data types. The most common types are:
● xs:string
● xs:decimal
● xs:integer
● xs:boolean
● xs:date
● xs:time
XML Schema defines 44 data types .19 of which are primitive and 25 of which
are derived.
maxInclusive,,maxExclusive etc.
XML schema
ex: <lastname>Refsnes</lastname>
<age>36</age>
<dateborn>1970-03-27</dateborn>
• Elements are defined in an XML schema with the element tag, which is from the
XMLSchema namespace. Recall that the prefix xsd is normally used for names
from this namespace. Use the element tag and set the name and type attributes
• An element can be given default value using default attribute .Default value is
automatically assigned to the element when no other value is specified.
• An element can have constant value, using fixed attribute .A fixed value is
automatically assigned to the element , and we cannot specify another value.
Restrictions are used to define acceptable values for XML elements or attributes.
Facets values are specified with the value attribute.
</xsd:restriction>
</xsd:simpleType>
The length facet is used to restrict the string to an exact number of characters.
The minLength facet is used to specify a minimum length.
</xsd:restriction>
</xsd:simpleType>
• There are several categories of complex types, but we discuss just one,
element-only elements which can have elements in their content, but no text.
Element-only elements are defined with the complexType tag.
• Nested elements can include attributes that give the allowed number of
occurrences
• For ex:
<xsd:sequence>
</xsd:sequence>
</xsd:complexType>
A default value is automatically assigned to the element when no other value is
specified.
A fixed value is also automatically assigned to the element, and you cannot
specify another value.
Attributes are optional by default. To specify that the attribute is required, use the
"use" attribute:
It doesn’t support
2. It supports namespace.
namespace.
11. File here is saved as .dtd File in XSD is saved as .xsd file.
XSLT Introduction
XSL (eXtensible Stylesheet Language) is a styling language for XML.
XSLT - Transformation:
The root element that declares the document to be an XSL style sheet is
<xsl:stylesheet> or <xsl:transform>.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
or:
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<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>
<tr>
<td>.</td>
<td>.</td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
The <xsl:value-of> element can be used to extract the value of an XML element and add
it to the output stream of the transformation:
ex:
<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>
<tr>
<td><xsl:value-of select="catalog/cd/title"/></td>
<td><xsl:value-of select="catalog/cd/artist"/></td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
<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>
<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">
<xsl:sort select="artist"/>
<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>
Syntax
<xsl:if test="expression">
</xsl:if>
To add a conditional test, add the <xsl:if> element inside the <xsl:for-each>
element in the XSL file:
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>
<th>Price</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
<td><xsl:value-of select="price"/></td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Note: The value of the required test attribute contains the expression
to be evaluated.
The code above will only output the title and artist elements of the
CDs that has a price that is higher than 10.
<xsl:choose>
<xsl:when test="expression">
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
Where to put the Choose Condition
To insert a multiple conditional test against the XML file, add the
<xsl:choose>, <xsl:when>, and <xsl:otherwise> elements to the XSL file:
<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>
<xsl:choose>
<td bgcolor="#ff00ff">
<xsl:value-of select="artist"/></td>
</xsl:when>
<xsl:otherwise>
<td><xsl:value-of select="artist"/></td>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="cd">
<p>
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="artist"/>
</p>
</xsl:template>
<xsl:template match="title">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
<xsl:template match="artist">
Artist: <span style="color:#00ff00">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
</xsl:stylesheet>
DTD vs XSD
N DTD XSD
o.
1) DTD s ta nds for Document XSD s ta nds for XML Schema Definition.
Type Definition.
s ynta x.
datatypes . a ttributes .
names pace.
5) DTD does n't define order for XSD defines order for child elements .
child elements .
6) DTD is not extens ible. XSD is extens ible.
7) DTD is not s imple to learn. XSD is s imple to learn beca us e you don't