Vous êtes sur la page 1sur 14

1

B.COM – COMPUTER – III YEAR - NOTES


WEB PROGRAMMING

INDEX – UNIT - V

S.no: Title Page no:

Extensible Markup Language (XML) –


1. 02 - 04
Introduction

2. Creating XML Documents 05 - 05

3. XML Style Sheets 06 - 07

4. Hyperlinks in XML Documents 08 - 09

5. XML Document Object Model 10 - 11

6. XML Query Language 12 - 14

http://www.saif4u.webs.com
2

Extensible Markup Language (XML) – Introduction


Extensible Markup Language (XML) is a set of rules for encoding documents
inmachine-readable form. It is defined in the XML 1.0 Specification[4] produced by
the W3C, and several other related specifications, all gratis open standards.[5]

The design goals of XML emphasize simplicity, generality, and usability over
theInternet.[6] It is a textual data format with strong support via Unicode for the
languages of the world. Although the design of XML focuses on documents, it is widely
used for the representation of arbitrary data structures, for example in web services.

Many application programming interfaces (APIs) have been developed that software
developers use to process XML data, and several schema systems exist to aid in the
definition of XML-based languages.

As of 2009, hundreds of XML-based languages have been


developed,[7] including RSS,Atom, SOAP, and XHTML. XML-based formats have
become the default for most office-productivity tools, including Microsoft Office (Office
Open XML), OpenOffice.org(OpenDocument), and Apple's iWork.[8]
XML was designed to transport and store data.

HTML was designed to display data.

What is XML?

• XML stands for EXtensible Markup Language


• XML is a markup language much like HTML
• XML was designed to carry data, not to display data
• XML tags are not predefined. You must define your own tags
• XML is designed to be self-descriptive
• XML is a W3C Recommendation

The Difference Between XML and HTML

XML is not a replacement for HTML.

XML and HTML were designed with different goals:

• XML was designed to transport and store data, with focus on what data is
• HTML was designed to display data, with focus on how data looks

HTML is about displaying information, while XML is about carrying information.

http://www.saif4u.webs.com
3

XML Does Not DO Anything

Maybe it is a little hard to understand, but XML does not DO anything. XML was created
to structure, store, and transport information.

The following example is a note to Tove, from Jani, stored as XML:

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

The note above is quite self descriptive. It has sender and receiver information, it also
has a heading and a message body.

But still, this XML document does not DO anything. It is just information wrapped in
tags. Someone must write a piece of software to send, receive or display it.

With XML You Invent Your Own Tags

The tags in the example above (like <to> and <from>) are not defined in any XML
standard. These tags are "invented" by the author of the XML document.

That is because the XML language has no predefined tags.

The tags used in HTML are predefined. HTML documents can only use tags defined in
the HTML standard (like <p>, <h1>, etc.).

XML allows the author to define his/her own tags and his/her own document structure.

XML is Not a Replacement for HTML

XML is a complement to HTML.

It is important to understand that XML is not a replacement for HTML. In most web
applications, XML is used to transport data, while HTML is used to format and display
the data.

My best description of XML is this:

XML is a software- and hardware-independent tool for carrying information.

http://www.saif4u.webs.com
4

XML is a W3C Recommendation

XML became a W3C Recommendation February 10, 1998.

XML is Everywhere

XML is now as important for the Web as HTML was to the foundation of the Web.

XML is the most common tool for data transmissions between all sorts of applications.

http://www.saif4u.webs.com
5

Creating XML Documents

XML stands for eXtensible Markup Language.

XML is designed to transport and store data.

XML is important to know, and very easy to learn.

XML Document Example

<?xml version="1.0"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

Using a Text Editor

1. Select a text editor such as Notepad or WordPad.


2. Create the three-line prologue to ensure the document references the correct
DTD and XSL stylesheet.
3. Review the DTD to determine the required structure of the document. The
opening tag of the root element must be placed at the beginning of the
document. In the apartment example, Directory is the root element.
4. Following the root element, all of the elements and their children must be
incorporated. These elements can be nested within each other, as seen in the
apartment example where the City element contains the Apartment element. In
turn, the Apartment element contains the elements comprising apartment_info
and the Unit element, which contains the elements comprising unit_info. To
replicate this structure within the XML document, the opening tags for the City
and Apartment elements are included first.
5. Once all of the apartments and their associated units have been created, the
closing tags for the City and Directory elements must be included at the end of
the document.
6. The XML document should be saved and placed into a Web accessible directory.
The directory should also contain the DTD and XSL stylesheets referenced by
the document

http://www.saif4u.webs.com
6

