0% found this document useful (0 votes)
58 views56 pages

Introduction To XML 110130061644 Phpapp02

The document discusses XML (eXtensible Markup Language), including what it is, its uses, and related technologies. Specifically, it defines XML as a universal language for describing structured data using tags and attributes. It also discusses XML's advantages like being platform-independent, as well as introducing related concepts like XML namespaces, validation via DTDs and XSDs, and the XPath and XSLT languages for querying and transforming XML documents.

Uploaded by

J Mani Vannan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views56 pages

Introduction To XML 110130061644 Phpapp02

The document discusses XML (eXtensible Markup Language), including what it is, its uses, and related technologies. Specifically, it defines XML as a universal language for describing structured data using tags and attributes. It also discusses XML's advantages like being platform-independent, as well as introducing related concepts like XML namespaces, validation via DTDs and XSDs, and the XPath and XSLT languages for querying and transforming XML documents.

Uploaded by

J Mani Vannan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

Introduction

Svetlin Nakov to XML


Bulgarian Association of
Software Developers
www.devbg.org
Contents
1. What is XML?
2. XML and HTML
3. When to use XML?
4. Namespaces
5. Schemes and validation DTD
and XSD schemes
6. XPath Language
7. XSL
8. XSL transformations
What is XML
What is XML?
Universal language (notation) for
describing structured data
The data is stored together with the meta-
information about it
Looks like HTML text based, uses tags
and attributes
It is used to describe other languages
(formats) for data representation
What is XML? (2)
Worldwide-affirmative standard,
supported by the W3C (www.w3c.org)
Platform, programming languages and OS
independent
XML - Example

<?xml version="1.0"?>
<library name=".NET Developer's Library">
<book>
<title>Programming Microsoft .NET</title>
<author>Jeff Prosise</author>
<isbn>0-7356-1376-1</isbn>
</book>
<book>
<title>Microsoft .NET for Programmers</title>
<author>Fergal Grimes</author>
<isbn>1-930110-19-7</isbn>
</book>
</library>
XML Example header
part
(prolog)
<?xml version="1.0"?>
<library name=".NET Developer's Library"> attribute
<book>
<title>Programming Microsoft .NET</title>
opening
tag <author>Jeff Prosise</author>
<isbn>0-7356-1376-1</isbn>
</book>
element
<book>
<title>Microsoft .NET for Programmers</title>
closing tag <author>Fergal Grimes</author>
<isbn>1-930110-19-7</isbn>
</book>
</library> element value
XML and HTML
Likenesses between XML and HTML:
Both are text based
Both use tags and attributes
Differences between XML and HTML:
HTML is a language, and XML is a
syntax for describing other languages
HTML describes the formatting of information,
and XML describes structured information
XML requires the documents to be
well-formed
Well-Formed Documents
XML requires the documents to be well-
formed
Tags should always be closed in the
right order
Attributes should always be closed
The document should contain only one
root element
There are restrictions of the tag and
attribute names
Well-Formed Documents (2)

Example of bad-defined XML document

<xml>
<button bug! value="OK name="b1">
<animation source="demo1.avi"> 1 < 2 < 3
</click-button>
< / xml >
When to Use XML?
Use XML:
For information exchange between
different systems
For storing structured data
To create custom-defined languages for
describing information
Configuration data
XML Disadvantages
XML disadvantages:
Data is more massive (take more space)
Decreased performance
Frequently there is a need of more
memory
Increased network traffic
Namespaces
Namespaces
Namespaces in XML documents add the possibility
to use different tags with the same name:

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


<country:towns xmlns:country="urn:nakov-com:country"
xmlns:town="http://www.nakov.com/town">
<town:town>
<town:name>Sofia</town:name>
<town:population>1 200 000</town:population>
<country:name>Bulgaria</country:name>
</town:town>
<town:town>
<town:name>Plovdiv</town:name>
<town:population>700 000</town:population>
<country:name>Bulgaria</country:name>
</town:town>
</country:towns>
Namespaces - Example
<?xml version="1.0" encoding="UTF-8"?>
<country:towns xmlns:country="urn:nakov-com:country"
xmlns:town="http://www.nakov.com/town">
<town:town>
Namespace with prefix "country" and
<town:name>Sofia</town:name>
URI identifier "urn:nakov-
<town:population>1 200com:country "
000</town:population>
<country:name>Bulgaria</country:name>
</town:town> Tag with name "name" from namespace
<town:town> "country", the full name is "urn:nakov-
com:country:name"
<town:name>Plovdiv</town:name>
<town:population>700 000</town:population>
<country:name>Bulgaria</country:name>
</town:town>
</country:towns>
Namespaces (2)
It is possible to use default namespaces

<?xml version="1.0" encoding="windows-1251"?>


<order xmlns="http://www.hranitelni-stoki.com/orders">
<item>
<name> ""</name>
<ammount>8</ammount>
<measure></measure>
<price>3.76</price>
</item>
<item>
<name></name>
<ammount>12</ammount>
<measure></measure>
<price>4.20</price>
</item>
</order>
XML Schemes and
Validation
Schemes and Validation
The XML documents structure is defined

