SOA - WS-Topic 3-II

Download as pdf or txt
Download as pdf or txt
You are on page 1of 65

SERVICE ORIENTED ARCHITECTURE

& WEB SERVICES


ITC 3013

Dr.Chamara
Dept. of ICT, Faculty of Technology
University of Sri Jayewardenepura

SOA DATA MODEL TOPIC 3-II


XML DECLARATION NODE
<?xml version="1.0" encoding="UTF-8"?>
This is not a processing instruction
XML declaration statement
Its purpose is to configure the XML parser correctly before it starts
reading the rest of the document.
It is not necessary for "valid" XML. "Valid" means "represents a well-
defined document type, as described in a DTD or a schema". Without
a schema or DTD the word "valid" has no meaning.
A well-formed XML document is one that obeys the basic syntax rules
of XML.
XML DECLARATION NODE
doc4.xml

doc4.xml is well-formed
But not valid
Error at line 2, column 6: no declaration found for element 'foo'

doc4.xml is well-formed & valid


PCDATA VS CDATA
PCDATA
 PCDATA means parsed character data.

CDATA
 Character Data
 All text in an XML document will be parsed by the parser, but
CDATA text will be ignored by the parser. Tags inside the text will
NOT be treated as markup.
INTERNAL DTD DECLARATION
EXTERNAL DTD DECLARATION
The file “doc3.dtd" which contains the DTD:

