Unit 2 - XML
Unit 2 - XML
XML
Contents
Introduction to SGML – features of XML - XML as a
subset of SGML – XML Vs HTML – Views of an XML
document - Syntax of XML- XML Document Structure
– Namespaces- XML Schemas- simple XML documents
– Different forms of markup that can occur in XML
documents - Document Type declarations – Creating
XML DTDs – Displaying XML Data in HTML browser –
Converting XML to HTML with XSL minimalist XSL style
sheets – XML applications
SGML(ISO 8879)
• Standard Generalized Markup Language is a meta-
markup language which means it allows us to create
our own language or our own tags.
• The international standard for defining descriptions of
structure and content in text documents.
• Interchangeable: device-independent, system-
independent, tags are not predefined.
• Using DTD to validate the structure of the document
• Large, powerful, and very complex
• Heavily used in industrial and commercial usages for
over a decade
XML Versions
5) HTML has its own predefined tags. You can define tags according to your
need.
6) In HTML, it is not necessary to use a XML makes it mandatory to use a
closing tag. closing tag.
7) HTML is static because it is used to display XML is dynamic because it is used to
data. transport data.
Elements:
• Elements are used to mark up the sections of an XML document. Elements
are the basic units used to identify and describe the data in XML. They are
the building blocks of an XML document. Elements are represented using
tags. An XML element has the following form:
<ElementName>Content</ElementName>
• The content is contained within the XML tags. Content refers to the
information represented by the elements of an XML document.
• Empty Elements:Although XML tags usually enclose content, you can also have
elements that have no content, called empty elements. In XML, an empty element can
be represented as follows:
<ElementName/>
• Nested Elements:Elements can be nested. For example, if you wanted to group all the
patient information under a single Patient element, you might want to rewrite the
patient record example as follows:
<Patient>
<PatientName>John Smith</PatientName>
<PatientAge>108</PatientAge>
<PatientWeight>155</PatientWeight>
</Patient>
• Thus XML elements can contain other elements. However, the elements must be
strictly nested: each start tag must have a corresponding end tag.
• Attributes: Attributes provide additional information about the elements for which
they are declared. An attribute consists of a name-value pair. Consider the following
example:
<Student_name S_ID = “101”>shanshak </ Student_name >
Control Information:
This gives control to the XML document.
• Comments: Comments are statements used to explain the XML code. They are used to
provide documentation information about the XML file or the application to which the file
belongs. The parser ignores comments entries during code execution.
<!--Comment here-->
• Processing Instructions: An XML Documents usually begins with the XML declaration
statement called the Processing Instructions .This statement provides information on how
the XML file should be processed.
e.g. <?xml version =”1.0” encoding=”UTF-8”?>
The Processing Instruction statement uses the encoding property to specify the encoding
scheme used to create the XML file
• DTD:DTD stands for Document Type Definition. It defines the legal building blocks of an
XML document. It is used to define document structure with a list of legal elements and
attributes. Each XML has associated DTD held in a separate file so that it can be used with
many document. DTD file holds the rules of grammar for a particular XML datastructure.
Rules are used by validating parsers to check.
<!DOCTYPE employee SYSTEM "employee.dtd">
• CDATA: (Unparsed Character data): CDATA contains the text
which is not parsed further in an XML document. Tags inside
the CDATA text are not treated as markup and entities will not
be expanded.
<?xml version="1.0"?>
<!DOCTYPE employee SYSTEM "employee.dtd">
<employee>
<![CDATA[
<firstname>vimal</firstname>
<lastname>jaiswal</lastname>
<email>[email protected]</email>
]]>
</employee>
• PCDATA: (Parsed Character Data): XML parsers are used to parse all the text in an XML document.
PCDATA stands for Parsed Character data. PCDATA is the text that will be parsed by a parser. Tags
inside the PCDATA will be treated as markup and entities will be expanded.
employee.xml
<?xml version="1.0"?>
<!DOCTYPE employee SYSTEM "employee.dtd">
<employee>
<firstname>vimal</firstname>
<lastname>jaiswal</lastname>
<email>[email protected]</email>
</employee>
employee.dtd
<!ELEMENT employee (firstname,lastname,email)>
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT lastname (#PCDATA)>
<!ELEMENT email (#PCDATA)>
Entities:
An entity is a name that is associated with a block of data, such as chunk of text or a reference to
an external file that contains textual or binary information. It is a set of information that can be
specifying a single name.
XML Document Structure
XML document is a tree structure which contain exactly one root element
which is the start tag of the XML document and it contains all other elements.
All elements in an XML document can contain sub elements, text and
attributes. The tree represented by an XML document starts at the root
element and branches to the lowest level of elements.
<root>
<section>
<sub-section></sub-section>
<sub-section></sub-section>
</section>
<section>
<sub-section></sub-section>
<sub-section></sub-section>
</section>
<root>
XML Declaration
Demerits of DTD:
1.The DTDs are very basic and hence cannot be much specific for complex document.
2.The language that DTD uses is not an XML document. Hence various frameworks
used by XML cannot be supported by the DTDs.
3.The DTD cannot define the type of data contained within the XML document. Hence
we cannot specify whether the element is numeric or string data type.
4.There are some XML processor which do not understand DTDs.
5.The DTDs are not aware of namespace concept. It does not support the namespaces
XML Schemas
• XML Schema is commonly known as XML Schema
Definition (XSD) which is a language ffor XML Schema.
• 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.
• It also allows the developer to use data types.
• This can be used as an alternative to XML DTD.
DTD vs XSD
No. DTD XSD
<?xml version="1.0"?>
<employee
xmlns="http://www.javatpoint.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.javatpoint.com employee.xsd">
<firstname>vimal</firstname>
<lastname>jaiswal</lastname>
<email>[email protected]</email>
</employee>
Global Types
• With the global type, you can define a single type in your document, which
can be used by all other references. For example, suppose you want to
generalize the person and company for different addresses of the company.
In such case, you can define a general type as follows −
Example:
<xsl:for-each select="class/employee">
<tr>
<td>
<!-- value-of processing instruction
process the value of the element matching the XPath expression
-->
<xsl:value-of select = "@id"/>
</td>
<td><xsl:value-of select = "firstname"/></td>
<td><xsl:value-of select = "lastname"/></td>
<td><xsl:value-of select = "nickname"/></td>
<td><xsl:value-of select = "salary"/></td>
</tr>
</xsl:for-each>
XSLT Elements
XSLT <xsl:sort> Element
• The XSLT <xsl:sort> element is used to specify a sort criteria on the nodes. It displays the
output in sorted form.
• The <xml:sort> element is added inside the <xsl:for-each> element in the XSL file, to sort the
output.
<xsl:sort
select = string-expression
lang = { nmtoken }
data-type = { "text" | "number" | QName }
order = { "ascending" | "descending" }
case-order = { "upper-first" | "lower-first" } >
</xsl:sort>
XSLT <xsl:if> Element
• The XSLT <xsl:if> element is used to specify a conditional test against the content of the XML
file.
<xsl:if test="expression">
...some output if the expression is true...
</xsl:if>
XSLT Elements
XSLT <xsl:choose> Element
• The XSLT <xsl:choose> element is used to specify
a multiple conditional test against the content of
nodes with the <xsl:otherwise> and <xsl:when>
elements.
Example:
<xsl:choose>
<xsl:choose>
<xsl:when test="expression"> <xsl:when test = "salary > 50000">
... some output ... High
</xsl:when> </xsl:when>
<xsl:otherwise>
... some output .... <xsl:when test = "salary > 40000">
Medium
</xsl:otherwise>
</xsl:when>
</xsl:choose>
<xsl:otherwise>
Low
</xsl:otherwise>
</xsl:choose>
XSLT Elements