You are on page 1of 95

Web Technology

BCA III Semester


Unit-3
Detail syllabus
Unit 3 The Client Tier
Representing Content; Introduction to XML; Elements and
Attributes; Rules for Writing XML; Namespaces; Schema:
Simple Types and Complex Types, XSD Attributes, Default and
Fixed Values, Facets, Use of Patterns, Order Indicators(All,
Choice, Sequences), Occurrence Indicators ( Maxoccurs,
Minoccurs), DTD: Internal Declaration, Private External
Declaration, Public External Declaration, Defining Elements and
Attributes; XSL/XSLT; Xpath; Xquery; SAX; DOM , Creating
XML Parser.
Introduction to XML

• XML stands for eXtensible Markup Language


• XML is a markup language much like HTML
• XML was designed to be self-descriptive
• XML is a W3C Recommendation
• XML has no predefined tags
Introduction to XML
<friendsList>
<friends>
<name>Janak</name>
I have many friends, </friends>
<friends>
Janak and Sabin are my friends <name>Sabin</name>
Salina is my best friend </friends>
<bestFriends>
<name>Salina</name>
</bestFriends>
</friendsList>
XML usages
• XML can work behind the scene to simplify the creation of HTML documents for
large web sites.
• XML can be used to exchange the information between organizations and
systems.
• XML can be used for offloading and reloading of databases.
• XML can be used to store and arrange the data, which can customize your data
handling needs.
• XML can easily be merged with style sheets to create almost any desired output.
• Virtually, any type of data can be expressed as an XML document.
• XML use for design in android
XML vs. HTML

-XML was designed to carry data - with focus on what data is


-HTML was designed to display data - with focus on how data looks
-XML tags are not predefined like HTML tags are
XML vs. HTML
HTML XML
HTML is used to display data and focuses on how XML is a software and hardware independent
data looks. tool used to transport and store data. It focuses
on what data is.
HTML is a markup language itself. XML provides a framework to define markup
languages.
HTML is not case sensitive. XML is case sensitive.
HTML is a presentation language. XML is neither a presentation language nor a
programming language.
HTML has its own predefined tags. You can define tags according to your need.
In HTML, it is not necessary to use a closing tag. XML makes it mandatory to use a closing tag.
HTML is static because it is used to display data. XML is dynamic because it is used to transport
data.
HTML does not preserve whitespaces. XML preserve whitespaces.
XML Features
1) XML separates data from HTML
With 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.
3) XML simplifies data transport
Exchanging data as XML greatly reduces this complexity, since the data can be read
by different incompatible applications.
XML Features
4) XML simplifies Platform change
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.

5) XML increases data availability


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
WAP and WML as markup languages for handheld devices
SMIL for describing multimedia for the web
Rules for writing XML
1. XML Documents Must Have a Root Element This element is "the parent"
of all other elements.

Example:
<friendsList>
<root> <friends>
<child> <name>Janak</name>
<subchild>.....</subchild> </friends>
</child> <bestFriends>
<name>Salina</name>
</root> </bestFriends>
</friendsList>

-FriendsList is root element


Rules for writing XML
2. The XML Prolog
The XML prolog is optional. If it exists, it must come first in the document.

<?xml version="1.0" encoding="UTF-8"?>


<friendlist>
<to>Ram</to>
<from>Shita</from>
<body>Happy weekend</body>
</note>
Rules for writing XML
3. All XML Elements Must Have a Closing Tag
<p>This is a paragraph.</p>

4. XML Tags are Case Sensitive


XML tags are case sensitive. The tag <Friend> is different from the tag <friend>.

5. XML Elements Must be Properly Nested


Html: <b><i>This text is bold and italic</b></i>
XML <b><i>This text is bold and italic</i></b>
"Properly nested" simply means that since the <i> element is opened inside the <b> element,
it must be closed inside the <b> element.
Rules for writing XML
6. XML Attribute Values Must Always be Quoted
<note date="12/11/2023">
<to>Ram</to>
<from>Sita</from>
</note>

7. 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.
Example: <student>marks < 100</student>

To avoid this error, replace the "<" character with an entity reference:
<student>marks &lt; 100</student>
XML Entity Reference

&lt; < less than

&gt; > greater than

&amp; & ampersand

&apos; ' apostrophe

&quot; " quotation mark


