0% found this document useful (0 votes)
8 views49 pages

XML New

Uploaded by

vosoc35287
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)
8 views49 pages

XML New

Uploaded by

vosoc35287
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
You are on page 1/ 49

Lecture Structure

05
Outline & Objectives
Minutes

05
Recap of Previous Lecture
Minutes

30 Delivery of Actual Content as per


Minutes Lesson Plan

05
Industry Relevance
Minutes

10
Minutes Interactive Activities

02
Importance of Discipline
Minutes

03
Recording of Attendance
Minutes

Reference: Office Order No. Provost/092023135, Date: 18 th September 2023

Mukti Patel, Dept. of Computer Sc. &


26/04/2025 1
Engineering
Outline
1. Introduction
• Introduction to XML
• Features of XML
• XML Key Component
2. Document Type Definition (DTD)
3. XML Schemas
4. XSL
5. XSLT

Mukti Patel, Dept. of Computer Sc. &


26/04/2025 2
Engineering
HTML vs XML

 HTML and XML were designed with below goals:

 HTML was designed to display data - focus on how data


looks
 XML was designed to carry data - focus on what data is

 HTML works with predefined elements like <h2>, <div>,


<b>,<body>,<p> etc.

 XML tags are not predefined like HTML tags are

 With XML, the author must define both the tags and the
document structure.
Mukti Patel, Dept. of Computer Sc. &
26/04/2025 3
Engineering
Features of XML
 It is in a format that both human and machines can read.
 It supports Unicode.
 It supports data structures.
 It is self-documenting.
 It has a strict format that makes it easy for parsing to take place.
 It can be understood and exchanged between dissimilar systems.
 It can be useful for swapping data between different applications.

Mukti Patel, Dept. of Computer Sc. &


26/04/2025 4
Engineering
Introduction to XML
 XML stands for eXtensible Markup Language
 XML is a language to describe other languages.
 Its main purpose is to allow the sharing of data across different type of
systems and it is particularly useful in this sense for applications that do this
over the internet.
 Example : Here person is root element
<?xml version=“1.0”>
<person> Here started is an attribute of
<first>Narendra</first> element employed
<last>Modi</last>
<birthdate>01/01/45</birthdate>
<employed started=“01/02/03”>
Professor
</employed>
</person>
Mukti Patel, Dept. of Computer Sc. &
26/04/2025 5
Engineering
Tree Structure of XML

An XML tree starts at a root (parent) element and branches from the root
to child elements.

All elements can have child elements and child elements can have sub
child elements:
<root>
<child>
<subchild>.....</subchild>
</child>
</root>

The terms parent, child, and sibling are used to describe the relationships
between elements.
Parents have children. Children have parents. Siblings are children on the
same level (brothers and sisters).

Mukti Patel, Dept. of Computer Sc. &


26/04/2025 6
Engineering
Tree Structure of XML(Contd..)

Example:<?xml version="1.0" <student>


encoding="UTF-8"?> <enrollment>150303108002</enro
<students> llment>
<student> <name>Hiren</name>
<enrollment>160303108001 <batch>2015</batch>
</enrollment> <age>25</age>
<name>Kuldeep</name>
</student>
<batch>2016</batch>
<age>24</age> </students>
</student>

Mukti Patel, Dept. of Computer Sc. &


26/04/2025 7
Engineering
Syntax Rules of XML
 XML Documents Must Have one and only one root element.
 Each & Every element must have both tag(start and an end tag)
 XML Tags are Case Sensitive.
 XML Elements Must be Properly Nested
 The XML Prolog
 XML Attribute Values Must Always be Quoted
 We use Entity References.
 We can use Comments in XML.
 All the White-space is Preserved in XML
 Names (used for tags and attributes) must begin with a letter or
underscore, and can consist of letters & digits.

Mukti Patel, Dept. of Computer Sc. &


26/04/2025 8
Engineering
XML Key Component
 One of the key aspects of XML is how strict the
syntax is.
 There are mainly 3 components of the XML
1. Elements
2. Attribute
3. Namespace