XML Style Sheets

Displaying XML with CSS


It is possible to use CSS to format an XML document.

Below is an example of how to use a CSS style sheet to format an XML document:

Take a look at this XML file: The CD catalog

Then look at this style sheet: The CSS file

Finally, view: The CD catalog formatted with the CSS file

Displaying XML with XSLT


XSLT is the recommended style sheet language of XML.

XSLT (eXtensible Stylesheet Language Transformations) is far more sophisticated than


CSS.

XSLT can be used to transform XML into HTML, before it is displayed by a browser:

Display XML with XSLT

XML XSLT
<?xml version="1.0" encoding="ISO-8859-1"?> <?xml version="1.0"
<!-- Edited by XMLSpy® --> encoding="ISO-8859-1"?>
<breakfast_menu> <!-- Edited by XMLSpy® -->
<food> <html xsl:version="1.0"
<name>Belgian Waffles</name> xmlns:xsl="http://www.w3.org/1
<price>$5.95</price> 999/XSL/Transform"
<description>two of our famous Belgian Waffles with plenty xmlns="http://www.w3.org/1999
of real maple syrup</description> /xhtml">
<calories>650</calories> <body style="font-
</food> family:Arial;font-
<food> size:12pt;background-
<name>Strawberry Belgian Waffles</name> color:#EEEEEE">
<price>$7.95</price> <xsl:for-each
<description>light Belgian waffles covered with strawberries select="breakfast_menu/food">
and whipped cream</description> <div style="background-
<calories>900</calories> color:teal;color:white;padding:4p
</food> x">
<food> <span style="font-

http://www.saif4u.webs.com
7

<name>Berry-Berry Belgian Waffles</name> weight:bold"><xsl:value-of


<price>$8.95</price> select="name"/></span>
<description>light Belgian waffles covered with an - <xsl:value-of select="price"/>
assortment of fresh berries and whipped cream</description> </div>
<calories>900</calories> <div style="margin-
</food> left:20px;margin-
<food> bottom:1em;font-size:10pt">
<name>French Toast</name> <xsl:value-of
<price>$4.50</price> select="description"/>
<description>thick slices made from our homemade <span style="font-style:italic">
sourdough bread</description> <xsl:value-of select="calories"/>
<calories>600</calories> (calories per serving)
</food> </span>
<food> </div>
<name>Homestyle Breakfast</name> </xsl:for-each>
<price>$6.95</price> </body>
<description>two eggs, bacon or sausage, toast, and our </html>
ever-popular hash browns</description>
<calories>950</calories>
</food>
</breakfast_menu>

OUTPUT

http://www.saif4u.webs.com
8

Hyperlinks in XML Documents


XLink defines a standard way of creating hyperlinks in XML documents.

XPointer allows the hyperlinks to point to more specific parts in the XML document.

What is XLink?
• XLink is short for XML Linking Language
• XLink is used to create hyperlinks in XML documents
• Any element in an XML document can behave as a link
• XLink supports simple links (like HTML) and extended
links (for linking multiple resources together)
• With XLink, the links can be defined outside the linked
files
• XLink is a W3C Recommendation

XLink Syntax
In HTML, we know (and all the browsers know!) that the <a> element defines a
hyperlink. However, this is not how it works with XML. In XML documents, you can use
whatever element names you want - therefore it is impossible for browsers to predict
what hyperlink elements will be called in XML documents.

The solution for creating links in XML documents was to put a marker on elements that
should act as hyperlinks.

Below is a simple example of how to use XLink to create links in an XML document:

<?xml version="1.0"?>

<homepages xmlns:xlink="http://www.w3.org/1999/xlink">

<homepage xlink:type="simple"
xlink:href="http://www.w3schools.com">Visit W3Schools</homepage>

<homepage xlink:type="simple"
xlink:href="http://www.w3.org">Visit W3C</homepage>

</homepages>

To get access to the XLink attributes and features we must declare the XLink
namespace at the top of the document.

The XLink namespace is: "http://www.w3.org/1999/xlink".

http://www.saif4u.webs.com
9

The xlink:type and the xlink:href attributes in the <homepage> elements define that the
type and href attributes come from the xlink namespace.