by schemes
Schemes describes
Possible tags
Possible attributes
Possible values for the attributes and
elements
Order of the tags
Default values
The DTD Language
DTD (Document Type Definition) is:
Formal language for describing XML
document structures
Contains sum of rules for the tags and
their attributes in a document
Text-based language, but not XML
based
Rarely used because it is substituted
by XSD
The DTD Language (2)
DTD Example:

<!ELEMENT library (book+)>


<!ATTLIST library
name CDATA #REQUIRED
>
<!ELEMENT book (title, author, isbn)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT isbn (#PCDATA)>
XSD Schemes
XSD (XML Scheme Definition Language)
is:
Powerful XML-based language for
describing the structure of XML
documents
Contains sum of rules for the tags
and their attributes is a document
XSD schemes have bigger descriptive
power than the DTD
XSD consequently replaces DTD
XSD Scheme Example

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


<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="library">
<xs:complexType>
<xs:sequence>
<xs:element ref="book"
maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="name"
type="xs:string" use="optional"/
>
</xs:complexType>
</xs:element> (the example continues)
XSD Scheme Example (2)
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element ref="title"/>
<xs:element ref="author"/>
<xs:element ref="isbn"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/
>
<xs:element name="isbn" type="xs:string"/>
</xs:schema>
XPath
XPath
XPath (XML Path Language) is a language for
addressing parts of XML documents
XPath expressions contains description of
paths to nodes and criteria, which the nodes
should pass
XPath is frequently used with XSLT and
XPointer
Example for XPath expressions:

/library/book[isbn='1-930110-19-7']

/catalog/cd[@price<10.80]
XPath Expressions
/ addresses the root element
/someNode addresses all nodes with name
"someNode", direct inheritors to the root
/books/book addresses all nodes "book"
, direct inheritors to the node "books"
/books/book[price<"10"]/author
addresses all authors (/books/book/
author), whose books have price < "10"
/items/item[@type="food"] addresses
all nodes with name item, which have attribute
"type" with value "food" and are inheritors to
the "items" node
XSL
XSL for Cross-Media
Publishing

System
PDF as
saves as/
XSLT-Stylesheet digital
converts to
<XML-Document> preprint
XML
Author writes
in text
processing
system using XSLT-Stylesheet
style sheets XSLT-Stylesheet

Catalogue entry
MARC-Format HTML as standard
Library catalogue Internet Format
For Online Publishing
XSL Transformation
Language (XSLT)
What is XSLT?
XSLT stands for XSL Transformations
XSLT transforms an XML document into
another text format document
Uses XPath to navigate in the XML
document
XSLT is a W3C Recommendation
XSL/XSLT Capabilities

Add prefix or suffix text to content


Remove, create, re-order and sort elements
Re-use elements elsewhere in the document
Transform data between XML formats
Specify the XSL formatting objects to be
applied to each element class
Uses a recursive mechanism to go through
document
Browsers Support
Note: Not all browsers have full support
for XML and XSLT
Internet Explorer 6 full support
Mozilla 1.7.8 - is available with an
XSLT implementation
Netscape 8 - based on the Mozilla engine.
Same XML / XSLT support as Mozilla
Opera 8 - supports XML + CSS
Simple XML

First we have a XML document


catalog.xml

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


<catalog>
<cd>
<title>Evergreens</title>
<artist></artist>
<country>USA</country>
<company>USA Music Enterprise</company>
<price>12.00</price>
<year>1980</year>
</cd>
. . .
</catalog>
Create an XSL Style Sheet

Then create an XSL Style Sheet (cdc.xsl)


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

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<xsl:for-each select="catalog/cd">
<br/>
<xsl:value-of select="title"/>
<xsl:value-of select="artist"/>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Link the XSL Style Sheet to the
XML Document
A XML document can be linked
explicitly to a XSL transformation
document
Use processing instruction <?xml-
stylesheet ?>

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


<?xml-stylesheet type="text/xsl"
href="cdc.xsl"?>
<catalog>
...
</catalog>
Output Format Control

Define output as XML, HTML, or text


Can specify the character encoding
Control the XML declaration and DOCTYPE

<xsl:stylesheet xmlns:xsl= . . .>


<xsl:output method="html"
encoding="UTF-8" />
<xsl:template match="/">
<HTML></HTML>
</xsl:template>
</xsl:stylesheet>
XSLT Specification

Available from http://www.w3.org/XSL/


Defines 34 elements and their
attributes
Can build useful style sheets using
stylesheet
template
apply-templates
Depends on the XPath specification
Template Element

Template element specifies


transformation rule
<xsl:template match="/">

</xsl:template>

The match attribute takes XPath


expressions
If more than one XPath matches then
use priority rules to determine which is
used
Import Element

Use import element to include multiple


XSLT files

<stylesheet >
<import href="tables.xsl"/>
<import href="features.xsl">
<template > </template>

</stylesheet>
The <xsl:value-of> Element

Used to extract the value of an XML


element

...
<xsl:value-of select="catalog/cd/title"/>
<xsl:value-of select="catalog/cd/artist"/>
...

Note: The value of the select attribute


is an XPath expression
The <xsl:for-each> Element

Used to select every XML element of a


specified node-set (in a loop)
...
<h2>My CD Collection</h2>
<xsl:for-each select="catalog/cd">
<br/>
<xsl:value-of select="title"/>
<xsl:value-of select="artist"/>
</xsl:for-each>
</body>
...
Note: The value of the select attribute
is an XPath expression
The <xsl:for-each>
Element Filter output
We can also filter the output from the XML
<xsl:for-each select="catalog/cd[artist='Bob Dylan']">

We use expressions in square brackets [


... ] for setting conditions
Legal operator are:

= (equal)

!= (not equal)

&lt; less than

&gt; greater than
XSLT <xsl:sort> Element

Used to sort the output


Add this element inside the
<xsl:for-each> element
<h2>My CD Collection</h2>
<xsl:for-each select="catalog/cd">
<xsl:sort select="artist"/>
<br/>
<xsl:value-of select="title"/>
<xsl:value-of select="artist"/>
</xsl:for-each>
</body>

Note: The select attribute indicates


what XML element to sort on
Sort Element Properties

The 'order' attribute


'ascending' or 'descending'
The 'data-type' attribute
'text' (default) or 'number'
The 'case-order' attribute
'lower-first' or 'upper-first'
<xsl:sort select="product/price"
data-type="number" order="descending"/>
The <xsl:if> Element

Used to put a conditional test against


the content of the XML file
<h2>My CD Collection</h2>
<xsl:for-each select="catalog/cd">
<xsl:if test="price &gt; 10">
<br/>
<xsl:value-of select="title"/>
<xsl:value-of select="artist"/>
</xsl:if>
</xsl:for-each>
</body>

Note: The value of the required test attribute


contains the expression to be evaluated
The <xsl:choose> Element

Used in conjunction with


<xsl:when> and <xsl:otherwise>
to express multiple conditional tests

<xsl:choose>
<xsl:when test="XPath expression">
... some output ...
</xsl:when>
<xsl:otherwise>
... some output ....
</xsl:otherwise>
</xsl:choose>
The <xsl:apply-templates>
Element
Applies a template to the current element or to
the current element's child nodes
Select attribute will process only the child
element that matches the value of the attribute
<xsl:template match="cd">
<p><xsl:apply-templates select="title"/></p>
</xsl:template>

<xsl:template match="title">
Title:
<xsl:value-of select="."/>
<br />
</xsl:template>

Note: The value of the required select attribute


is a XPath expression
Variable Element

Variables may be declared and used in


XSLT
<xsl:variable name="colour">red</xsl:variable>

Variable is referenced with $ notation


<xsl:value-of select="$colour"/>
Reusing Templates
If same formatting is required many times
// A named template called by call-template
<xsl:template name="CreateHeader">
<html:hr/>
<html:h2>***<xsl:apply-templates/>***</html:h2>
<html:hr/>
</xsl:template>
...

<xsl:template match="title">
<xsl:call-template name="CreateHeader" />
</xsl:template>

<xsl:template match="head">
<xsl:call-template name=CreateHeader" />
</xsl:template>
Attribute Values

Use 'value-of' element to access attributes


Attributes identified by '@' prefix
<full-name first="John" second="Smith"/>
-----------------------------------------
<xsl:template match="full-name">
<ajr:person>
<xsl:value-of select="@first">
<xsl:value-of select="@second">
</ajr:person>
<ajr:person name="{@first} {@second}" />
</xsl:template>
-----------------------------------------
<ajr:person>John Smith</ajr:person>
<ajr:person name="John Smith"/>
Creating Elements

Use 'Element' tag to create new elements


Most powerful with use of variables
<xsl:template match="name">
<xsl:element name="{.}">
Nice person!
</xsl:element>
</xsl:template>

Input <name>Gosho</name

<Gosho>
Result Nice person!
</Gosho>
XSLT
Live Demo
Introduction to XML

Questions?
Exercises
1. What does the XML language represents? What
is it used for? When do we use it?
Create an XML document students.xml,
which contains structured description of
students. For each student you should
enter information for his name, sex, birth
date, phone, course, specialty, faculty
number and a list of taken exams (exam name,
tutor, grade).
What does the namespaces represent in the
XML documents? What are they used for?
When do we used them?
Exercises (2)
Add default namespace for the students
"urn:students".
Write an XML file containing orders. Each order
is described by date, customer name and a list
of order items. Each order item consists of
product name, amount and price.
Write an XSL stylesheet to transform the XML
file to a human readable XHTML document.
Sort the products in alphabetical order.
Test the produced XHTML file in your Web
browser.
Exercises (3)
1. Write your CV in XML format. It should have
the following structure:
Personal information (name, DOB, ...)
Education
Skills
Work experience
...
2. Write a XSL stylesheet for transforming the
CV to HTML and XML with other structure.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy