0% found this document useful (0 votes)
120 views

CACS 205: Web Technology XML: Prepared by Krishna Pd. Acharya (Mechi Multiple Campus)

Uploaded by

Dipesh Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
120 views

CACS 205: Web Technology XML: Prepared by Krishna Pd. Acharya (Mechi Multiple Campus)

Uploaded by

Dipesh Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 67

CACS 205: Web Technology

XML

PREPARED BY KRISHNA PD. ACHARYA

(MECHI MULTIPLE CAMPUS)


The client tier 10 Hrs
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, sequence), occurrence indicators (max
occurs, min occurs)
DTD:
Internal declaration,
Private external declaration,
Public external declaration, defining elements and attributes
XSL/XSLT, Xpath, Xquery;
Sax:Dom, Creating XML Parser
2
Introduction to xml 10 Hrs.
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Mechi Multiple Campus</to>
<from>Krishna Acharya</from>
<heading>Workshop</heading>
<body>we are going to conduct a workshop this month</body>
</note>

3
Introduction to xml 10 Hrs.
 XML stands for Extensible Markup Language. It is a text-based markup language derived from
Standard Generalized Markup Language (SGML).
 XML is not going to replace HTML in the near future, but it introduces new possibilities by
adopting many successful features of HTML.
 XML was designed to be both human- and machine-readable.
 XML is designed to store and transport data.
 Xml was released in late 90’s. it was created to provide an easy to use and store self
describing data.
 XML became a W3C Recommendation on February 10, 1998.
 XML is not a replacement for HTML.
 XML is designed to be self-descriptive.(data that describes both its content and structure.)
 XML is designed to carry data, not to display data.
 XML tags are not predefined. You must define your own tags.
 XML is platform independent and language independent.
4
Introduction to xml 10 Hrs.
Characteristics of XML
 XML is extensible − XML allows us to create your own self-descriptive tags, or language, that
suits your application.
 XML carries the data, does not present it − XML allows us to store the data irrespective of how it
will be presented.
 XML is a public standard − XML was developed by an organization called the World Wide Web
Consortium (W3C) and is available as an open standard.
 XML separates data from HTML
 XML simplifies data sharing
 XML simplifies data transport
 XML simplifies Platform change
 XML increases data availability
 XML can be used to create new internet languages

5
HTML vs XML 10 Hrs.

6
Introduction to xml 10 Hrs.
Usages of xml in web development
 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.

Is XML a Programming Language?


 XML does not qualify to be a programming language as it does not perform any computation
or algorithms.
 It is usually stored in a simple text file and is processed by special software that is capable of
interpreting XML.

7
Introduction to xml 10 Hrs.
XML Basics:
 Developed by W3C (World Wide Web Consortium).
 It is the pared-‐down version of SGML (Standardized Generalized Markup Language).
 Designed especially for web documents
 Allows you to create your own language for displaying documents.
“XML is not the replacement of HTML.”
 XML requires a processing application. There is no XML browser in market yet.
 XML isn’t about display – it’s about Structure.
 XML file should be saved with the extension “.xml”.
 You can use any text editor to write XML code. And the output can be viewed in any browser.
 Every XML page consists of processing instruction at the very first line.
 There should be a root element in every XML document, which should be unique.

8
Introduction to xml 10 Hrs.
Advantages of XML:
 The first benefit of XML is that because you are writing your own markup language, you are not
restricted to a limited set of tags defined by proprietary vendors.
 With XML, you can create your own set of tags at your own pace.
 XML allows every person/organization to build their own tag library which suits their needs
perfectly.
 XML allows you to define all sorts of tags with all sorts of rules, such as tags representing business
rules or tags representing data description or data relationships.
 With XML, GUI is extracted. Thus, changes to display do not require futzing with the data.
Instead separate style-sheet will specify a table display or a list display.
 Searching the data is easy and efficient. Search-engine can simply parse the