Rules for writing XML
8. Comments in XML
The syntax for writing comments in XML is similar to that of HTML:

Example: <!-- This is a comment -->

9. White-space is Preserved in XML

XML: Hello Ram


HTML: Hello Ram
XML Tree Structure
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
XML Example
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Ram</to>
<from>Shia</from>
<heading>Reminder</heading>
<body>Happy weekend!</body>
</note>

-The first line is the XML declaration. It defines the XML version (1.0) and the encoding
used (ISO-8859-1 = Latin-1/West European character set).
-The next line describes the root element of the document (like saying: "this document is a
note"):
-The next 4 lines describe 4 child elements of the root (to, from, heading, and body).
-And finally the last line defines the end of the root element.
XML Elements
An XML element is everything from (including) the element's start tag to (including) the
element's end tag.
An element can contain:
• text
• attributes
• other elements
• or a mix of the above
XML Elements
Example:
<friendsList>
<friends>
<name>Janak</name>
</friends>
<bestFriends>
<name>Salina</name>
</bestFriends>
<friendsList>

<name> has text content


<friendlist>, <bestFriends> has element content.
XML Attribute
XML Attributes Must be Quoted

Example:
<friendsList>
<friends gender=“male”>
<name>Janak</name>
</friends>
<bestFriends>
<name>Salina</name>
</bestFriends>
<friendsList>

Gender is attribute
XML Elements Vs Attribute
<person gender="female">
<firstname>Salina</firstname>
<lastname>Nagarkoti</lastname>
</person>

<person>
<gender>female</gender>
<firstname>Salina</firstname>
<lastname>Nagarkoti</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.
XML Attribute: Things to consider
Some things to consider when using attributes are:
-attributes cannot contain multiple values (elements can)
-attributes cannot contain tree structures (elements can)
-attributes are not easily expandable (for future changes)
XML Namespace
XML Namespaces provide a method to avoid element name conflicts.
Example
<namelist>
<names> If these XML fragments were added together,
<name>Bishnu</name> there would be a name conflict. Both contain a
<name> element, but the elements have different
<name>Ramesh</name>
content and meaning.
</names> A user or an XML application will not know how to
handle these differences.
<names>
<name>Nikesh</name>
<name>Sabin</name>
</names>
<namelist>
XML Namespace

-we have the same table


element

-If we use them in separate


XML files it is ok. However, if
we use both elements in the
same XML, it occurs name
conflict.

To solve the name conflict issue we add a prefix


XML Namespace

-We add an f prefix for the


furniture table and an h prefix
for html table.

-To solve the name conflict issue we add a prefix


-Whenever using prefix in XML, namespace must be defined
XML Namespace
Solving the Name Conflict Using a Prefix
Example:
<namelist>
<s.names>
<s.name>Bishnu</s.name>
In the example, there will be no
<s.name>Ramesh</s.name> conflict because the <names>
</s.names> elements have different prefix.
<e.names> However, this gives error, because
<e.name>Nikesh</e.name> we haven’t added namespaces.
<e.name>Sabin</e.name>
</e.names>
<namelist>
Adding XML Namespace
-When using prefixes in XML, a namespace for the prefix must be defined.
-The namespace can be defined by an xmlns attribute in the start tag of an element.
-The namespace declaration has the following syntax. xmlns:prefix="URI".

Example:
<namelist> A Uniform Resource
<s.names xmlns:s="http://example.com/students"> Identifier (URI) is a string of
<s.name>Bishnu</s.name> characters which identifies an
<s.name>Ramesh</s.name> Internet Resource.
</s.names> The most common URI is
<e.names xmlns:e="http://example.com/employee"> the Uniform Resource
<e.name>Nikesh</e.name> Locator (URL) which identifies
<e.name>Sabin</e.name> an Internet domain address.
</e.names>
</namelist>
Adding XML Namespace
We can add namespaces at the root elements

Example:
<namelist xmlns:s=“http://example.com/students”
xmlns:e="http://example.com/employee">
<s.names>
<s.name>Bishnu</s.name>
<s.name>Ramesh</s.name>
</s.names>
<e.names>
<e.name>Nikesh</e.name>
<e.name>Sabin</e.name>
</e.names>
</namelist>
Default Namespace
If we are using default namespaces, we do not need to provide prefix. To do this, we
provide URL in tag level.

Example:
<namelist>
<names xmlns="http://example.com/students">
<name>Bishnu</name>
<name>Ramesh</name>
</names>
<names xmlns="http://example.com/employee">
<name>Nikesh</name>
<name>Sabin</name>
</names>
</namelist>
XML Validator
-Use our XML validator to syntax-check your XML.
-An XML document with correct syntax is called "Well Formed".

To check XML validation you can search validator in google.


Example:
https://www.w3schools.com/xml/xml_validator.asp
XML: Grammar
We have two grammar to specify the XML structure:
1. DTD (Document Type Definition): Structure Grammar
2. Schema: Content Grammar
XML: DTD
-DTD stands for Document Type Definition.
-A DTD defines the structure and the legal elements and attributes of an XML
document.
-It helps to validate XML

DTD are two types:


1. Internal
2. External
XML: DTD
Internal DTD:
Syngax:
<!DOCTYPE rootelement DTD identifier
[
first declaration
second declaration
.
.
nth declaration
]>
XML: DTD
When to Use a DTD?
-To interchanging data.
-To verify that the data you receive from the outside world is valid.
-Used a DTD to verify your own data.
XML: DTD
Important terms: DTD attribute default values and attribute types
(#PCDATA)- to represent parsable data
CDATA: to represent characters or symbols
ATTLIST: Attribute list
EMPTY: to represent the empty element
#IMPLIED: specifies that there is no default value for this attribute and
that attribute is optional.
#REQUIRED: you don’t provide default value
XML: DTD
Important terms: DTD attribute default values
(#PCDATA)- to represent parsable data
CDATA: to represent characters or symbols
ATTLIST: Attribute list
EMPTY: to represent the empty element
#IMPLIED: specifies that there is no default value for this attribute and
that attribute is optional.
#REQUIRED:
XML: DTD
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE names SYSTEM “names.dtd">
<names>
<name>Nikesh</name>
<age>20</age>
</names>

The DOCTYPE declaration above contains a reference to a DTD file. The content of the DTD file is shown below.
<!DOCTYPE names
[
The DTD above is interpreted like this:
<!ELEMENT names (name,age)>
!DOCTYPE names - Defines that the root element of the document is
<!ELEMENT name (#PCDATA)>
names
<!ELEMENT age (#PCDATA)>
!ELEMENT names - Defines that the names element must contain the
]>
elements: “name, age"
#PCDATA means parseable character data.
!ELEMENT name - Defines the name element to be of type "#PCDATA"
!ELEMENT age - Defines the age element to be of type "#PCDATA"
DTD: define attributes
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE names SYSTEM “names.dtd">
<names type=“student”>
<name>Nikesh</name>
<age>20</age>
</names>

The DOCTYPE declaration above contains a reference to a DTD file. The content of the DTD file is shown below.
<!DOCTYPE names
[
The DTD above is interpreted like this:
<!ELEMENT names (name,age)>
!DOCTYPE names - Defines that the root element of the document is
<!ELEMENT name (#PCDATA)>
names
<!ELEMENT age (#PCDATA)>
!ELEMENT names - Defines that the names element must contain the
<!ATTLIST type ID #REQUIRED>
elements: “name, age"
]>
!ELEMENT name - Defines the name element to be of type "#PCDATA"
!ELEMENT age - Defines the age element to be of type "#PCDATA“
<!ATTLIST type defines the type attribute is REQUIRED
DTD: Internal declaration

If the DTD is declared inside the XML file, it must be wrapped inside the <!DOCTYPE> definition:
<?xml version="1.0"?>
<!DOCTYPE names [
<!ELEMENT names (name,age)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
]>
<names>
<name>Nikesh</name>
<age>22</age>
</names>
DTD: External Declaration
If the DTD is declared in an external file, the <!DOCTYPE> definition must contain a reference to the
DTD file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE names SYSTEM “names.dtd">
<names>
<name>Nikesh</name>
<age>20</age>
</names>

The DOCTYPE declaration above contains a reference to a DTD file. The content of the DTD file is
shown below.
<!DOCTYPE names
[
<!ELEMENT names (name,age)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
]>
DTD: External Declaration
Example:
Data.xml
<!DOCTYPE data SYSTEM "data.dtd">
<data> -The <text> element's content model is defined as
<text>Hello, <em>World!</em></text> (#PCDATA | em)*. This means it can contain either
<script><![CDATA[alert("This is a CDATA section!");]]></script> parsed character data (#PCDATA) or the <em>
</data> element. The * indicates that it can have zero or
more occurrences of #PCDATA or <em>. This allows
us to include both plain text and <em> elements
Data.dtd within the <text> element.
<!ELEMENT data (text, script)>
<!ELEMENT text (#PCDATA | em)*> -The <script> element's content model is defined as
<!ELEMENT script (#CDATA)> (#CDATA). This indicates that the content of the
<script> element is treated as character data
(#CDATA). The #CDATA section allows us to include
raw text content, such as JavaScript code, without
any special interpretation by the XML parser.
DTD: Practice set

Create a valid XML document containing details of a car like: id, company name, model, engine, and
mileage using DTD.
XML Schema: XSD

-In order to work with content we need to create Schema.


-Using DTD we can only write structure grammar but using schema we can write
both structure and content.
-The XML Schema language is also referred to as XML Schema Definition (XSD).
XML Schema Element

The <schema> Element


The <schema> element is the root element of every XML Schema:

Syntax:
<xs:schema>
...
...
</xs:schema>
XML Schema Element

1. Simple element
2. Complex element
Simple Element:
A simple element is an XML element that contains only text. It cannot contain any other elements or
attributes.
Example:
<xs:element name="xxx" type="yyy"/>
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 Element

Complex element
A complex element is an XML element that contains other elements and/or attributes.

Example:
<employee>
<firstname>John</firstname>
<lastname>Smith</lastname>
</employee>
There are four kinds of complex elements:
1. empty elements
2. elements that contain only other elements
3. elements that contain only text
4. elements that contain both other elements and text
XML Schema Attribute

The syntax

<xs:attribute name="xxx" type="yyy"/>

Example:
<xs:attribute name="lang" type="xs:string"/>
XML Attributes

Default and fixed value for attribute

-Attributes may have a default value OR a fixed value specified.


-A default value is automatically assigned to the attribute when no other value is specified.
In the following example the default value is "EN":

<xs:attribute name="lang" type="xs:string" default="EN"/>

-A fixed value is also automatically assigned to the attribute, and you cannot specify
another value.
In the following example the fixed value is "EN":
<xs:attribute name="lang" type="xs:string" fixed="EN"/>
XML Attributes

Optional and Required Attributes


-Attributes are optional by default. To specify that the attribute is required, use
the "use" attribute:

<xs:attribute name="lang" type="xs:string" use="required"/>


XSD Indicators

-Indicators are used to control, HOW elements are to be used in documents

Types of indicators (three categories)


A. Order indicators:
1. All
2. Choice
3. Sequence
B. Occurrence indicators:
1. maxOccurs
2. minOccurs
C. Group indicators:
1. Group name
2. attributeGroup name
XSD Indicators
A. Order indicators: Order indicators are used to define the order of the elements.
1. Sequence
2. All
3. Choice
Sequence indicator:
The <sequence> indicator specifies that the child elements must appear in a specific order,
that is first name and last name should be in same order in both xml and xsd file.
Example: person.xsd
Person.xml
<xs:element name="person">
<person>
<xs:complexType> <name>first name </name>
<xs:sequence> <name>lastname</name>
<xs:element name="firstname" type="xs:string"/> </person>
<xs:element name="lastname" type="xs:string"/>
</xs: sequence>
</xs:complexType>
</xs: element>
XSD Indicators
All indicator:
The <all> indicator specifies that the child elements can appear in any order, that is first name
or last name can be in any order in xml and xsd file.

Example: person.xsd
<xs:element name="person">
<xs:complexType> Person.xml
<xs:all> <person>
<xs:element name="firstname" type="xs:string"/> <name>lastname</name>
<xs:element name="lastname" type="xs:string"/> <name>first name </name>
</xs:all>
</person>
</xs:complexType>
</xs:element>
XSD Indicators
Choice indicator:
The <choice> indicator specifies that either one child element or another can occur in xml file.
Both can’t exist together.

Example: person.xsd
<xs:element name="person">
<xs:complexType>
Person.xml
<xs:choice> <person>
<xs:element name="firstname" type="xs:string"/> <name>lastname</name>
<xs:element name="lastname" type="xs:string"/> </person>
</xs:all>
</xs:complexType>
</xs:element>
XSD Indicators
B. Occurrence indicators: Occurrence indicators are used to define how often an element can occur.
1. maxOccurs
2. minOccurs
Max occurs: The <maxOccurs> indicator specifies the maximum number of times an element can occur:

Example: person.xsd
Person.xml
<xs:element name="person"> <person>
<xs:complexType> <name>firstname</name>
<xs:choice> <name>lastname</name>
<xs:element name="firstname" type="xs:string"/> <name>lastname</name>
<xs:element name="lastname" type="xs:string“ maxoccurs=“2”/> </person>
</xs:all>
</xs:complexType>
Here last name can occur 2 times
</xs:element> because maxoccur in xsd is 2.
XSD Indicators
B. Occurrence indicators: Occurrence indicators are used to define how often an element can occur.
1. maxOccurs
2. minOccurs
Max occurs: The <minOccurs> indicator specifies the minimum number of times an element can occur:

Example: person.xsd
<xs:element name="person">
<xs:complexType> Person.xml
<xs:choice> <person>
<xs:element name="firstname" type="xs:string"/> <name>firstname</name>
<xs:element name="lastname" type="xs:string“ minoccurs=“1”/> <name>lastname</name>
</xs:all> <name>lastname</name>
</xs:complexType> </person>
</xs:element>

Here last name can occur at least 1


times because minoccur in xsd is 1.
Creating XML Schema
Generating XML Schema online
We can generate XML schema using online tools.

https://www.freeformatter.com/xsd-generator.html
Facets
Restrictions on XML elements are called facets.
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer"> <age>20</age>
<xs:minInclusive value="0"/> Here age value can’t be
<xs:maxInclusive value="120"/> lower than 0 and higher
</xs:restriction> than 120
</xs:simpleType>
</xs:element>
Patterns
The example below defines an element called “life" with a restriction. The only acceptable
value is ONE of the LOWERCASE letters from a to z:

<xs:element name=“life"> <name>life</name>


<xs:simpleType> At least one letter should be
<xs:restriction base="xs:string"> lower case
<xs:pattern value="[a-z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Patterns
An element called “helloworld" with a restriction. The only acceptable value is THREE of the
UPPERCASE letters from a to z:
<xs:element name=" helloworld">
<xs:simpleType>
<xs:restriction base="xs:string">
<name>HELloworld</name>
<xs:pattern value="[A-Z][A-Z][A-Z]"/> At least three letter should
</xs:restriction> be upper case
</xs:simpleType>
</xs:element>
Practice:
Create a valid XML document containing details of a car like: id, company name, model,
engine, and mileage using DTD.
XSL/XSLT
-XSL stands for EXtensible Stylesheet Language. It is a styling language for XML
just like CSS is a styling language for HTML.
-File extension is .xsl like .css in html.

-XSLT stands for XSL Transformation. It is used to transform XML documents into
other formats (like transforming XML into HTML).
XSL/XSLT
XSL/XSLT
Main parts of XSL Document
XSLT: It is a language for transforming XML documents into various other
types of documents.
XPath: It is a language for navigating in XML documents.
XQuery: It is a language for querying XML documents.
XSL-FO: It is a language for formatting XML documents.
XSL/XSLT
<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<?xml version="1.0"?>
<xsl:template match="/class">
<?xml-stylesheet type="text/xsl" href="xsldemo.xsl"?> <html>
<class> <body>
<table border="1">
<students>
<tr bgcolor="blue">
<name>Ramesh</name> <th>name</th>
<age>20</age> <th>age</th>
</tr>
</students>
<xsl:for-each select="students">
</class> <tr>
<td><xsl:value-of select = "name"/></td>
<td><xsl:value-of select = "age"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Xpath
Xpath

-XPath is a major element in the XSLT standard.


-XPath stands for XML Path Language
-XPath uses "path like" syntax to identify and navigate nodes in an XML document
-XPath contains over 200 built-in functions
-XPath is a major element in the XSLT standard
-XPath is a W3C recommendation
Xpath

Nodes:
In XPath, there are seven kinds of nodes: element, attribute, text, namespace,
processing-instruction, comment, and document nodes.

<?xml version="1.0" encoding="UTF-8"?> <bookstore> (root element node)


<bookstore>
<book> <author>J K. Rowling</author> (element node)
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year> lang="en" (attribute node)
<price>29.99</price>
</book>
</bookstore>
Xpath

Atomic values: Atomic values are nodes with no children or parent.

<?xml version="1.0" encoding="UTF-8"?> J K. Rowling


<bookstore>
<book>
<title lang="en">Harry Potter</title> "en"
<author>J K. Rowling</author>
</book>
</bookstore>
Xpath: selecting node

XPath uses path expressions to select nodes in an XML document. The node is selected by following a path
or steps.
Expression Description
nodename Selects all nodes with the name "nodename"
/ Selects from the root node
// Selects nodes in the document from the current node that match the selection
no matter where they are
. Selects the current node
.. Selects the parent of the current node
@ Selects attributes
Xpath: selecting node

Selecting Unknown Nodes

Wildcard Description

* Matches any element node

@* Matches any attribute node

node() Matches any node of any kind


Xpath: generation example
Xpath: Predicate

Predicates are used to find a specific node or a node that contains a specific value.
Predicates are always embedded in square brackets.
Path Expression Result
/bookstore/book[1] Selects the first book element that is the child of the bookstore
element.
/bookstore/book[last()] Selects the last book element that is the child of the bookstore
element
//title[@lang] Selects all the title elements that have an attribute named lang
//title[@lang='en'] Selects all the title elements that have a "lang" attribute with a
value of "en"
/bookstore/book[price>35.00] Selects all the book elements of the bookstore element that have a
price element with a value greater than 35.00
Xpath: Operators
Operator Description Example
| Computes two node-sets //book | //cd
+ Addition 6+4
- Subtraction 6-4
* Multiplication 6*4
div Division 8 div 4
= Equal price=9.80
!= Not equal price!=9.80
< Less than price<9.80
<= Less than or equal to price<=9.80
> Greater than price>9.80
>= Greater than or equal to price>=9.80
or or price=9.80 or price=9.70
and and price>9.00 and price<9.90
mod Modulus (division remainder) 5 mod 2
Xpath: absolute and relative
Xpath: Generator

There are online tools to generate Xpath.


Xquery

• XQuery is to XML what SQL is to databases.


• XQuery is the language for querying XML data
• XQuery for XML is like SQL for databases
• XQuery is built on XPath expressions
• XQuery is supported by all major databases
• XQuery is a W3C Recommendation
Xquery

XQuery can be used to:


-Extract information to use in a Web Service
-Generate summary reports
-Transform XML data to XHTML
-Search Web documents for relevant information
Xquery
Functions
books.xml XQuery uses functions to extract data from XML documents.
<bookstore> The doc() function is used to open the "books.xml" file:
<book category="COOKING">
<title lang="en">Everyday Italian</title> doc("books.xml")
<author>Giada De Laurentiis</author>
<year>2005</year> Path Expressions
<price>30.00</price> XQuery uses path expressions to navigate through elements in an
</book> XML document.
<book category="CHILDREN">
<title lang="en">Harry Potter</title> doc("books.xml")/bookstore/book/title
<author>J K. Rowling</author>
<year>2005</year> The XQuery above will extract the following:
<price>29.99</price> <title lang="en">Everyday Italian</title>
</book> <title lang="en">Harry Potter</title>
</bookstore>
Xquery
Predicates
books.xml XQuery uses predicates to limit the extracted data from
<bookstore>
XML documents.
<book category="COOKING">
<title lang="en">Everyday Italian</title> The following predicate is used to select all the book
<author>Giada De Laurentiis</author> elements under the bookstore element that have a price
<year>2005</year> element with a value that is less than 30:
<price>30.00</price>
</book> doc("books.xml")/bookstore/book[price<30]
<book category="CHILDREN">
<title lang="en">Harry Potter</title> Output:
<author>J K. Rowling</author> <book category="CHILDREN">
<year>2005</year> <title lang="en">Harry Potter</title>
<price>29.99</price> <author>J K. Rowling</author>
</book> <year>2005</year>
</bookstore> <price>29.99</price>
</book>
Xquery FLWOR expressions

For - selects a sequence of nodes


Let - binds a sequence to a variable
Where - filters the nodes
Order by - sorts the nodes
Return - what to return (gets evaluated once for every node)
Example
for $x in doc("books.xml")/bookstore/book
books.xml where $x/price<30
<bookstore>
return $x/title
<book category="COOKING">
<title lang="en">Harry Potter</title> Output:
<author>Giada De Laurentiis</author> <title lang="en">Everyday Italian</title>
<year>2005</year>
<price>30.00</price> With FLWOR you can sort the result:
</book> for $x in doc("books.xml")/bookstore/book
<book category="CHILDREN"> where $x/price<=30
<title lang="en">Everyday Italian</title>
order by $x/title
<author>J K. Rowling</author>
<year>2005</year> return $x/title
<price>29.99</price> Output:
</book> <title lang="en">Everyday Italian</title>
</bookstore> <title lang="en">Harry Potter</title>
XQuery: Basic syntax rule

Some basic syntax rules:


-XQuery is case-sensitive
-XQuery elements, attributes, and variables must be valid XML names
-An XQuery string value can be in single or double quotes
-An XQuery variable is defined with a $ followed by a name, e.g. $bookstore
XML Parser

-An XML parser is a software library or package that provides interfaces for client
applications to work with an XML document. The XML Parser is designed to read the XML
and create a way for programs to use XML.
-XML parser validates the document and check that the document is well formatted.
XML Parser

Types of Parser
1. Document Object Model (DOM)
2. SAX
Document Object Model (DOM)

-The DOM defines a standard for accessing and manipulating documents:


-The XML DOM defines a standard way for accessing and manipulating XML
documents. It presents an XML document as a tree-structure.

The XML DOM is:


A standard object model for XML
A standard programming interface for XML
Platform- and language-independent
A W3C standard
Document Object Model (DOM)

-A DOM document is a collection of nodes or pieces of information organized in a hierarchy.


-This hierarchy allows a developer to navigate through the tree looking for specific
information. Because it is based on a hierarchy of information, the DOM is said to be tree
based.
Document Object Model (DOM)
Document Object Model (DOM)

-All XML elements can be accessed through the XML DOM.


Document Object Model (DOM)
<html>
<body> https://www.w3schools.com/xml/xml_parser.asp
<p id="demo"></p>
<script>
var text, parser, xmlDoc;
text = "<bookstore><book>" +
"<title>Everyday Italian</title>" +
"<author>Giada De Laurentiis</author>" +
"<year>2005</year>" +
"</book></bookstore>";
parser = new DOMParser();
xmlDoc = parser.parseFromString(text,"text/xml");
document.getElementById("demo").innerHTML =
xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;
</script>
</body>
</html>
SAX
SAX is a programming interface for processing XML files based on events. The DOM’s
counterpart, SAX, has a very different way of reading XML code.

-SAX stands for the Simple API for XML parsing.


-SAX, which is an event-driven online algorithm for parsing XML documents.
SAX is a way of reading data from an XML document that is an alternative to the
Document Object Model’s mechanism (DOM).
-SAX is a programming interface for processing XML files based on events.
- To access data from XML file, SAX follows top to bottom approach.
- It does not create any internal structure.
When to use SAX
You should use a SAX parser when −
• You can process the XML document in a linear fashion from top to down.
• The document is not deeply nested.
• You are processing a very large XML document whose DOM tree would
consume too much memory. Typical DOM implementations use ten
bytes of memory to represent one byte of XML.
• The problem to be solved involves only a part of the XML document.
• Data is available as soon as it is seen by the parser, so SAX works well for
an XML document that arrives over a stream.
DOM vs SAX
DOM SAX
DOM stands for Document Object Model. SAX stands for the Simple API for XML parsing.
DOM needs a lot of memory. SAX does not need a lot of memory.
It is memory inefficient. It is memory efficient.

DOM parser work on the Document object model. SAX work on the event-based XML parser.
DOM is Faster. SAX is slower.
DOM loads whole XML document memory. SAX load a small part of XML memory.

It’s not suitable for a large XML file. It’s suitable for a large XML file.

DOM implements DOM API. SAX impliment SAX API.


Java provides support DOM parsers, and we can parse Java provides support SAX parsers, and we can parse
XML files in java using DOM parser. XML files in java using SAX parser.
Find me

9851083215

Santosh. it288@ mail . com

www. phtechno. com

Anamnagar , Kathmandu

You might also like