Mukti Patel, Dept. of Computer Sc. &


26/04/2025 9
Engineering
1) Elements
 The strict syntax of XML contains a few rules about elements that must
be adhered to:
• Elements must have a closing tag.
• Tags are case sensitive
• Elements must be nested correctly
• XML documents must have a root element.
 Example :
<birthdate>26th October 1788</birthdate> ü
<Birthdate>26th October 1788</birthdate> û (elements are case sensitive)

<b><i>Hello<b></i> û (elements not nested properly)

<b><i>Hello</i></b> ü
Mukti Patel, Dept. of Computer Sc. &
26/04/2025 10
Engineering
2) Attributes
 Attributes can be added to elements in XML but must always be
quoted.
 For Example, here employed is a element and started is the
attribute of the element employed.
<employed started=“10/11/12”>GSFC</employed> ü
<employed started=10/11/12>GSFC</employed> û
Value must be quoted

Mukti Patel, Dept. of Computer Sc. &


26/04/2025 11
Engineering
3) Namespace
 Sometimes in XML there is a danger of conflicting names
between documents.
 Example :
• You create one document with name element for the
professor, it may also possible someone else create a
document with name element for the animal name, so to
avoid the conflict we can use namespaces.
 Namespace usually take the form of a URL, beginning with a
domain name, an optional namespace label in the form of a
directory name and finally a version number, which is also
optional.
xmlns = “http://www.mydomain.com/ns/animals/1.1”

Mukti Patel, Dept. of Computer Sc. &


26/04/2025 12
Engineering
XML Key Components

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


Name Conflicts:
 In XML, element names are defined by the user/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>

Mukti Patel, Dept. of Computer Sc. &


26/04/2025 13
Engineering
XML Key Components
 This XML carries information about a table (a piece of furniture):
<table>
<name>Tea Table</name>
<width>70</width>
<length>100</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.

 A user or an XML application will not know how to handle these differences.

Mukti Patel, Dept. of Computer Sc. &


26/04/2025 14
Engineering
XML Key Components
xmlns Attribute:
 A namespace for the prefix must be defined, When we use prefixes in XML.
 It can be defined by an xmlns attribute in the start tag of an element.
syntax. xmlns:prefix="URI".
<root><h:table xmlns:h=“Class1">
<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>Tea Table</f:name>
<f:width>70</f:width>
<f:length>100</f:length></f:table> </root>

Mukti Patel, Dept. of Computer Sc. &


26/04/2025 15
Engineering
XML Key Components

 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>Tea Table</f:name>
<f:width>70</f:width>
<f:length>100</f:length>
</f:table>
</root>

Mukti Patel, Dept. of Computer Sc. &


26/04/2025 16
Engineering
XML Key Component (Cont.)
 White space is preserved in XML where as in HTML it is
truncated down to just a single space.
 One thing does remain in common with HTML though is
comments,
• Comments can be added using the triangle brackets like
this:
<!-- here are some remarks -->

Mukti Patel, Dept. of Computer Sc. &


26/04/2025 17
Engineering
XML -Validation
 Validation is a process by which an XML document is validated.

 An XML document is called valid if its contents match with the


elements, attributes and associated DTD, and if the document
complies with the constraints expressed in it.

 Validation is dealt in two ways by the XML parser.

They are −
1.Well-formed XML document
2.Valid XML document

Mukti Patel, Dept. of Computer Sc. &


26/04/2025 18
Engineering
XML -Validation
1.Well-formed XML Document:
 It follow the following rules then it is well-formed:

 It must follow the ordering of the tag.

 Each of its opening tags must have a closing tag or it must be a self ending
tag.

 It must have only one attribute in a start tag, which needs to be quoted.

 Non DTD XML files must use the predefined character entities.

Mukti Patel, Dept. of Computer Sc. &


26/04/2025 19
Engineering
XML -Validation

2.Valid XML document

 If an XML document is well-formed and has an


associated DTD- Document Type Declaration , then
it is said to be a valid XML document

Mukti Patel, Dept. of Computer Sc. &