description- bearing tags rather than muddling in the data. Tags provide the search engines
with the intelligence they lack.
 Complex relationships like tree and inheritance can be communicated.
 The code is much more legible to the person coming into the environment with no prior
knowledge.

9
Introduction to xml 10 Hrs.
Disadvantages of XML:
 XML requires a processing application.
 There are no XML browsers on the market yet. Thus, XML documents must either be converted
into HTML before distribution or converting it to HTML on‐the‐fly by middleware.
 Barring translation, developers must code their own processing applications.
 XML isn't about display - it's about structure.
 This has implications that make the browser question secondary. So the whole issue of what is to
be displayed and by what means is intentionally left to other applications.

10
Rules for writing XML 10 Hrs.
 The first line must be <?XML version = “1.0” ?>.
 Tags are enclosed in angle brackets.
 Tags are case sensitive and must have a matching closing tag.
 Tags may contain attributes in the form name = “value”.
 Tags may contain text, other tags, or both. Tags content lies between the starting and ending
tag.
XML Declaration
 To begin an XML document, it is a good idea to include the XML declaration as the very first
line of the document.
 The XML declaration is a processing instruction that notifies the processing agent that the
following document has been marked up as an XML document.
 The XML declaration looks as following:
 <?xml version = "1.0“ ?>
The XML declaration might look like the following:
<?xml version = "1.0"? standalone = "yes" encoding = "UTF-8"?>

11
XML 10 Hrs.
Elements naming convention
 Element names are case-sensitive
 Element names must start with a letter or underscore
 Element names cannot start with the letters xml (or XML, or Xml, etc)
 Element names can contain letters, digits, hyphens, underscores, and periods
 Element names cannot contain spaces.

12
XML 10 Hrs.
Elements
 Elements are the basic unit of XML content.
 Syntactically, an element consists of a start tag, and an end tag, and everything in between.
Example:
<NAME>Krishna Acharya </NAME>
 All XML documents must have at least one root element to be well formed. The root element,
also often called the document tag, must follow the prolog (XML declaration plus DTD) and
must be a nonempty tag that encompasses the entire document.
 XML defines the text between the start and end tags to be "character data" and the text within
the tags to be "markup".
Rules for Elements
 XML documents can have only one root element.
 Every starting tag must have a matching end tag.
 XML elements must be properly but must not must not overlapped
 XML attribute values must always be quoted
 XML is case sensitive. The tag <Letter> is different from the tag <letter>.
 white-space is preserved in xml.
13
XML 10 Hrs.
Rules for Elements
 Comments and processing instructions may not appear inside tags.
valid invalid
 No unescaped < or & signs may occur inside character data.

valid

 XML documents that conform to the syntax rules above are said to be "Well Formed" XML
documents. 14
XML
XML Attributes
 XML elements can have attributes. By the use of attributes we can add the information
about the element. XML attributes enhance the properties of the elements.
Avoid XML Attributes?
 attributes cannot contain multiple values (elements can)
 attributes cannot contain tree structures (elements can)
 attributes are not easily expandable (for future changes)
Q1. Create well-form xml document form bellow figure.

15
XML
CDATA :
 The term CDATA means, Character Data. CDATA is defined as blocks of text that are
not parsed by the parser, but are otherwise recognized as markup.
 The predefined entities such as &lt;, &gt;, and &amp; require typing and are generally
difficult to read in the markup.
 In such cases, CDATA section can be used. By using CDATA section, you are
commanding the parser that the particular section of the document contains no
markup and should be treated as regular text.
<?xml version="1.0" encoding="UTF-8"?>
<script>
<![CDATA[
<message> Welcome to Mecchi Multiple Campus </message>
]]>
</script>

 The above markup code shows an example of CDATA. Here, each character written
inside the CDATA section is ignored by the parser.
 In the above syntax, everything between <message> and </message> is treated as
character data and not as markup.

16
XML
Processing Instructions (PIs)
 Processing instructions (PIs) allow documents to contain instructions for applications. PIs