doc3.dtd
EMPTY ELEMENTS
ELEMENTS WITH ANY CONTENTS
EXAMPLE
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE bibliography [
<!ELEMENT bibliography (book+)>
<!ELEMENT book (title, author*, publisher?, year?, section*)>
<!ATTLIST book ISBN CDATA #REQUIRED>
<!ATTLIST book price CDATA #IMPLIED>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT publisher (#PCDATA)>
<!ELEMENT year (#PCDATA)>
<!ELEMENT i (#PCDATA)>
<!ELEMENT content (#PCDATA|i)*>
<!ELEMENT section (title, content?, section*)>
]>
<bibliography>
<book ISBN="gg">
<title>'Home'</title>
<author>'Disanayeke'</author>
<publisher>'SJP'</publisher>
</book>

<book ISBN="gk">
<title>'Campus'</title>
<author>'Kumara'</author>

</book>

</bibliography>
ENTITIES
Entities are variables used to define shortcuts to standard
text or special characters.
 Entity references are references to entities
 Entities can be declared internal or external
INTERNAL ENTITY DECLARATION
Syntax:

<!ENTITY entity-name "entity-value">

DTD Example:

<!ENTITY writer "Donald Duck.">


<!ENTITY copyright "Copyright Disney.">

XML Example:

<author>&writer;&copyright;</author>
EXTERNAL ENTITY DECLARATION
Syntax:
<!ENTITY entity-name SYSTEM "URI/URL">

DTD Example:
<!ENTITY writer SYSTEM
"https://www.disney.com/entities.dtd">
<!ENTITY copyright SYSTEM
"https://www.disney.com/entities.dtd">

XML Example:
<author>&writer;&copyright;</author>
QUESTION: WRITE A SAMPLE XML FOR
THE FOLLOWING DTD.

Parsed Character Data –PCDATA , XML parsers are used to


parse all the text in an XML.
CDATA- In XML it is basically a block of texts or sentences
that are not parsed by the parser and are treated as
regular English text.
XML SCHEMA
XML Schema is an XML-based (and more powerful)
alternative to DTD.
The purpose of an XML Schema is to define the legal
building blocks of an XML document, just like a DTD.
The XML Schema language is also referred to as XML
Schema Definition (XSD).
DTD- Document Type Definition (from previous section)
DTD stands for Document Type Definition and it is a document which defines
the structure of an XML document. It is used to describe the attributes of XML
language precisely. It can be classified into two types namely internal DTD
and external 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
XML SCHEMA OVER DTD
XML Schemas are extensible to future additions
XML Schemas are richer and more powerful than DTDs
XML Schema has a wealth of derived and built-in data
types that are not available in DTD.
XML Schema is strongly typed, while DTD is not
(includes the various properties )
XML Schemas are written in XML
XML Schemas support data types.
XML Schemas support namespaces.
XML SCHEMA: WRITTEN IN XML
You don't have to learn a new language
You can use your XML editor to edit your Schema files
You can use your XML parser to parse your Schema files
You can manipulate your Schema with the XML DOM
You can transform your Schema with XSLT

The Document Object Model (DOM) is the foundation of


XML. XML documents have a hierarchy of informational
units called nodes; DOM is a way of describing those
nodes and the relationships between them.
What is already learnt? It is well-formed but not valid

It is well-formed and valid

Two options - Internal DTD & External DTD

What is next ? XSD


XML SCHEMA: SUPPORTS DATA TYPES
It is easier to describe allowable document content
It is easier to validate the correctness of data
It is easier to work with data from a database
It is easier to define data facets (restrictions on data)
It is easier to define data patterns (data formats)
It is easier to convert data between different data types
XML SCHEMA
The <schema> element
The <schema> element is the root element of every XML
Schema.
XML SCHEMA
The <schema> element may contain some attributes. A
schema declaration often looks something like this:
XML SCHEMA
The following part:

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:
XML SCHEMA
The following part:

indicates that any elements used by the XML instance


document which were declared in this schema must be
namespace qualified.
XML FILE
student.xml
XML SCHEMA
student.xsd
XML FILE: REFERENCE TO THE XML
SCHEMA
student.xml
XSD SIMPLE ELEMENTS
A simple element is an XML element that contains only text.
It cannot contain any other elements or attributes. The
syntax for defining a simple element is:

XML Schema has a lot of built-in data types. The most


common types are:
 xs:string
 xs:decimal
 xs:integer
 xs:boolean
 xs:date
 xs:time
XSD SIMPLE ELEMENTS: EXAMPLE
Some XML elements:

<lastname>Silva</lastname>
<age>36</age>
<dateborn>1970-03-27</dateborn>

The corresponding simple element definitions:

<xs:element name="lastname" type="xs:string"/>


<xs:element name="age" type="xs:integer"/>
<xs:element name="dateborn" type="xs:date"/>
XML SIMPLE ELEMENTS: DEFAULT AND
FIXED VALUES
Simple elements may have a default value OR a fixed
value specified. A default value is automatically assigned
to the element when no other value is specified. In the
following example the default value is "red":

<xs:element name="color" type="xs:string" default="red"/>

A fixed value is also automatically assigned to the element,


and you cannot specify another value. In the following
example the fixed value is "red":

<xs:element name="color" type="xs:string" fixed="red"/>


XSD ATTRIBUTES
All attributes are declared as simple types. Simple elements
cannot have attributes. If an element has attributes, it is
considered to be of a complex type. But the attribute itself is
always declared as a simple type. The syntax for defining an
attribute is:
<xs:attribute name="xxx" type="yyy"/>

An XML element with an attribute:

<lastname lang="EN">Peter Parker</lastname>

Corresponding attribute definition:


<xs:attribute name="lang" type="xs:string"/>
XSD ATTRIBUTES
Attributes may have a default or fixed value specified. A
default value is automatically assigned to the attribute when no
other value is given.
<xs:attribute name="lang" type="xs:string" default="EN"/>

A fixed value automatically assigns a value to the attribute


and you cannot specify another value.
<xs:attribute name="lang" type="xs:string" fixed="EN"/>

By default, attributes are optional. To state that an attribute is


required, the “use” attribute can be used.
<xs:attribute name="lang" type="xs:string" use="required"/>
EXAMPLE: XML FILE
customers.xml
EXAMPLE: XML SCHEMA
customers.xsd
Example-2
doc8.xml doc8.xsd
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<addressBook <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" elementFormDefault="qualified">
<xs:element name="addressBook">
xsi:noNamespaceSchemaLocation="doc8.xsd"> <xs:complexType>
<friend firstName="nimal" lastName="gurusinghe" <xs:sequence>
age="39"> <xs:element ref="friend" maxOccurs="unbounded"/>
<address> 24, 2nd Lane, Col 7 </address> </xs:sequence>
<gender>male</gender> </xs:complexType>
</xs:element>
</friend>
<xs:element name="friend">
<friend firstName="ann" lastName="perera" age="29"> <xs:complexType>
<address>123, Galle Rd, Dehiwala</address> <xs:sequence>
<gender>female</gender> <xs:element name="address" type="xs:string"/>
</friend> <xs:element name="gender" type="xs:string"/>
<friend firstName="kamal" lastName="weerakkody" </xs:sequence>
<xs:attribute name="age" type="xs:string" use="required"/>
age="25">
<xs:attribute name="firstName" type="xs:string" use="required"/>
<address>Galle</address> <xs:attribute name="lastName" type="xs:string" use="required"/>
<gender>male</gender> </xs:complexType>
</friend> </xs:element>
</addressBook> </xs:schema>
Out put
XSD RESTRICTIONS/FACETS
Restrictions are used to define acceptable values for XML
elements. Restrictions on XML elements are called facets.

The example above defines an element called “age” with a


restriction. The value of the element cannot be lower than 0
and greater than 120. This is an example of restrictions on
values.
XSD RESTRICTIONS/FACETS:
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 above defines an element called "car" with a


restriction. The only acceptable values are: Audi, Golf, BMW.
XSD RESTRICTIONS/FACETS:
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 above defines an element called "letter" with a


restriction. The only acceptable value is one of the lowercase
letters from a to z.
XSD RESTRICTIONS/FACETS:
RESTRICTIONS ON A SERIES OF VALUES
Examples:

Pattern Value Description


[A-Z][A-Z][A-Z] Three of uppercase letters from a to z.

[a-zA-Z][a-zA-Z][a-zA-Z] Three of the lowercase or uppercase letters


from a to z.
[xyz] One of the letters x, y or z.

[0-9][0-9][0-9][0-9] Five digits in a sequence.


XSD RESTRICTIONS/FACETS:
RESTRICTIONS ON A SERIES OF VALUES
Examples:
Pattern Value Description
([a-z])* Zero or more occurrences of lowercase letters
from a to z.
([a-z][A-Z])+ One or more pairs of letters, each having a
lower case letter followed by an uppercase
letter.
male|female male or female

[a-zA-Z0-9]{8} Exactly eight characters in a row, those must be


lowercase/uppercase letters from a to z, or a
number from 0 to 9.
XSD RESTRICTIONS/FACETS:
RESTRICTIONS ON LENGTH
To limit the length of a value in an element, the length,
maxLength, and minLength constraints can be used.
XSD RESTRICTIONS/FACETS
COMPLEX ELEMENTS
A complex element is an XML element that contains other
elements and/or attributes. There are four kinds of complex
elements.
 empty elements
 elements that contain only other elements
 elements that contain only text
 elements that contain both other elements and text
COMPLEX ELEMENT: EXAMPLES
An empty complex XML element:
<customer cid="1345"/>

A complex XML element which only contains other elements:


<student>
<firstname>Peter</firstname>
<lastname>Parker</lastname>
</student>

A complex XML element which contains both elements and text:


<note>
The date was: <date lang="EN">2020-05-02</date> ....
</note>
DEFINING COMPLEX ELEMENTS
Following is a complex XML element that has only other
elements:

It can be defined like this:


DEFINING COMPLEX ELEMENTS
In the previous example:
 Only the student element can use the specified complex type.
 The “sequence” indicator specifies that the child elements must
appear in the given order
DEFINING COMPLEX ELEMENTS
The previous example can also be defined like this:

In this case, several elements can refer to the same complex


type.
DEFINING COMPLEX ELEMENTS
In the previous method was used for definition, several elements
can refer to the same complex type.
XSD INDICATORS
Indicators can be used to control how elements are used in
documents. Following are some useful XSD indicators:
Order indicators
 All indicator
 Choice indicator
 Sequence indicator

Occurrence indicators
 maxOccurs indicator
 minOccurs indicator
ORDER INDICATORS
Order indicators are used to define the order of the
elements.
<xs:element name="student">
<xs:complexType>
<order_indicator>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</order_indicator>
</xs:complexType>
</xs:element>

Replace order_indicator with an appropriate order


indicator according to the requirement. (See next slide…)
ORDER INDICATORS
In the previous example, replace order_indicator with the
following order indicators:
▪ xs:all – All – specifies child elements can appear in any order and
each child must occur once.
▪ xs:choice – Sequence – specifies either once child element or the
other can occur.
▪ xs:sequence – Choice – specifies that child elements must appear in
a specific order.
OCCURRENCE INDICATORS
Occurrence indicators are used to define how often an
element can occur. For all order indicators the values for
minOccurs and maxOccurs is 1 by default.
▪ minOccurs – specifies the minimum number of times and element can
occur.
▪ maxOccurs – specifies the maximum number of times an element can
occur. (for unlimited number of occurrences set maxOccurs=
‘unbounded’)
OCCURRENCE INDICATORS: EXAMPLES
sname element can occur a minimum of one time and a
maximum of ten times.

<xs:element name="sname" type="xs:string" maxOccurs="10"/>

sname element can occur a minimum of zero times and a


maximum of ten times.

<xs:element name="dname" type="xs:string"


maxOccurs="10" minOccurs="0"/>
XML Namespaces

XML Namespaces provide a method to avoid element


name conflicts.

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.
This XML carries HTML table information

<table>
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>

This XML carries information about a table

<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 content and meaning.
A user or an XML application will not know how to handle
these differences.

XML Namespaces - The xmlns Attribute

When using prefixes in XML, a so-called namespace for the


prefix must be defined.
The namespace is defined by the xmlns attribute in the start
tag of an element.
The namespace declaration has the following syntax.
xmlns:prefix="URI".
Solution : Name Conflicts
<root>
<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>
In the example above, the xmlns attribute in the <table> tag give the h: and
f: prefixes a qualified namespace.
When a namespace is defined for an element, all child elements with the
same prefix are associated with the same namespace.
XML Namespaces

Implicit declaration (Default Explict declaration


NameSpaces)

If an XML namespaces declare If an XML namespaces declare


doesn’t contain a prefex contains a prefex

<a xmlns=””> <foo:a xmlns=””>


<b>GNU</b> <foo:b>GNU</foo:b>
</a> </foo:a>
XPath

XPath is used to navigate through elements and attributes in


XML documents.

•XPath is a syntax for defining parts of an XML document


•XPath uses path expressions to navigate in XML documents
•XPath contains a library of standard functions
•XPath is a major element in XSLT
•XPath is a W3C recommendation
•XPath (XML Path Language) is a language used to point to (or
select) parts of XML documents.
•XPath is not a complete language, but a syntax to be used by other
languages.
•XPath was designed by W3C, as a major element in the W3C
standards, XSLT, XQuery, XLink, and XPointer.
•Today XPath can also be used in JavaScript and Java, XML Schema,
PHP, Python, C and C++, and lots of other languages.
XLink
XLink defines a standard way of creating hyperlinks in XML
documents.

•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" encoding="UTF-8"?>

<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>
XPointer

•XPointer is short for XML Pointer Language


•XPointer allows the links to point to specific parts of an XML
document
•XPointer uses XPath expressions to navigate in the XML
document
•XPointer is a W3C Recommendation
XPointer Syntax

In HTML, we can create a hyperlink that either points to an HTML


page or to a bookmark inside an HTML page (using #).
Sometimes it is more useful to point to more specific content. For
example, let's say that we want to link to the third item in a particular
list, or to the second sentence of the fifth paragraph. This is easy
with XPointer.

If the hyperlink points to an XML document, we can add an XPointer


part after the URL in the xlink:href attribute, to navigate (with an
XPath expression) to a specific place in the document.
For example, in the example below we use XPointer to point to the
fifth item in a list with a unique id of "rock":

href="http://www.example.com/cdlist.xml#id('rock').child(5,item)"
QUESTIONS

Task 1: Write down internal DTD, external DTD and XML schema

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

<addressBook>

<friend firstName="nimal" lastName="gurusinghe" age="39">

<address> 24, 2nd Lane, Col 7 </address>

<gender>male</gender>

</friend>

<friend firstName="ann" lastName="perera" age="29">

<address>123, Galle Rd, Dehiwala</address>

<gender>female</gender>

</friend>

<friend firstName="kamal" lastName="weerakkody" age="25">

<address>Galle</address>

<gender>male</gender>

</friend>

</addressBook>

* Students can try 1-4 questions Lab-sheet 02


Q&A

You might also like