Exemel
Exemel
Contents
Why XML? Introduction HTML & XML Types of XML Documents Namespaces
DTD Schema
Why XML?
It is a standard, easily extensible way to encode information To resolve issues in large scale electronic information publishing To create richly structured arbitrary document Due to its easy parseability, software can transform it in countless ways
Introduction
XML stands for eXtensible Markup Language A technology from W3C A method of creating document with User defined tags In XML, data speaks what it is? Not how it looks XML can serve web with HTML and WML transformation
Comparison
<b>Vishwanathan</b> <i>Nalasupara</i>
Umm
There is some bold and italic text But, we could still guess what data it is representing Bold text
Italic text
<name>Vishwanathan</name> <location>Nalasupara</location>
Comparison
HTML
Data Presentation Un-Structured Non-Case-Sensitive Vocabulary of build-in tags No error checks Current Version - 4.x Extension - .htm, .html
Comparison
XML
Data Definition 100% Structured 100% Case-Sensitive User defined tags Checks for errors Version since beginning - 1.0 Extension - .xml
10
Well-Formed
An XML document follows all the structuredness rules of the XML language A well-formed XML document is Valid if it conforms to its Vocabulary
Valid
Every valid XML document is a well-formed XML document but, every well-formed document is not a valid XML document
12
Well-Formedness Rules
XML Declaration (Mandatory) All tags are grouped within a single tag
13
Continued
Should follow rules given by W3C There can be only one root element Root element should include all other elements Every element should have a closing tag Empty elements should be self-closing
<empty/>
Continued
Attribute values should be enclosed in double quotes Naming rules should be followed for elements and attributes
Should start with A-Z or a-z or _ Can contain A-Z or a-z or 0-9 or _ or - or . (dot) Any external data embedded in XML should also be well-formed
15
Is it a Well-Formed Document?
<?xml version=1.0 ?> <Person gender=Male> <Name>Zubair Shaikh<Name> <Age>29</Age> <City>Mumbai<City/> <Indian> </person>
16
The attribute value for gender is male/female The name element is the first child of person All the elements are appeared and that too in the desired sequence
17
The element that can or must appear How often the element can appear How the element can be nested The allowable, required and default attributes
18
Defining Vocabulary
19
Namespaces
Why Namespaces?
Naming standards to the element/attribute are too brittle Element and Attribute names can be ambiguous There is no way to know if a name is already in use
21
Introduction
It is introduced to makes naming unique and free of ambiguity It refers to two-part name as Qualified Names or QNames It deals with the mechanics of associating a URI qualifier with element and attribute names
Qualified Names
QNames are used in place of element names QName have a prefix and a local part They are like <prefix:localPart>
<person:name>
It can be interpreted as
{http://www.example.com/person}name
Declaring Namespaces
<prefix:elementName xmlns:prefix=URI>
The following example declare the namespace person for URI http://www.example.com/persons
24
An Example
<?xml version=1.0 ?> <persons:person xmlns:persons= http://www.example.com/persons> <persons:name>Zubair<persons:name> <persons:age>29</persons:age> <persons:city>Mumbai</persons:city> </persons:person>
25
26
DTD
Why DTD?
A DTD adds syntactical requirements in addition to the well-formed requirement It helps in eliminating errors when creating or editing XML documents It clarifies the intended semantics It simplifies the processing of XML documents
28
Introduction
Document Type Definitions (DTDs) impose structure on XML documents The DTD is a syntactic specification The DTD follows regular expressions
29
Requirements
<?xml version=1.0 ?> <person> <name>Zubair Shaikh</name> <age>29</age> <city>Mumbai</city> <phone>022-22872183</phone> <phone>+91-9829748328</phone> <email>[email protected]</email> <indian/> </person>
30
Requirements
person is an element name, age and city appears in a sequence There can be one or more occurrences of phone email is the optional element Person can be either Indian or NRI
31
indain|nri
e?
e+ e1|e2 (e)
An element Sequence Zero or more occurrences Zero or One occurrences One or more occurrences Either Grouping
33
An Example
<!ELEMENT person (name,age,city,phone*,email?, (indian|nri))> <!ELEMENT name (#PCDATA)> <!ELEMENT age (#PCDATA)> <!ELEMENT city (#PCDATA)> <!ELEMENT phone (#PCDATA)> <!ELEMENT email (#PCDATA)> <!ELEMENT indian EMPTY> <!ELEMENT nri EMPTY>
34
Schema
Is it an XML document Need for Data types Validating element values Specifying upper limit of occurrences Defining custom types for similar elements
36
Introduction
XML Schema is an XML based alternative to DTD An XML vocabulary for expressing your data's business rules Object-oriented'ish With the support for data types Better support for mixed content model
37
XML Path
XPath
Introduction
XPath is a W3C recommendation (1999) According to W3C
XPath models XML documents as trees of nodes using the Document Object Model XPath uses path expressions to locate nodes with XML documents
39
Introduction
XPath is an efective and powerful way of query XML documents XPath is a language for specifying parts of an XML document XPath also inludes a library of standard functions for node-sets, strings, boolean and numbers XPath uses path expressions to identify XML document parts
40
An Example
<?xml version=1.0 ?> <library> <book isbn=37-4239> <title>Java</title> <author>Frank</author> <price>450</price> </book> <book isbn=43-4749> <title>XML</title> <author>Mark</author> <price>390</price> </book> </library>
41
Locating Nodes
Path = /library/book/price
<?xml version=1.0 ?> <library> <book isbn=37-4239> <title>Java</title> <author>Frank</author> <price>450</price> </book> <book isbn=43-4749> <title>XML</title> <author>Mark</author> <price>390</price> </book> </library>
42
Wild Cards
Path = /library/book/*
<?xml version=1.0 ?> <library> <book isbn=37-4239> <title>Java</title> <author>Frank</author> <price>450</price> </book> <book isbn=43-4749> <title>XML</title> <author>Mark</author> <price>390</price> </book> </library>
43
Selecting Branches
Path = /library/book[1]
<?xml version=1.0 ?> <library> <book isbn=37-4239> <title>Java</title> <author>Frank</author> <price>450</price> </book> <book isbn=43-4749> <title>XML</title> <author>Mark</author> <price>390</price> </book> </library>
44
Selecting Branches
Path = /library/book[last()]
<?xml version=1.0 ?> <library> <book isbn=37-4239> <title>Java</title> <author>Frank</author> <price>450</price> </book> <book isbn=43-4749> <title>XML</title> <author>Mark</author> <price>390</price> </book> </library>
45
Selecting Attributes
Path = //book[@isbn=43-4749]
<?xml version=1.0 ?> <library> <book isbn=37-4239> <title>Java</title> <author>Frank</author> <price>450</price> </book> <book isbn=43-4749> <title>XML</title> <author>Mark</author> <price>390</price> </book> </library>
47
Location Paths
One of the most important xpath expressions is the location path. A location path expression results in a node set Each location path consist of one or more location steps
48
49
XPath Axis
There are several axes in XPath Each of which selects a different subset of the nodes in the document That is depending on the context node
50
51
52
53
Root nodes Element nodes Text nodes Attribute nodes Namespace nodes Processing instruction nodes Comment nodes
54
child::foo - foo child element parent::text() - text node of parent descendent::comment() - comment node below
55
56
Predicate Expressions
Expression consist of
Example
Predicate Expressions
Expressions describes a set of nodes in a documents Path expression may be absolute or relative
/library/book/title book/author
58
Expressions
Numerical
div
mod
Equality
=
<
!=
> <= >=
Relational
Boolean
and or
59
Functions
XPath defines a number of useful functions for converting and translating data
String functions
concat() - returns the concatenations of all its arguments contains() - returns true if the second of 2 given strings is in the first
60
Functions
Number functions
sum() - returns the total value of a set of numeric values in a node set
not() - returns true if the argument is false, and false if the argument is true
Boolean functions
61
Presenting XML
Presenting XML
XML data can be presented The available options for the same
63
CSS is used to view XML as if it were HTML A style must be defined for each of the XML element Or the browser displays it in its default format
64
66
An Example
<?xml version=1.0 ?> <?xml-stylesheet type=text/css href=greeting.css ?> <greeting> <person>Rosy Dsouza</person> <message>Happy Diwali</message> </greeting>
67
Style Sheet
greeting{ display:block; background-color:pink; } person{ font:20pt verdana; color:blue; } message{ font:24pt georgia; color:red; font-style:itlaic; }
68
Transformation
Introduction
XML Style Sheet Language - Transformation A markup language and programming syntax for processing XML Most often used to
Transform XML to HTML to deliver to standard web clients Transform XML from one set of tag to another Transform XML into another syntax/system
70
Introduction
An XML document is a collection of nodes that can be identified, selected and acted upon using an XPath expression XSLT is based on the process of matching templates to nodes of the XML tree An XSLT style sheet is a collection of templates that act against specified nodes in the XML source tree
71
An Example
<?xml version=1.0 ?> <greeting> <person>Sam Mendes</person> <message>Eid Mubarak</message> </greeting>
72
Tree Nodes
Root
73
Templates
A separate template is defined for each element Syntax for defining template
74
Calling Templates
A template can call other templates Tree Processing
<xsl:apply-templates/>
<xsl:apply-templates select=title/>
<xsl:call-template name=title/>
75
Eg: <xsl:apply-templates/>
76
77
Decision Structure
78
<xsl:for-each select=expression>
For sorting
<xsl:sort />
79
XQuery
Why XQuery?
One of the great strengths of XML is its flexibility in representing many different kinds of information from diverse sources As increasing amounts of information are stored, exchanged, & presented using XML The ability to intelligently query XML data sources becomes increasingly important To exploit this flexibility, an XML query language must provide features for retrieving and interpreting information
81
XPath
Simple Navigation through the tree Recursive Traversal The SQL of XML
XSLT
XQuery
82
What is XQuery
83
Introduction
XQuery is a query language for XML data XQuery follows FLWR (flower)
84
FLWOR
WHERE
RETURN
85
An Example
<?xml version=1.0 ?> <library> <book isbn=37-4239> <title>Java</title> <author>Frank</author> <price>450</price> </book> <book isbn=43-4749> <title>XML</title> <author>Mark</author> <price>390</price> </book> </library>
86
FOR
LET
88
Query Example
Query Example
Thank You
Zubair Shaikh
[email protected]