are not part of the character data of the document, but MUST be passed through to
the application.
 Processing instructions (PIs) can be used to pass information to applications. PIs can
appear anywhere in the document outside the markup. They can appear in the
prolog, including the document type definition (DTD), in textual content, or after the
document.
 Eg <?xml-stylesheet href = "style.css" type = "text/css"?>

17
XML DTD
 The XML Document Type Declaration, commonly known as DTD, is a way to describe
XML language precisely.
 DTDs check vocabulary and validity of the structure of XML documents against
grammatical rules of appropriate XML language.
 An XML DTD can be either specified inside the document, or it can be kept in a
separate document and then liked separately.
 with a DTD, independent groups of people can agree on a standard DTD for
interchanging data.
 An application can use a DTD to verify that XML data is valid.

Internal vs External

note.dtd
18
XML DTD
• Element content Categories
Content Category Description
ANY Element type may contain any well formed XML data.
EMPTY Element type may contain any text or child elements-‐ only elements
attributes are permitted.

Element Element type contains only child elements no additional text is


permitted.

Mixed Element type may contain text and/or child element.


PCDATA Element type may contain text (character data) only.

Cardinality
Operators Description

[none] The absence of a cardinality operator character indicates that one, and only
one, instance of child element is allowed (required).

? Zero or one element – optional singular element.

* Zero or more element – optional element(s).

+ One or more child elements – required element(s).

19
XML DTD
 !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"
PCDATA
 #PCDATA means parsed character data.(parse-able text 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 &amp; &lt; and &gt; entities, respectively.

