Introduction To XML 110130061644 Phpapp02
Introduction To XML 110130061644 Phpapp02
<?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)
<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:
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:
/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
<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 ?>
<stylesheet >
<import href="tables.xsl"/>
<import href="features.xsl">
<template > </template>
</stylesheet>
The <xsl:value-of> Element
...
<xsl:value-of select="catalog/cd/title"/>
<xsl:value-of select="catalog/cd/artist"/>
...
<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>
<xsl:template match="title">
<xsl:call-template name="CreateHeader" />
</xsl:template>
<xsl:template match="head">
<xsl:call-template name=CreateHeader" />
</xsl:template>
Attribute Values
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.