The xlink:type="simple" creates a simple, two-ended link (means "click from here to go
there"). We will look at multi-ended (multidirectional) links later.

The XML Example Document


Look at the following XML document, "bookstore.xml", that represents a few books:

<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore xmlns:xlink="http://www.w3.org/1999/xlink">

<book title="Harry Potter">


<description
xlink:type="simple"
xlink:href="http://book.com/images/HPotter.gif"
xlink:show="new">
As his fifth year at Hogwarts School of Witchcraft and
Wizardry approaches, 15-year-old Harry Potter is.......
</description>
</book>

<book title="XQuery Kick Start">


<description
xlink:type="simple"
xlink:href="http://book.com/images/XQuery.gif"
xlink:show="new">
XQuery Kick Start delivers a concise introduction
to the XQuery standard.......
</description>
</book>

</bookstore>

http://www.saif4u.webs.com
10

XML Document Object Model

The Document Object Model (DOM) is a cross-platform and language-independent


convention for representing and interacting
with objects in HTML, XHTML and XML documents.[1] Aspects of the DOM (such as its
"Elements") may be addressed and manipulated within the syntax of the programming
language in use. The public interface of a DOM is specified in its application
programming interface (API).

What is the DOM?


The DOM is a W3C (World Wide Web Consortium) standard.

The DOM defines a standard for accessing documents like XML and HTML:

"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 DOM is separated into 3 different parts / levels:

• Core DOM - standard model for any structured document


• XML DOM - standard model for XML documents
• HTML DOM - standard model for HTML documents

The DOM defines the objects and properties of all document elements, and
the methods (interface) to access them.

http://www.saif4u.webs.com
11

What is the HTML DOM? 


The HTML DOM defines the objects and properties of all HTML elements, and
the methods(interface) to access them.

What is the XML DOM? 


The XML DOM is:

• A standard object model for XML


• A standard programming interface for XML
• Platform- and language-independent
• A W3C standard

The XML DOM defines the objects and properties of all XML elements, and
the methods (interface) to access them.

In other words: The XML DOM is a standard for how to get, change, add, or delete
XML elements.

http://www.saif4u.webs.com
12

XML Query Language


XQuery is a query and functional programming language that is designed to query
collections of XML data.

XQuery 1.0 was developed by the XML Query working group of the W3C. The work was
closely coordinated with the development of XSLT 2.0 by the XSL Working Group; the
two groups shared responsibility for XPath 2.0, which is a subset of XQuery 1.0. XQuery
1.0 became a W3C Recommendation on January 23, 2007.

"The mission of the XML Query project is to provide flexible query facilities to extract
data from real and virtual documents on the World Wide Web, therefore finally providing
the needed interaction between the Web world and the database world. Ultimately,
collections of XML files will be accessed like databases".[1]

What is XQuery?

• 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 is About Querying XML 


XQuery is a language for finding and extracting elements and attributes from XML
documents.

Here is an example of a question that XQuery could solve:

"Select all CD records with a price less than $10 from the CD collection stored in the
XML document called cd_catalog.xml"

XQuery and XPath 


XQuery 1.0 and XPath 2.0 share the same data model and support the same functions
and operators. If you have already studied XPath you will have no problems with
understanding XQuery.

http://www.saif4u.webs.com
13

XQuery - Examples of Use 


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 Basic Syntax Rules


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 :)

XQuery Conditional Expressions 


"If-Then-Else" expressions are allowed in XQuery.

Look at the following example:

for $x in doc("books.xml")/bookstore/book
return if ($x/@category="CHILDREN")
then <child>{data($x/title)}</child>
else <adult>{data($x/title)}</adult>

Notes on the "if-then-else" syntax: parentheses around the if expression are


required. else is required, but it can be just else ().

The result of the example above will be:

<adult>Everyday Italian</adult>
<child>Harry Potter</child>
<adult>Learning XML</adult>
<adult>XQuery Kick Start</adult>

http://www.saif4u.webs.com
14

XQuery Comparisons 
In XQuery there are two ways of comparing values.

1. General comparisons: =, !=, <, <=, >, >=

2. Value comparisons: eq, ne, lt, le, gt, ge

The difference between the two comparison methods are shown below.

The following expression returns true if any q attributes have a value greater than 10:

$bookstore//book/@q > 10

The following expression returns true if there is only one q attribute returned by the
expression, and its value is greater than 10. If more than one q is returned, an error
occurs:

$bookstore//book/@q gt 10

Examples
The sample XQuery code below lists the unique speakers in each act of Shakespeare's
play Hamlet, encoded in hamlet.xml
<html><head/><body>
{
for $act in doc("hamlet.xml")//ACT
let $speakers := distinct-values($act//SPEAKER)
return
<div>
<h1>{ string($act/TITLE) }</h1>
<ul>
{
for $speaker in $speakers
return <li>{ $speaker }</li>
}
</ul>
</div>
}
</body></html>

http://www.saif4u.webs.com

Vous aimerez peut-être aussi