26/04/2025 20
Engineering
XML -DTD
 A DTD is a Document Type Definition. It is defines the structure and the
legal elements and attributes of an XML document.

 DTDs check vocabulary and validity of the structure of XML documents


against grammatical rules of appropriate XML language.

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

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


"Valid".

Mukti Patel, Dept. of Computer Sc. &


26/04/2025 21
Engineering
Document Type Definition (DTD)
 XML is particularly concerned with being well formed
or correct in syntax.
 There are two ways of checking whether the
document follows expected order and structure
• Document Type Definitions (DTDs)
• Schemas
 A Document Type Definition (DTD) defines the legal
building blocks of an XML document.
 A DTD can be declared inline inside an XML
document, or as an external reference

Mukti Patel, Dept. of Computer Sc. &


26/04/2025 22
Engineering
Why Use a DTD?
 With a DTD, each of your XML files can carry a description of
its own format.
 With a DTD, independent groups of people can agree to use a
standard DTD for interchanging data.
 Your application can use a standard DTD to verify that the data
you receive from the outside world is valid.

Mukti Patel, Dept. of Computer Sc. &


26/04/2025 23
Engineering
DTD (Example)
<?xml version="1.0"?>
<!DOCTYPE note [
<!ELEMENT note (to,from,title,message)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT message (#PCDATA)>
]>

Mukti Patel, Dept. of Computer Sc. &


26/04/2025 24
Engineering
DTD (Cont.)
 DTD can be internal or external
 If it is internal than simply put previous code to the
top of the XML file
 If it is external than save it as .dtd file extension and
refer it from XML,
<!DOCTYPE note SYSTEM “note.dtd”>

Mukti Patel, Dept. of Computer Sc. &


26/04/2025 25
Engineering
DTD Elements
 We can specify the number of occurrences of the elements using +,
*, ? and | operators (works ~ similar to Regular Expression)
 Example :
• <!ELEMENT note(to+,from,title?,message*) />
 Above example suggest that root element of the xml must be note
and should have one or more (+) recipients, sender should be only
one, title must be one or zero(?) and messages can be zero or
more(*).
 We can also specify to have either one of the elements using | operator
• <!ELEMENT note(to,from,title,message|information) >
 In above declaration we have specified that either message should be
there or the information element should be there in the note

Mukti Patel, Dept. of Computer Sc. &


26/04/2025 26
Engineering
DTD Attribute
We can specify the attributes also using DTD using ATTLIST
declaration.
 Syntax:
<!ATTLIST element-name attribute-name attribute-type default-value >
This is the default value for started
 Example :
<!ATTLIST employed started CDATA “01/01/01”>

We can also specify required or fixed for


Thisthe attribute
suggest that started is mandatory
field
 Example :
<!ATTLIST employed started CDATA #REQUIRED>
<!ATTLIST employed started CDATA #FIXED “01/01/01”>
Mukti Patel, Dept. of Computer Sc. &
26/04/2025 27
Engineering
Entity- Reference
 They are use to avoid the ambiguity while using some symbols.
For example,
less than ( < ) or greater than ( > ) symbol is used with the angle
tag (<>).
 Following is a list of pre-defined character entities from XML
specification.
Ampersand − &amp;
Single quote − &apos;
Greater than − &gt;
Less than − &lt;
Double quote − &quot;
Mukti Patel, Dept. of Computer Sc. &
26/04/2025 28
Engineering
XML Schema
 XML Schema is an XML-based alternative to DTD
 An XML schema describes the structure of an XML
document.
 The XML Schema language is also referred to as XML
Schema Definition (XSD)
 An XML Schema
• defines elements that can appear in a document
• defines attributes that can appear in a document
• defines which elements are child elements
• defines the order of child elements
• defines data types for elements and attributes
• defines default and fixed values for elements and attributes
Mukti Patel, Dept. of Computer Sc. &
26/04/2025 29
Engineering
Data Types in XSD
 xs:string
 xs:decimal
 xs:integer
 xs:boolean
 xs:date
 xs:time
 Etc…..

Mukti Patel, Dept. of Computer Sc. &


26/04/2025 30
Engineering
XML Schema (cont.)
 XML Schemas are the Successors of DTDs
• XML Schemas are extensible to future additions
• XML Schemas are richer and more powerful than DTDs
• XML Schemas are written in XML
• XML Schemas support data types
• XML Schemas support namespaces

Mukti Patel, Dept. of Computer Sc. &


26/04/2025 31
Engineering
XML Schema (Example)
note.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs=http://www.w3.org/2001/XMLSchema >
<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>
</xs:schema>
Mukti Patel, Dept. of Computer Sc. &
26/04/2025 32
Engineering
XML Schema (Example) (cont)
<?xml version="1.0"?>
<note
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation=“note.xsd“>
<to>Mukti</to>
<from>Student</from>
<heading>Reminder</heading>
<body>Don't forget to attend lecture this weekend!
</body>
</note>
Mukti Patel, Dept. of Computer Sc. &
26/04/2025 33
Engineering
Complex Types in XSD
 Complex elements can be built that contain other
elements and attributes.
 For example,
<xs:complexType name=“productinfo”>
<xs:sequence>
<xs:element name=“item” type=“xs:string” />
<xs:element name=“itemcode” type=“xs:string” />
</xs:sequence>
</xs:complexType>
<xs:element name=“food” type=“productinfo”/>
<xs:element name=“magazine” type=“productinfo”/>
<xs:element name=“clothes” type=“productinfo”/>
Mukti Patel, Dept. of Computer Sc. &
26/04/2025 34
Engineering
Default ,Fixed and Required Values
 To define attribute a similar style is adopted, for
example for the XML element :
<firstname lang=“English”>Mukti</firstname>
 XSD would be
<xs:attribute name="lang" type="xs:string"/>
• Default value attribute in XSD
<xs:attribute name="lang" type="xs:string" default="EN"/>
• Fixed value attributes in XSD
<xs:attribute name="lang" type="xs:string" fixed="EN"/>
• Required value attributes in XSD
<xs:attribute name="lang" type="xs:string" use="required"/>
Mukti Patel, Dept. of Computer Sc. &
26/04/2025 35
Engineering
What is XSL
 XSL stands for eXtensible Stylesheet Language.
• CSS = Style Sheets for HTML
• XSL = Style Sheets for XML
 XSL describes how the XML document should be displayed!
 XSL - More Than a Style Sheet Language
 XSL consists of three parts:
• XSLT - a language for transforming XML documents
• XPath - a language for navigating in XML documents
• XSL-FO - a language for formatting XML documents

Mukti Patel, Dept. of Computer Sc. &


26/04/2025 36
Engineering
What is XSLT?
 XSLT stands for XSL Transformations
 XSLT is the most important part of XSL
 XSLT transforms an XML document into another XML document
(ex. (X)HTML)
 XSLT uses XPath to navigate in XML documents

Mukti Patel, Dept. of Computer Sc. &


26/04/2025 37
Engineering
XSL Transformation
The style sheet provides the template that transforms the
document from one structure to another

 <xsl:template> starts the definition of the actual template, as


the root of the source XML document

 The match = “/” attribute makes sure this begins applying the
template to the root of the source XML document

 The style sheet is linked into the XML by adding the connecting
statement to the XML document:
<?xml‐stylesheet type=”text/xsl” href=”abc.xsl” ?>

Mukti Patel, Dept. of Computer Sc. &


26/04/2025 38
Engineering
XSLT Example book.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type = "text/xsl" href = "book.xsl"?>
<library>
<book>
<title>Developing Web Application</title>
<author>Ralph Moseley</author>
<price>109</price>
</book>
<book>
<title>Software engineering</title>
<author>Roger Pressmen</author>
<price>120</price>
</book>
<book>
<title>Java head first</title>
<author>Bob Dylan</author>
<price>400</price>
</book>
</library> Mukti Patel, Dept. of Computer Sc. &
26/04/2025 39
Engineering
XSLT Example (Cont)
book.xsl
<?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>Library Book Collection</h2>
<table border="1">
<xsl:for-each select="library/book">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="author"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Mukti Patel, Dept. of Computer Sc. &
26/04/2025 40
Engineering
XSL Elements
 stylesheet
 template
 value‐of
 for‐each
 sort
 If
 choose
 when
 otherwise

Mukti Patel, Dept. of Computer Sc. &


26/04/2025 41
Engineering
XSL Elements (cont.)
 <xsl:stylesheet>
• <xsl:stylesheet version="1.0”
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
• <xsl:stylesheet>, defines that this document is an XSLT style sheet
document (along with the version number and XSLT namespace attributes).
 <xsl:template match="/">
• The <xsl:template> element is used to build templates.
• Attribute match is used to set the starting point for the XPath
• Match=“/” specifies that XPath is calculated from the root of the document
 <xsl:value-of>
• The <xsl:value-of> element can be used to extract the value of an XML
element
• <xsl:value-of select=“XPath"/>
• It will provide output to the stream of transformation
• XPath index will start from 1 (not 0)
Mukti Patel, Dept. of Computer Sc. &
26/04/2025 42
Engineering
XSL Elements (cont.)
 <xsl:for-each>
• The XSL <xsl:for-each> element can be used to select every
XML element of a specified node-set
<xsl:for-each select=“XPath">
</xsl:for-each>
• We can also filter the output from the XML file by
<xsl:for-each select=“GSFC university/chat[to=‘ABC']">
• Legal filter operators are:
• = (equal)
• ! = (not equal)
• &lt; less than
• &gt; greater than
Mukti Patel, Dept. of Computer Sc. &
26/04/2025 43
Engineering
XSL Elements (cont.)
 <xsl:sort>
• The <xsl:sort> element is used to sort the output.
• <xsl:sort> is always within <xsl:for-each>
<xsl:for-each select=“GSFC university/chat">
<xsl:sort select=“message“ order=“ascending|
descending”/>
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>

Mukti Patel, Dept. of Computer Sc. &


26/04/2025 44
Engineering
XSL Elements (cont.)
 <xsl:if>
• To put a conditional if test against the content of the XML
file, add an <xsl:if> element to the XSL document.
<xsl:if test="expression">
...some output if the expression is true...
</xsl:if>
• <xsl:if test="price &gt; 10">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="price"/></td>
</tr>
</xsl:if>
Mukti Patel, Dept. of Computer Sc. &
26/04/2025 45
Engineering
XSL Elements (cont.)
 <xsl:choose>
• The <xsl:choose> element is used in conjunction with
<xsl:when> and <xsl:otherwise> to express multiple
conditional tests
<xsl:choose>
<xsl:when test="expression">
... some output ...
</xsl:when>
<xsl:otherwise>
... some output ....
</xsl:otherwise>
</xsl:choose>

Mukti Patel, Dept. of Computer Sc. &


26/04/2025 46
Engineering
Interactive Section

Develop your own question and ask that to other student of


your choice

Mukti Patel, Dept. of Computer Sc. &


26/04/2025 47
Engineering
 In every lecture, Faculty may talk on any of the following:
• DCS & GSFCU Portal: Remind students to use DCS, talk about it’s features and instruct all to visit GSFCU

website

• Discipline, Values and Ethics.

b
• GSFC University – My University: Emphasize the importance of taking ownership of the University.

• Respect & Courtesy: Remind students to be respectful to peers, teachers, and staff. Encourage polite language

and behavior.

• Punctuality: Emphasize the importance of arriving on time to class and completing assignments by deadlines.

• Classroom Rules: Reinforce basic classroom rules, such as keeping mobile phones on silent and maintaining a

clean environment.

• Safety: Remind students about safety protocols, whether it's physical safety in the classroom or digital safety

when using online resources.

• Positive Attitude: Encourage a positive mindset, cooperation and supporting one another.
Mukti Patel, Dept. of Computer Sc. &
26/04/2025 48
Engineering
GSFCU & Values

Respect & Courtesy

Mukti Patel, Dept. of Computer Sc. &


26/04/2025 49
Engineering

You might also like