Unit Iv: PHP and XML
Unit Iv: PHP and XML
Unit Iv: PHP and XML
<?php
echo "Hello World!";
?>
</body>
</html>
• Comments in PHP
<?php
// This is a single-line comment
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
?>
• PHP Variables
• Variables are "containers" for storing information.
• In PHP, a variable starts with the $ sign, followed
by the name of the variable.
• Example
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
Unlike other programming languages, PHP has no
command for declaring a variable. It is created the
moment you first assign a value to it.
• Rules for PHP variables:
• A variable starts with the $ sign, followed by
the name of the variable
• A variable name must start with a letter or the
underscore character
• A variable name cannot start with a number
• A variable name can only contain alpha-
numeric characters and underscores (A-z, 0-9,
and _ )
• Variable names are case-sensitive ($age and
$AGE are two different variables)
• Output Variables
• echo statement is often used to output data
to the screen.
<?php
$txt = "W3Schools.com";
echo "I love $txt!";
?>
The following example will produce the same
output as the example above
<?php
$txt = "W3Schools.com";
echo "I love " . $txt . "!";
?>
• The following example will output the sum of two
variables
• <?php
$x = 5;
$y = 4;
echo $x + $y;
?>
• PHP is a Loosely Typed Language
• In the example above, notice that we did not have to
tell PHP which data type the variable is.
• PHP automatically converts the variable to the correct
data type, depending on its value.
• In other languages such as C, C++, and Java, the
programmer must declare the name and type of the
variable before using it.
• In PHP, all keywords (e.g. if, else, while, echo, etc.),
classes, functions, and user-defined functions are NOT
case-sensitive.
• However; all variable names are case-sensitive
• <!DOCTYPE html>
<html>
<body>
<?php
$color = "red";
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?>
</body>
</html>
• PHP Data Types
• PHP supports the following data types:
• String
• Integer
• Float (floating point numbers - also called
double)
• Boolean
• Array
• Object
• NULL
Strings
• Surrounded by single/double quotes
• Escape sequence-\n,\r,\t,\\,\$,\”
• Concatenation using dot operator
• $str3=$str1.$str2;
• Get The Length of a String
<?php
echo strlen("Hello world!"); // outputs 12
?>
• Count The Number of Words in a String
<?php
echo str_word_count("Hello world!");
// outputs 2
?>
Reverse a String
<?php
echo strrev("Hello world!");
// outputs !dlrow olleH
?>
Search For a Specific Text Within a String
<?php
echo strpos("Hello world!", "world");
// outputs 6
?>
• Replace Text Within a String
<?php
echo str_replace("world", "Dolly", "Hello
world!");
// outputs Hello Dolly!
?>
Decision Making
Syntax
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
Ex:
$t = date("H");
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"].
"<br>";
}
• // sql to delete a record
$sql = "DELETE FROM MyGuests WHERE
id=3";
1. comments
2. Processing instructions
3. Document type declaration
Comments in XML
XML example:
<author>&writer; ©right;</author>
• Note: An entity has three parts: an ampersand (&), an entity
name, and a semicolon (;).
3.Document Type Declaration(DTD)
• DTD – Document type definition.
• DTD holds the rules of the grammar for a
particular XML data structure.
• 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.
Document Definitions
• There are different types of document
definitions that can be used with XML:
1. The original Document Type Definition (DTD)
2. XML based-XML Schema
• This note 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>
Internal DTD
<?xml version="1.0"?>
<!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Will meet this weekend</body>
</note>
External DTD: Sample.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "Note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Note.dtd
<!DOCTYPE note
[
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
Xml namespace
• XML Namespaces provide a method to avoid
element name conflicts.
• A namespace is a unique URI (Uniform
Resource Locator)
Name Conflicts
• In XML, element names are defined by the
developer. This often results in a conflict when
trying to mix XML documents from different
XML applications.
Name Conflicts
This XML carries HTML table information:
<table>
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>
This XML carries information about a table (a piece of
furniture):
<table>
<name>African Coffee Table</name>
<width>80</width>
<length>120</length>
</table>
If these XML fragments were added together, there would be a
name conflict. Both contain a <table> element, but the
elements have different values
Namespaces can be declared in the
a.)elements where they are used
or
b.)in the XML root element:
Solving the Name Conflict Using a
<root>
Prefix
<h:table xmlns:h="http://www.w3.org/TR/html4/">
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table xmlns:f="http://www.w3schools.com/furniture">
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table></root>
<root
xmlns:h=“http://www.w3.org/TR/html4/”
xmlns:f="http://www.abc.com/furniture">
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
</root>
XML Schema
• XML Schema is an XML-based alternative to
DTD.
• An XML schema describes the structure of an
XML document.
• The XML Schema language is also referred to
as XML Schema Definition (XSD).
What is an XML Schema?
The purpose of an XML Schema is to define the legal building
blocks of an XML document, just like a DTD.
An XML Schema:
• defines elements that can appear in a document
• defines attributes that can appear in a document
• defines which elements are child elements
• defines the order of child elements
• defines the number of child elements
• defines whether an element is empty or can include text
• defines data types for elements and attributes
• defines default and fixed values for elements and attributes
A Simple XML Document
<?xml version="1.0"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Will meet this weekend!</body>
</note>
A DTD File
<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
An XML Schema
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www. w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element></xs:schema>
• xmlns:xs=http://www.w3.org/2001/XMLSchema-indicates that the
elements and data types used in the schema come from the
"http://www.w3.org/2001/XMLSchema" namespace.
• It also specifies that the elements and data types that come from
the "http://www.w3.org/2001/XMLSchema" namespace should
be prefixed with xs:
• targetNamespace=http://www.abc.com-indicates that the
elements defined by this schema (note, to, from, heading, body.)
come from the "http://www.abc.com" namespace.
• xmlns=http://www.w3schools.com-indicates that the default
namespace is "http://www.abc.com".
• elementFormDefault="qualified“-indicates that elements from the
target namespace must be qualified with the namespace prefix
• The note element is a complex type because it contains other
elements.
• The other elements (to, from, heading, body) are simple types
because they do not contain other elements.
Advantage
XML Schemas will be used in most Web applications
as a replacement for DTDs. Here are some
reasons:
• XML Schemas are extensible to future additions
• XML Schemas are richer and more powerful than
DTDs
• XML Schemas are written in XML
• XML Schemas support data types
• XML Schemas support namespaces
XML parser
• XML parser can handle documents in any way
that their developers choose.
• An XML parser converts an XML document
into an XML DOM object - which can then be
manipulated .
• Two models are commonly used for parser
1. SAX
2.DOM
XML DOM
• A DOM (Document Object Model) defines a standard way for
accessing and manipulating documents.
• The XML DOM views an XML document as a tree-structure.
• All elements can be accessed through the DOM tree. Their
content (text and attributes) can be modified or deleted, and
new elements can be created. The elements, their text, and
their attributes are all known as nodes.
• To extract the text from the “to” element in the XML file
above ("note.xml"), the syntax is:
getElementsByTagName("to")[0].childNodes[0].nodeValue
Load an XML Document
<!DOCTYPE html>
<html><body><script>
if (window.XMLHttpRequest) {
xhttp=new XMLHttpRequest(); }
else { / for IE 5/6
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET","books.xml",false);
xhttp.send();
xmlDoc=xhttp.responseXML;
document.write("XML document loaded into an XML
DOM Object.");</script></body></html>
• Create an XMLHttpRequest object
• Use the open() and send() methods of the
XMLHttpRequest object to send a request to a
server
• Get the response data as XML data
XML DOM Properties
• These are some typical DOM properties:
• x.nodeName - the name of x
• x.nodeValue - the value of x
• x.parentNode - the parent node of x
• x.childNodes - the child nodes of x
• x.attributes - the attributes nodes of x
• Note: In the list above, x is a node object.
XML DOM Methods
<script>
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book")[1]
y=x.childNodes[3];
document.write(y.nodeName);
</script>
</body>
</html> O/P: AUTHOR
SAX Parser
• SAX Stands for Simple API for XML Parsing.
• This is an event based XML Parsing and it parse XML file step
by step so much suitable for large XML Files.
• SAX XML Parser fires event when it encountered opening tag,
element or attribute and the parsing works accordingly.
• It’s recommended to use SAX XML parser for parsing large
xml files in Java because it doesn't require to load whole XML
file in Java and it can read a big XML file in small parts.
• Java provides support for SAX parser and you can parse
any xml file in Java using SAX Parser.
• One disadvantage of using SAX Parser in java is
that reading XML file in Java using SAX Parser requires more
code in comparison of DOM Parser.
Difference between DOM and SAX
DOM SAX
DOM parser loads whole xml document in SAX only loads small part of XML file in
memory memory.
DOM parser is faster than SAX because it slow
access whole XML document in memory.
Reading a large XML file using DOM SAX parser in Java is better suitable for
parser there is more chances that it will large XML file than DOM Parser because it
take a long time or even may not be able doesn't require much memory.
to load it completely simply because it
requires lot of memory to create XML
Dom Tree.
DOM parser works on Document Object SAX is an event based xml parser.
Model
Presenting xml
Presentations of XML documents using XSL style
sheets.
Stylesheets are introduced as the presentation
layer in the separation of content, structure and
presentation.
With the help of Stylesheets, XML documents
can be presented specific to the application and
publishing medium, effectively and in varying
forms.
• XSL stands for Extensible Stylesheet Language.
• XSL describes how the XML document should
be displayed.
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 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.
• XSLT transforms an XML source-tree into an
XML result-tree.
Correct Style Sheet FOR XSL Declaration
The root element that declares the document to
be an XSL style sheet is <xsl:stylesheet> or
<xsl:transform>.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Tran
sform">
or
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Tran
sform">
• To get access to the XSLT elements, attributes
and features we must declare the XSLT
namespace at the top of the document.
• The
xmlns:xsl="http://www.w3.org/1999/XSL/Tran
sform" points to the official W3C XSLT
namespace.
• If you use this namespace, you must also
include the attribute version="1.0".
cdcatalog.xml
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
.
.
</catalog>
Create an XSL Style Sheet-
cdcatalog.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0“
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Link the XSL Style Sheet to the XML
Document
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<catalog> <cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
.
.
</catalog>
XSLT <xsl:template> Element
• An XSL style sheet consists of one or more set
of rules that are called templates.
• A template contains rules to apply when a
specified node is matched.
• The match attribute is used to associate a
template with an XML element.
<xsl:template> 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).
• Eg: <xsl:template match="/">
Example:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl=http://www.w3.org/1999/XSL/Transform>
<xsl:template match="/">
<html> <body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<tr>
<td>…</td>
<td>..</td>
</tr>
</table> </body> </html>
</xsl:template></xsl:stylesheet>
• XSL style sheet is an XML document, it always
begins with the XML declaration: <?xml
version="1.0" encoding="UTF-8"?>.
• The next element, <xsl:stylesheet>, defines
that this document is an XSLT style sheet
document
• The <xsl:template> element defines a
template. The match="/" attribute associates
the template with the root of the XML source
document.
• The content inside the <xsl:template> element
defines some HTML to write to the output.
<xsl:value-of>
• 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.
Example
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html> <body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<tr>
<td><xsl:value-of select="catalog/cd/title"/></td>
<td><xsl:value-of select="catalog/cd/artist"/></td>
</tr>
</table>
</body> </html>
</xsl:template>
</xsl:stylesheet>
<xsl:for-each>
• The XSL <xsl:for-each> element can be used to
select every XML element of a specified
node-set.
Example:
<xsl:template match="/">
<html> <body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body> </html>
</xsl:template>
<xsl:sort>
• To sort the output, simply add an <xsl:sort>
element inside the <xsl:for-each> element in
the XSL file.
Example:
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<xsl:sort select="artist"/>
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
<xsl:if>
• To put a conditional if test against the content
of the XML file, add an <xsl:if> element to the
XSL document.
Syntax
<xsl:if test="expression">
...some output if the expression is true...
</xsl:if>
Example:
<xsl:for-each select="catalog/cd">
<xsl:if test="price > 10">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
<td><xsl:value-of select="price"/></td>
</tr>
</xsl:if>
</xsl:for-each>
Output the title and artist elements of the CDs that
has a price that is higher than 10.
<xsl:choose>
• The <xsl:choose> element is used in conjunction
with <xsl:when> and <xsl:otherwise> to express
multiple conditional tests.
<xsl:choose>
<xsl:when test="expression">
... some output ...
</xsl:when>
<xsl:otherwise>
... some output ....
</xsl:otherwise>
</xsl:choose>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<xsl:choose>
<xsl:when test="price > 10">
<td bgcolor="#ff00ff">
<xsl:value-of select="artist"/></td>
</xsl:when>
<xsl:otherwise>
<td><xsl:value-of select="artist"/></td>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>
add a pink background-color to the "Artist" column WHEN the
price of the CD is higher than 10.
<xsl:apply-templates>
• The <xsl:apply-templates> element applies a
template to the current element or to the
current element's child nodes.
• If we add a select attribute to the <xsl:apply-
templates> element it will process only the
child element that matches the value of the
attribute.
<xsl:template match="title">
<xsl:template match="/">
<html> Title: <span
<body> style="color:Red">
<h2>My CD Collection</h2> <xsl:value-of
<xsl:apply-templates/> select="."/></span>
</body> <br />
</html> </xsl:template>
</xsl:template>
</xsl:attribute>
Example 1
Add a source attribute to the picture element.
<picture>
<xsl:attribute name="source"/>
</picture>
Example 2
Create an attribute-set that can be applied to any output
element.
<xsl:attribute-set name="font">
<xsl:attribute name="fname">Arial</xsl:attribute>
<xsl:attribute name="size">14px</xsl:attribute>
<xsl:attribute name="color">red</xsl:attribute>
</xsl:attribute-set>
<xsl:copy>
• <xsl:copy-of> element creates a copy of the
current node.
• This element can be used to insert multiple
copies of the same node into different places
in the output.
Example 1
Copy the message node to the output document.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:template match="message">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<xsl:comment>
• A comment is added to the output document.
• <xsl:comment>
</xsl:comment>
XML- eXtensible Markup
Language
ch37fhgks73j5mv9d63h5mgfkds8d984
lgnsmcns983
• Impossible to understand for human users
</data>
• Not expressive (no semantics along with
the data)
• Unstructured, read and write only with
Thespecial programs
actual benefit of using XML highly depends on the design of
the application.
Database
with XML
documents
6/27/2019 XML 127
App. Scenario
Buyer
2: Data ExchangeSuppl
ier
XML XML
XML
Adapte Adapte
r (BMECat, ebXML, RosettaNet, r
BizTalk, …)
Legacy
System Legacy
Order
(e.g., System
SAP (e.g.,
R/2) Cobol)
autho titl te
r e xt
number=
abstra secti “1“
Gerha ct on title=“
rd …“
Weiku In order Th inde provide
m … e s…
The x
Web in W
10 eb
years
6/27/2019 XML 141
More on XML Syntax
• Some special characters must be escaped
using entities:
< → <
& → &
(will be converted back when reading the XML
doc)
• Some other characters may be escaped, too:
> → >
“ → "
‘ → '
6/27/2019 XML 142
Well-Formed XML Documents
A well-formed document must adher to, among
others, the following rules:
• Every start tag has a matching end tag.
• Elements may nest, but must not overlap.
• There must be exactly one root element.
• Attribute values must be quoted.
• An element may not have two attributes with
the same name.
• Comments and processing instructions may
not appear inside tags.
6/27/2019 XML 143
Well-Formed XML Documents
A well-formed document must adher to, among
others, the following rules:
• Every start tag has a matching end tag.
Only well-formed
• Elements may nest, but must not overlap.
documents can be
• There must be exactly one root element.
processed
• Attribute values must beby XML
quoted.
• An element may parsers.
not have to attributes with
the same name.
• Comments and processing instructions may
not appear inside tags.
6/27/2019 XML 144
<library> 2.3 Namespaces
<description>Library of the CS
Department</description>
<book bid=“HandMS2000“>
<title>Principles of Data
Mining</title>
<description>
Short introduction to <em>data
mining</em>, useful
for the IRDM course
</description>
</book>
Semantics of the description element is
</library>
ambigous
Content may be defined differently
Renaming may be impossible
Disambiguation of separate (standards!)
XML
applications using unique prefixes
6/27/2019 XML 145
Namespace Syntax
<dbs:book xmlns:dbs=“http://www-dbs/dbs“>
<publications>
<publication type=“journal“ pubid=“Weikum01“>
<author>Gerhard Weikum</author>
<text>In the Web of 2010, XML <cite
cid=„12“/>...</text>
<citation cid=„12“ ref=„XML98“/>
<citation cid=„15“>...</citation>
</publication>
<publication type=“inproceedings“
pubid=“XML98“>
<text>XML, the extended Markup Language,
...</text>
</publication>
6/27/2019 </publications> XML 157
Attribute Examples
<ATTLIST publication type
(journal|inproceedings) #REQUIRED
pubid ID #REQUIRED>
<ATTLIST cite cid IDREF #REQUIRED>
<ATTLIST citation ref IDREF #IMPLIED
cid ID #REQUIRED>
<publications>
<publication type=“journal“ pubid=“Weikum01“>
<author>Gerhard Weikum</author>
<text>In the Web of 2010, XML <cite
cid=„12“/>...</text>
<citation cid=„12“ ref=„XML98“/>
<citation cid=„15“>...</citation>
</publication>
<publication type=“inproceedings“
pubid=“XML98“>
<text>XML, the extended Markup Language,
...</text>
</publication>
6/27/2019 </publications> XML 158
Linking DTD and XML Docs
• Document Type Declaration in the
XML document:
<!DOCTYPE article SYSTEM “http://www-
dbs/article.dtd“>
XML Schema
6/27/2019 XML 161
3.2 XML Schema Basics
• XML Schema is an XML application
• Provides simple types (string, integer, dateTime,
duration, language, …)
• Allows defining possible values for elements
• Allows defining types derived from existing types
• Allows defining complex types
• Allows posing constraints on the occurrence of
elements
• Allows forcing uniqueness and foreign keys
6/27/2019 XML 162
Simplified
<xs:schema> XML Schema
<xs:element name=“article“>
Example
<xs:complexType>
<xs:sequence>
<xs:element name=“author“
type=“xs:string“/>
<xs:element name=“title“
type=“xs:string“/>
<xs:element name=“text“>
<xs:complexType>
<xs:sequence>
<xs:element name=“abstract“
type=“xs:string“/>
<xs:element name=“section“
type=“xs:string“
minOccurs=“0“
maxOccurs=“unbounded“/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
6/27/2019 </xs:schema> XML 163