20
XML DTD
Declaring Elements
<!ELEMENT element-name (element-content)>
Empty Elements
<!ELEMENT br EMPTY>
Elements with Parsed Character Data
<!ELEMENT element-name (#PCDATA)>
<!ELEMENT from (#PCDATA)>
Elements with any Contents(an contain any combination of parsable data)
<!ELEMENT element-name ANY>
Example:
<!ELEMENT note ANY>(can contain any combination of parsable data)
Elements with Children (sequences)
<!ELEMENT element-name (child1,child2,...)>
Example:
<!ELEMENT note (to,from,heading,body)>

21
XML DTD
Declaring Only One Occurrence of an Element
<!ELEMENT element-name (child-name)>
Example:
<!ELEMENT note (message)>(child element "message" must occur once, and only once
inside the "note" element.)
Declaring Minimum One Occurrence of an Element
<!ELEMENT element-name (child-name+)>
Example:
<!ELEMENT note (message+)>(the child element "message" must occur one or more
times inside the "note" element.)
Declaring Zero or More Occurrences of an Element
<!ELEMENT element-name (child-name*)>
Example:
<!ELEMENT note (message*)>
Declaring either/or Content
<!ELEMENT note (to,from,header,(message|body))>
Declaring Zero or One Occurrences of an Element
<!ELEMENT note (message?)>
22
XML DTD
Declaring Mixed Content
<!ELEMENT note (#PCDATA|to|from|header|message)*>
ATTRIBUTES
In a DTD, attributes are declared with an ATTLIST declaration.
<!ATTLIST element-name attribute-name attribute-type attribute-value>

<!ATTLIST payment type CDATA "check">


XML example:
<payment type="check" />

23
XML DTD
A Default Attribute Value
DTD:
<!ELEMENT square EMPTY>
<!ATTLIST square width CDATA "0">
Valid XML:
<square width="100" />
#REQUIRED
DTD:
<!ATTLIST person number CDATA #REQUIRED>
<!ATTLIS Valid XML:
<person number="5677" />
Invalid XML:
<person /> T element-name attribute-name attribute-type #REQUIRED>
#IMPLIED
<!ATTLIST element-name attribute-name attribute-type #IMPLIED>
DTD:
<!ATTLIST contact fax CDATA #IMPLIED>
Valid XML:
<contact fax="555-667788" />
Valid XML:
<contact />
24
XML
#FIXED
DTD
<!ATTLIST element-name attribute-name attribute-type #FIXED "value">
DTD:
<!ATTLIST sender company CDATA #FIXED "Microsoft">
Valid XML:
<sender company="Microsoft" />
Invalid XML:
<sender company=“Mechicampus" />
Enumerated Attribute Values
<!ATTLIST element-name attribute-name (en1|en2|..) default-value>
DTD:
<!ATTLIST payment type (check|cash) "cash">
XML example:
<payment type="check" />
or
<payment type="cash" />

DTD - Entities
Entities are used to define shortcuts to special characters. Entities can be declared internal or external.
<!ENTITY entity-name "entity-value">
DTD Example:
<!ENTITY writer "Donald Duck.">
<!ENTITY copyright "Copyright W3Schools.">
XML example:
<author>&writer;&copyright;</author>

25
XML DTD
Avoid using attributes?
 attributes cannot contain multiple values (child elements can)
 attributes are not easily expandable (for future changes)
 attributes cannot describe structures (child elements can)
 attributes are more difficult to manipulate by program code
 attribute values are not easy to test against a DTD

26
XML DTD (Product Catalog DTD)

27
Limitation of DTD
 DTD are not extensible, unlike XML itself.
 Only one DTD may be associated with each XML document.
 DTDs do not work well with XML namespaces.
 Supports very weak data typing.
 Limited content model descriptions.
 No object oriented type object inheritance.
 A document can override / ignore an external DTD using internal subset.
 Non-XML syntax.
 No DOM support.
 Relatively few older, more expensive tools.
 Very limited support to modularity and reuse.
 Too simple ID attribute mechanism (no points to requirements, uniqueness scope, etc)

28
XML XSD
 XML Schema is commonly known as XML Schema Definition (XSD). 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.

29
XML XSD
 .

30
XML XSD
 .

31
XML XSD
 .

32
XML XSD
 .

33
XML XSD
Attributes
Attributes in XSD provide extra information within an element. Attributes have name and type
property as shown below −
<xs:attribute name = "x" type = "y"/>
Elements
<xs:element name = "x" type = "y"/>
Definition Types
 simple Type

Simple type element is used only in the context of the text. Some of the predefined simple
types are: xs:string,xs:decimal,xs:integer,xs:Boolean,xs:date,xs:time
 Complex Type
A complex type is a container for other element definitions. This allows you to specify which
child elements an element can contain and to provide some structure within your XML
documents.
<xs:complexType>
Default and Fixed Values for Attributes
<xs:attribute name="lang" type="xs:string" default="EN"/> you can define new value
<xs:attribute name="lang" type="xs:string" fixed="EN"/> you can not override it
34
XML XSD
<lastname lang="EN">Smith</lastname> in xml
<xs:attribute name="lang" type="xs:string"/> in xsd
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"/>
Restrictions on Content
 When an XML element or attribute has a data type defined, it puts restrictions on the
element's or attribute's content.
 If an XML element is of type "xs:date" and contains a string like "Hello World", the
element will not validate.
 With XML Schemas, you can also add your own restrictions to your XML elements and
attributes. These restrictions are called facets.

35
XML XSD
Restrictions on Values
 The following example defines an element called "age" with a restriction. The value of age
cannot be lower than 0 or greater than 120:
Restrictions on a Set of Values

 To limit the content of an XML element to a set of acceptable values, we would use the
enumeration constraint.
 The example below defines an element called "car" with a restriction. The only acceptable
values are: Audi, Golf, BMW:

36
XML XSD
Restrictions on a Series of Values
 To limit the content of an XML element to define a series of numbers or letters that can be
used, we would use the pattern constraint.
 The example below defines an element called "letter" with a restriction. The only
acceptable value is ONE of the LOWERCASE letters from a to z:

 defines an element called "initials" with a restriction. The only acceptable value is THREE of
the UPPERCASE letters from a to z:

37
XML XSD
Restrictions on a Series of Values
 An element called "choice" with a restriction. The only acceptable value is ONE of the
following letters: x, y, OR z:

 An element called "initials" with a restriction. The only acceptable value is THREE of the
LOWERCASE OR UPPERCASE letters from a to z:

38
XML XSD
Restrictions on a Series of Values
 an element called "prodid" with a restriction. The only acceptable value is FIVE digits in a
sequence, and each digit must be in a range from 0 to 9.

 an element called "letter" with a restriction. The acceptable value is zero or more
occurrences of lowercase letters from a to z:

39
XML XSD
Restrictions on a Series of Values
 an element called "letter" with a restriction. The acceptable value is one or more pairs of
letters, each pair consisting of a lower case letter followed by an upper case letter. For
example, "sToP" will be validated by this pattern, but not "Stop" or "STOP" or "stop":

 defines an element called "gender" with a restriction. The only acceptable value is male
OR female

40
XML XSD
Restrictions on a Series of Values

 defines an element called "password" with a restriction. There must be exactly eight
characters in a row and those characters must be lowercase or uppercase letters from
a to z, or a number from 0 to 9.
Restrictions on Whitespace Characters

 The white Space constraint is set to "preserve", which means that the XML processor
WILL NOT remove any white space characters
 The white Space constraint is set to “replace", which means that the XML processor
WILL REMOVE all white space characters 41
XML XSD
Restrictions on Length

 defines an element called "password" with a restriction. The value must be exactly
eight characters.
 defines another element called "password" with a restriction. The value must be
minimum five characters and maximum eight characters.

42
XML XSD

43
XML DTD

44
XSL Languages
 XSL stands for eXtensible Stylesheet Language. The World Wide Web Consortium (W3C)
started to develop XSL because there was a need for an XML based Style sheet Language.

 CSS = HTML Style Sheets

 HTML uses predefined tags and the meanings of the tags are well understood. The <table>
element in HTML defines a table and a browser knows how to display it. Adding styles to
HTML elements is simple. Telling a browser to display an element in a special font or color is
easy with CSS.

 XSL = XML Style Sheets

 XML does not use predefined tags (we can use any tag-‐names we like), and the
meaning of these tags are not well understood. A <table> element could mean an HTML
table, a piece of furniture, or something else and a browser does not know how to display
it.

 XSL describes how the XML document should be displayed! XSL is More Than a Style Sheet
Language

45
XSL Languages
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

XSLT -‐ the language for transforming XML documents.


 XSLT is a language for transforming XML documents into XHTML documents or to other
XML documents.
 XPath is a language for navigating in XML documents.
 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
 XSLT uses XPath to navigate in XML documents
 XSLT is a W3C Recommendation
 XSLT = XSL Transformations

46
XSL Languages
 XSLT is used to transform an XML document into another XML document, or another type of
document that is recognized by a browser, like HTML and XHTML. Normally XSLT does this by
transforming each XML element into an (X) HTML element.
 With XSLT you can add/remove elements and attributes to or from the output file. You can
also rearrange and sort elements, perform tests and make decisions about which elements
to hide and display, and a lot more.
 A common way to describe the transformation process is to say that XSLT transforms an
XML source-‐tree into an XML result-‐tree.
XSLT Uses XPath
XSLT uses XPath to find information in an XML document. XPath is used to navigate through
elements and attributes in XML documents
How does it works
In the transformation process, XSLT uses XPath to define parts of the source document that
should match one or more predefined templates. When a match is found, XSLT will transform
the matching part of the source document into the result document.
 XSLT is a W3C Recommendation
 XSLT became a W3C Recommendation 16. November 1999.

47
xpath
 XPath is a query language that is used for traversing through an XML document. It is
used commonly to search particular elements or attributes with matching patterns.
 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 defines a pattern or path expression to select nodes or node sets in an XML document.
These patterns are used by XSLT to perform transformations.
 The path expressions look like very similar to the general expressions we used in traditional file
system.

48
xpath
 XPath expression generally defines a pattern in order to select a set of nodes. These
patterns are used by XSLT to perform transformations.

49
XSL Languages

XSLT Browsers
Nearly all major browsers have support for XML and XSLT. Like Mozilla Firefox, Netscape,
Opera, Internet Explorer.
Advantage of XSLT
 XSLT provides an easy way to merge XML data into presentation because it applies user
defined transformations to an XML document and the output can be HTML, XML, or any
other structured document.
 XSLT provides Xpath to locate elements/attribute within an XML document. So it is more
convenient way to traverse an XML document rather than a traditional way, by using
scripting language.
 XSLT is template based. So it is more resilient to changes in documents than low level DOM
and SAX.
 By using XML and XSLT, the application UI script will look clean and will be easier to
maintain.
 XSLT templates are based on XPath pattern which is very powerful in terms of performance
to process the XML document.
 XSLT can be used as a validation language as it uses tree-pattern-matching approach.
 You an change the output simply modifying the transformations in XSL files.
50
XSL Languages
XSLT - Transformation
Correct Style Sheet Declaration
 The root element that declares the document to be an XSL style sheet is
<xsl:stylesheet> or <xsl:transform>.
 Note: <xsl:stylesheet> and <xsl:transform> are completely synonymous and either can
be used!

 The <xsl:template> Element


The match attribute is used to associate a template with an XML element. The match
attribute can also be used to define a template for the entire XML document. The value
of the match attribute is an XPath expression (i.e. match="/" defines the whole
document).

51
XSL Languages
XSLT - Transformation
Correct Style Sheet Declaration
 The root element that declares the document to be an XSL style sheet is
<xsl:stylesheet> or <xsl:transform>.
 Note: <xsl:stylesheet> and <xsl:transform> are completely synonymous and either can
be used!

 The <xsl:template> Element


The match attribute is used to associate a template with an XML element. The match
attribute can also be used to define a template for the entire XML document. The value
of the match attribute is an XPath expression (i.e. match="/" defines the whole
document).

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. 52
XSL Languages
XSLT - Transformation
<xsl:for-each> Element
can be used to select every XML element of a specified node-set.

• The <xsl:sort> element is used to sort the output.

53
XSL Languages
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="school.xsl" type="text/xsl"?> <xsl:stylesheet version="1.0"
<catalog> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<cd> <xsl:template match="/">
<title>Empire Burlesque</title> <html>
<artist>Bob Dylan</artist> <body>
<country>USA</country>
<h2>My CD Collection</h2>
<company>Columbia</company>
<table border="1">
<price>10.90</price>
<tr bgcolor="#9acd32">
<year>1985</year>
<th>Title</th>
</cd>
<th>Artist</th>
<cd>
</tr>
<title>Hide your heart</title>
<tr>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<td><xsl:value-of select="catalog/cd/title"/></td>
<company>CBS Records</company>
<td><xsl:value-of select="catalog/cd/artist"/></td>
<price>9.90</price>
</tr>
<year>1988</year> </table>
</cd> </body>
</catalog> </html>
</xsl:template>
</xsl:stylesheet>
54
XSL Languages
<?xml version="1.0" encoding="UTF-8"?> <xsl:template match="/">
<?xml-stylesheet href="school.xsl" type="text/xsl"?> <html>
<catalog>
<body>
<cd>
<title>Empire Burlesque</title> <h2>My CD Collection</h2>
<artist>Bob Dylan</artist> <table border="1">
<country>USA</country> <tr bgcolor="#9acd32">
<company>Columbia</company> <th>Title</th>
<price>10.90</price>
<th>Artist</th>
<year>1985</year>
</cd> </tr>
<cd> <xsl:for-each select="catalog/cd">
<title>Hide your heart</title> <tr>
<artist>Bonnie Tyler</artist> <td><xsl:value-of select="title" /></td>
<country>UK</country>
<td><xsl:value-of select="artist" /></td>
<company>CBS Records</company>
<price>9.90</price>
</tr>
<year>1988</year> </xsl:for-each>
</cd> </table>
</catalog> </body>
---------------------------------- </html>
<?xml version="1.0" encoding="UTF-8"?> </xsl:template>
<xsl:stylesheet version="1.0" </xsl:stylesheet>
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
55
XSL Languages
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="school.xsl" type="text/xsl"?> <xsl:template match="/">
<catalog> <html>
<cd> <body>
<title>Still got the blues</title> <h2>My CD Collection</h2>
<artist>Gary Moore</artist> <table border="1">
<country>UK</country> <tr bgcolor="#9acd32">
<company>Virgin records</company> <th>Title</th>
<price>10.20</price> <th>Artist</th>
<year>1990</year> </tr>
</cd> <xsl:for-each select="catalog/cd">
<cd> <xsl:sort select="artist"/>
<title>Greatest Hits</title> <tr>
<artist>Dolly Parton</artist> <td><xsl:value-of select="title"/></td>
<country>USA</country> <td><xsl:value-of select="artist"/></td>
<company>RCA</company> </tr>
<price>9.90</price> </xsl:for-each>
<year>1982</year> </table>
</cd> </body>
</catalog> </html>
---------------------------------- </xsl:template>
<?xml version="1.0" encoding="UTF-8"?> </xsl:stylesheet>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 56
XSL Languages
<?xml version="1.0" encoding="UTF-8"?> <xsl:template match="/">
<?xml-stylesheet href="school.xsl" type="text/xsl"?> <html>
<catalog> <body>
<cd> <h2>My CD Collection</h2>
<title>Empire Burlesque</title> <table border="1">
<artist>Bob Dylan</artist> <tr bgcolor="#9acd32">
<country>USA</country> <th>Title</th>
<company>Columbia</company> <th>Artist</th>
<price>10.90</price> <th>Price</th>
<year>1985</year> </tr>
</cd> <xsl:for-each select="catalog/cd">
<cd> <xsl:if test="price>10">
<title>Hide your heart</title> <tr>
<artist>Bonnie Tyler</artist> <td><xsl:value-of select="title"/></td>
<country>UK</country> <td><xsl:value-of select="artist"/></td>
<company>CBS Records</company> <td><xsl:value-of select="price"/></td>
<price>9.90</price> </tr>
<year>1988</year> </xsl:if>
</cd> </xsl:for-each>
</catalog> </table>
---------------------------------- </body>
<?xml version="1.0" encoding="UTF-8"?> </html>
<xsl:stylesheet version="1.0" </xsl:template>
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> </xsl:stylesheet> 57
XSL Languages
<?xml version="1.0" encoding="UTF-8"?> <body>
<?xml-stylesheet href="school.xsl" type="text/xsl"?> <h2>My CD Collection</h2>
<catalog> <table border="1">
<cd> <tr bgcolor="#9acd32">
<title>Empire Burlesque</title> <th>Title</th>
<artist>Bob Dylan</artist> <th>Artist</th>
<country>USA</country> </tr>
<company>Columbia</company> <xsl:for-each select="catalog/cd">
<price>10.90</price> <tr>
<year>1985</year> <td><xsl:value-of select="title"/></td>
</cd> <xsl:choose>
<cd> <xsl:when test="price > 10">
<title>Hide your heart</title> <td bgcolor="red">
<artist>Bonnie Tyler</artist> <xsl:value-of select="artist"/>
<country>UK</country> </td>
<company>CBS Records</company> </xsl:when>
<price>9.90</price> <xsl:when test="price > 9">
<year>1988</year> <td bgcolor="green">
</cd> <xsl:value-of select="artist"/></td>
<cd> </xsl:when>
<title>Greatest Hits</title> <xsl:otherwise>
<artist>Dolly Parton</artist> <td><xsl:value-of select="artist"/></td>
<country>USA</country> </xsl:otherwise>
<company>RCA</company> </xsl:choose>
<price>5.90</price> </tr>
<year>1982</year> </xsl:for-each>
</cd> </table>
</catalog> </body>
---------------------------------- </html>
<?xml version="1.0" encoding="UTF-8"?> </xsl:template>
<xsl:stylesheet version="1.0" </xsl:stylesheet>
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html> 58
XSL Languages
<?xml version = "1.0"?> <html>
<?xml-stylesheet href="school.xsl" type="text/xsl"?> <body>
<class> <h2>Students</h2>
<student rollno = "393"> <table border = "1">
<firstname>Dinkar</firstname> <tr bgcolor = "#9acd32">
<lastname>Kad</lastname> <th>Roll No</th>
<nickname>Dinkar</nickname> <th>First Name</th>
<marks>85</marks> <th>Last Name</th>
</student> <th>Nick Name</th>
<student rollno = "493"> <th>Marks</th>
<firstname>Vaneet</firstname> </tr>
<lastname>Gupta</lastname> <xsl:for-each select="class/student">
<nickname>Vinni</nickname> <tr>
<marks>95</marks> <td>
</student> <xsl:value-of select = "@rollno"/>
<student rollno = "593"> </td>
<firstname>Jasvir</firstname> <td><xsl:value-of select = "firstname"/></td>
<lastname>Singh</lastname> <td><xsl:value-of select = "lastname"/></td>
<nickname>Jazz</nickname> <td><xsl:value-of select = "nickname"/></td>
<marks>90</marks> <td><xsl:value-of select = "marks"/></td>
</student> </tr>
</class> </xsl:for-each>
---------------------------------- </table>
<?xml version = "1.0" encoding = "UTF-8"?> </body>
<xsl:stylesheet version = "1.0" </html>
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> </xsl:template>
<xsl:template match = "/"> </xsl:stylesheet>
59
Xquery
XQuery is to XML what SQL is to databases. XQuery is designed to query XML data.
 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
Application of 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
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
 XQuery comments are delimited by (: and :), e.g. (: XQuery Comment :)

60
Xquery
Example
The doc() function is used to open the “school.xml" file:
doc("school.xml")/catalog/cd/title
doc("school.xml")/catalog/cd[price<10]
doc("school.xml")/catalog/cd[price<10]/title

What is FLWOR?
FLWOR (pronounced "flower") is an acronym for "For, Let, Where, Order by, Return".
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)

for $x in doc("school.xml")/catalog/cd/
where $x/price>10
return $x/title
61
Xquery
Example
(:for $x in doc("school.xml")/catalog/cd
where $x/price>10
order by $x/title
return $x/title:)
ul>
{
for $x in doc("school.xml")/catalog/cd/title
order by $x
return <li>{$x}</li>
}
</ul>

62
DOM

63
DOM
 The DOM defines a standard for accessing and manipulating
documents:
 "The W3C Document Object Model (DOM) is a platform and language-
neutral interface that allows programs and scripts to dynamically access
and update the content, structure, and style of a document."
 The HTML DOM defines a standard way for accessing and manipulating
HTML documents. It presents an HTML document as a tree-structure.
 The XML DOM defines a standard way for accessing and manipulating
XML documents. It presents an XML document as a tree-structure.
 Understanding the DOM is a must for anyone working with HTML or XML.
 The XML DOM is:
 A standard object model for XML
 A standard programming interface for XML
 Platform- and language-independent
 A W3C standard
 In other words: The XML DOM is a standard for how to get, change, add, or delete XML
elements.
64
DOM Praser
<html>
<body>
<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>
65
SAX Praser
 SAX(Simple API for XML) is an event-driven algorithm for parsing XML
documents. SAX is an alternative to the Document Object Model
(DOM). Where the DOM reads the whole document to operate on XML,
SAX parsers read XML node by node. SAX processes documents state-
independently (the handling of an element does not depend on the
elements that came before). SAX parsers are read-only.
 SAX parsers are faster and require less memory. On the other hand,
DOM is easier to use and there are tasks, such as sorting elements,
rearranging elements or looking up elements, that are faster with DOM.

66
Assignment

67

You might also like