(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
it8074-soa-uniti-.pdf
1. IT8074-SOA
IT8074 SERVICE ORIENTED ARCHITECTURE
Objectives
To learn fundamentals of XML
To provide an overview of Service Oriented
Architecture and Web services and their
importance
To learn web services standards and technologies
To learn service oriented analysis and design for
developing SOA based applications
2. UNIT I
XML document structure – Well-formed and
valid documents – DTD – XML Schema –
Parsing XML using DOM, SAX – XPath -
XML Transformation and XSL – Xquery
3. 1.0 XML document Structure
• XML documents form a tree structure that
starts at "the root" and branches to "the
leaves".
4. XML Tree Structure
• XML documents are formed as element trees.
• An XML tree starts at a root element and branches from the root to
child elements.
• All elements can have sub elements (child elements):
• <root>
<child>
<subchild>.....</subchild>
</child>
</root>
• The terms parent, child, and sibling are used to describe the
relationships between elements.
• Parent have children. Children have parents. Siblings are children on
the same level (brothers and sisters).
• All elements can have text content (Harry Potter) and attributes
(category="cooking").
5. SYNTAX
• XML uses a much self-describing syntax.
• A prolog defines the XML version and the character encoding:
• <?xml version="1.0" encoding="UTF-8"?>
• The next line is the root element of the document:
• <bookstore>
• The next line starts a <book> element:
• <book category="cooking">
• The <book> elements have 4 child elements: <title>,< author>, <year>, <price>.
• <title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
• The next line ends the book element:
• </book>
• </bookstore>
6. 2.0 DTD(Document Type Definition)
A DTD defines the structure and the legal
elements and attributes of an XML document.
XML document with an 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>Don't forget me this weekend</body>
</note>
7. DTD
• The DTD above is interpreted like this:
• !DOCTYPE note defines that the root element of this
document is note
• !ELEMENT note defines that the note element must
contain four elements: "to,from,heading,body"
• !ELEMENT to defines the to element to be of type
"#PCDATA"
• !ELEMENT from defines the from element to be of type
"#PCDATA"
• !ELEMENT heading defines the heading element to be of
type "#PCDATA"
• !ELEMENT body defines the body element to be of type
"#PCDATA"
8. External DTD
An External DTD Declaration
• If the DTD is declared in an external file, the <!DOCTYPE>
definition must contain a reference to the DTD file:
• XML document with a reference to an external DTD
<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
9. External DTD
note.dtd
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
10. DTD - XML Building Blocks
The Building Blocks of XML Documents
Seen from a DTD point of view, all XML
documents are made up by the following
building blocks:
• Elements
• Attributes
• Entities
• PCDATA
• CDATA
12. DTD-XML BUILDING BLOCKS
PCDATA
• PCDATA means parsed character data.
• Think of character data as the text found between the start tag and the end
tag of an XML element.
• PCDATA is text that WILL be parsed by a parser. The text will be
examined by the parser for entities and markup.
• Tags inside the text will be treated as markup and entities will be expanded.
• However, parsed character data should not contain any &, <, or >
characters; these need to be represented by the & < and >
entities, respectively.
CDATA
• CDATA means character data.
• CDATA is text that will NOT be parsed by a parser. Tags inside the text
will NOT be treated as markup and entities will not be expanded.
13. DTD - Elements
Declaring Elements
• In a DTD, XML elements are declared with the following syntax:
• <!ELEMENT element-name category>
or
<!ELEMENT element-name (element-content)>
Empty Elements
• Empty elements are declared with the category keyword EMPTY:
<!ELEMENT element-name EMPTY>
Example:
<!ELEMENT br EMPTY>
XML example:
<br />
14. DTD - Elements
Elements with Parsed Character Data
• Elements with only parsed character data are declared with #PCDATA inside
parentheses:
• <!ELEMENT element-name (#PCDATA)>
Example:
<!ELEMENT from (#PCDATA)>
Elements with any Contents
• Elements declared with the category keyword ANY, can contain any combination of
parsable data:
• <!ELEMENT element-name ANY>
Example:
<!ELEMENT note ANY>
15. DTD ELEMENTS-? sign
Declaring zero or one occurrences of the same
element example:
The ? sign in the example above declares that the
child element message can occur zero or one
times inside the "note” element
<!ELEMENT element name(child name?)>
Example
<!ELEMENT note(message?)>
16. DTD Elements
Elements with Children (sequences)
• Elements with one or more children are declared with the
name of the children elements inside parentheses:
• <!ELEMENT element-name (child1)>
or
<!ELEMENT element-name (child1,child2,...)>
Example:
<!ELEMENT note (to,from,heading,body)>
• When children are declared in a sequence separated by
commas, the children must appear in the same sequence in
the document
17. DTD Elements
Declaring Minimum One Occurrence of an Element
• <!ELEMENT element-name (child-name+)>
Example:
<!ELEMENT note (message+)>
• The + sign in the example above declares that the child element "message"
must occur one or more times inside the "note" element.
Declaring Zero or More Occurrences of an Element
• <!ELEMENT element-name (child-name*)>
Example:
<!ELEMENT note (message*)>
• The * sign in the example above declares that the child element "message"
can occur zero or more times inside the "note" element.
18. DTD Elements
Declaring either/or Content
• <!ELEMENT note (to,from,header,(message|body))>
• The example above declares that the "note" element must contain a "to"
element, a "from" element, a "header" element, and either a "message"
or a "body" element.
Declaring Mixed Content
• <!ELEMENT note (#PCDATA|to|from|header|message)*>
• The example above declares that the "note" element can contain zero or
more occurrences of parsed character data, "to", "from", "header", or
"message" elements.
19. DTD - Attributes
Declaring Attributes
• An attribute declaration has the following syntax:
• <!ATTLIST element-name attribute-name attribute-
type attribute-value>
DTD example:
<!ATTLIST payment type CDATA "check">
XML example:
<payment type="check" />
20. DTD Attributes
The attribute-type can be one of the following:
Type Description
CDATA The value is character data
(en1|en2|..) The value must be one from an enumerated list
ID The value is a unique id
IDREF The value is the id of another element
IDREFS The value is a list of other ids
NMTOKEN The value is a valid XML name
NMTOKENS The value is a list of valid XML names
ENTITY The value is an entity
ENTITIES The value is a list of entities
NOTATION The value is a name of a notation
21. DTD Attributes
The attribute-value can be one of the following:
Value Explanation
value The default value of the
attribute
#REQUIRED The attribute is required
#IMPLIED The attribute is optional
#FIXED value The attribute value is fixed
22. DTD Attributes
A Default Attribute Value
• DTD:
<!ELEMENT square EMPTY>
<!ATTLIST square width CDATA "0">
Valid XML:
<square width="100" />
• In the example above, the "square" element is defined to be
an empty element with a "width" attribute of type CDATA.
If no width is specified, it has a default value of 0.
23. DTD Attributes
#REQUIRED
Syntax
<!ATTLIST element-name attribute-name attribute-type #REQUIRED>
Example
DTD:
<!ATTLIST person number CDATA #REQUIRED>
Valid XML:
<person number="5677" />
Invalid XML:
<person />
• Use the #REQUIRED keyword if you don't have an option for a default value, but
still want to force the attribute to be present.
24. DTD Attributes
#IMPLIED
Syntax
<!ATTLIST element-name attribute-name attribute-type #IMPLIED>
Example
DTD:
<!ATTLIST contact fax CDATA #IMPLIED>
Valid XML:
<contact fax="555-667788" />
Valid XML:
<contact />
Use the #IMPLIED keyword if you don't want to force the author to include an attribute, and you don't
have an option for a default value.
25. DTD Attributes
#FIXED
Syntax
• <!ATTLIST element-name attribute-name attribute-type #FIXED "value">
• Example
• DTD:
<!ATTLIST sender company CDATA #FIXED "Microsoft">
Valid XML:
<sender company="Microsoft" />
Invalid XML:
<sender company="W3Schools" />
• Use the #FIXED keyword when you want an attribute to have a fixed value without allowing the author to
change it. If an author includes another value, the XML parser will return an error.
Enumerated Attribute Values
Syntax
• <!ATTLIST element-name attribute-name (en1|en2|..) default-value>
• Example
• DTD:
<!ATTLIST payment type (check|cash) "cash">
XML example:
<payment type="check" />
or
<payment type="cash" />
• Use enumerated attribute values when you want the attribute value to be one of a fixed set of legal values.
29. DTD Examples
TV Schedule DTD
<!DOCTYPE TVSCHEDULE [
<!ELEMENT TVSCHEDULE (CHANNEL+)>
<!ELEMENT CHANNEL (BANNER,DAY+)>
<!ELEMENT BANNER (#PCDATA)>
<!ELEMENT DAY (DATE,(HOLIDAY|PROGRAMSLOT+)+)>
<!ELEMENT HOLIDAY (#PCDATA)>
<!ELEMENT DATE (#PCDATA)>
<!ELEMENT PROGRAMSLOT (TIME,TITLE,DESCRIPTION?)>
<!ELEMENT TIME (#PCDATA)>
<!ELEMENT TITLE (#PCDATA)>
<!ELEMENT DESCRIPTION (#PCDATA)>
<!ATTLIST TVSCHEDULE NAME CDATA #REQUIRED>
<!ATTLIST CHANNEL CHAN CDATA #REQUIRED>
<!ATTLIST PROGRAMSLOT VTR CDATA #IMPLIED>
<!ATTLIST TITLE RATING CDATA #IMPLIED>
<!ATTLIST TITLE LANGUAGE CDATA #IMPLIED>
]>
30. 3.0 Well formed and Valid XML
Documents
Well formed XML Document
• The XML specification defines a set of rules that XML documents must
follow in order to be well-formed.
• If an XML document is not well-formed according to the XML
specification, browsers, for example, should not make any attempt at
correcting the errors as they did for HTML documents.
• The following text demonstrates a complete, well-formed XML document:
<greeting>
Hello, World!
</greeting>
II. Valid xml document
If an XML document is well-formed and has an associated Document Type
Declaration (DTD), then it is said to be a valid XML document.
31. Valid and well-formed XML document
with DTD
Let's take an example of well-formed and valid XML document. It follows all the rules of DTD.
employee.xml
<?xml version="1.0"?>
<!DOCTYPE employee SYSTEM "employee.dtd">
<employee>
<firstname>vimal</firstname>
<lastname>jaiswal</lastname>
<email>vimal@javatpoint.com</email>
</employee>
• In the above example, the DOCTYPE declaration refers to an external DTD file. The content
of the file is shown in below paragraph.
employee.dtd
<!ELEMENT employee (firstname,lastname,email)>
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT lastname (#PCDATA)>
<!ELEMENT email (#PCDATA)>
•
32. WELL FORMED XML Document
Well-formed XML Document
• An XML document is said to be well-formed if it adheres to the following rules −
• Non DTD XML files must use the predefined character entities for amp(&), apos(single
quote), gt(>), lt(<), quot(double quote).
• It must follow the ordering of the tag. i.e., the inner tag must be closed before closing the outer tag.
• Each of its opening tags must have a closing tag or it must be a self ending tag.(<title>....</title> or
<title/>).
• It must have only one attribute in a start tag, which needs to be quoted.
• amp(&), apos(single quote), gt(>), lt(<), quot(double quote) entities other than these must be declared.
Example
Following is an example of a well-formed XML document −
<?xml version = "1.0" encoding = "UTF-8" standalone = "yes" ?>
<!DOCTYPE address [ <!ELEMENT address (name,company,phone)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT company (#PCDATA)>
<!ELEMENT phone (#PCDATA)> ]>
XML File
<address>
<name>Tanmay Patil</name>
<company>TutorialsPoint</company>
<phone>(011) 123-4567</phone>
</address>
33. 4.0 XML SCHEMA
• An XML Schema describes the structure of an XML document.
• The XML Schema language is also referred to as XML Schema Definition (XSD).
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<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>
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.
34. XML NAMESPACE
Solving the Name Conflict Using a Prefix
• Name conflicts in XML can easily be avoided using a name prefix.
• This XML carries information about an HTML table, and a piece of
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>
• In the example above, there will be no conflict because the two
<table> elements have different name
35. XML NAMESPACE
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 (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 content and meaning.
• A user or an XML application will not know how to handle these differences.
36. XML NAMESPACE
• XML Namespaces - The xmlns Attribute
• When using prefixes in XML, a namespace
for the prefix must be defined.
• The namespace can be defined by an xmlns
attribute in the start tag of an element.
• The namespace declaration has the following
syntax. xmlns:prefix="URI".
38. XSD - The <schema> Element
The <schema> Element
• The <schema> element is the root element of
every XML Schema:
• <?xml version="1.0"?>
<xs:schema>
...
...
</xs:schema>
39. XSD - The <schema>
Declaration Element
• The <schema> element may contain some
attributes. A schema declaration often looks
something like this:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/
XMLSchema"
targetNamespace="https://www.xxx.com"
xmlns="https://xxx.com"
elementFormDefault="qualified">
...
...
</xs:schema>
40. XSD - The <schema> Element
xmlns:xs="http://www.w3.org/2001/XMLSchem
a"
• 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:
41. XSD - The <schema> Element
This fragment:
targetNamespace="https://www.xxx.com"
• indicates that the elements defined by this
schema (note, to, from, heading, body.) come
from the "https://www.xxx.com" namespace.
42. XSD - The <schema> Element
This fragment:
xmlns="https://www.xxx.com"
• indicates that the default namespace is
"https://www.xxx.com".
This fragment:
elementFormDefault="qualified"
• indicates that any elements used by the XML
instance document which were declared in this
schema must be namespace qualified.
43. XSD Simple Elements
A simple element is an XML element that can
contain only text. It cannot contain any other elements or attributes.
Defining a Simple Element
The syntax for defining a simple element is:
<xs:element name="xxx" type="yyy"/>
• where xxx is the name of the element and yyy is the data type of the
element.
• 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
44. XSD Simple Elements
Example
Here are some XML elements:
<lastname>Refsnes</lastname>
<age>36</age>
<dateborn>1970-03-27</dateborn>
And here are 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"/>
45. Default and Fixed Values for Simple
Elements
• 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"/>
46. XSD Attributes
What is an Attribute?
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.
How to Define an Attribute?
The syntax for defining an attribute is:
<xs:attribute name="xxx" type="yyy"/>
• where xxx is the name of the attribute and yyy specifies the data type of the
attribute.
• 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
47. XSD Attributes
Example
• Here is an XML element with an attribute:
<lastname lang="EN">Smith</lastname>
• And here is the corresponding attribute
definition:
<xs:attribute name="lang" type="xs:string"/>
48. Default and Fixed Values for Attributes
• Attributes may have a default value or a fixed value specified.
• A default value is automatically assigned to the attribute when
no other value is specified.
• In the following example the default value is "EN":
• <xs:attribute name="lang" type="xs:string" default="EN"/>
• A fixed value is also automatically assigned to the attribute,
and you cannot specify another value.
• In the following example the fixed value is "EN":
• <xs:attribute name="lang" type="xs:string" fixed="EN"/>
Optional and Required Attributes
• Attributes are optional by default. To specify that the attribute
is required, use the "use" attribute:
• <xs:attribute name="lang" type="xs:string" use="required"/>
49. XSD Restrictions/Facets
Restrictions are used to define acceptable values for XML
elements or attributes. Restrictions on XML elements
are called facets.
Restrictions on Values
• The following example defines an element called "age"
with a restriction. The value of age cannot be lower
than 0 or greater than 120:
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
50. 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 below defines an element called "car" with a
restriction. The only acceptable values are: Audi, Golf, BMW:
<xs:element name="car">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
51. 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 below defines an element called "letter" with a
restriction. The only acceptable value is ONE of the LOWERCASE
letters from a to z:
• <xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
52. XSD Restrictions/Facets
Other Restrictions on a Series of Values
• The example below defines an element called "letter"
with a restriction. The acceptable value is zero or more
occurrences of lowercase letters from a to z:
• <xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z])*"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
53. XSD Restrictions/Facets
The next example defines an element called "initials" with a restriction.
The only acceptable value is THREE of the UPPERCASE letters from a to
z:
<xs:element name="initials">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z][A-Z][A-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
The next example also defines an element called "initials" with a
restriction. The only acceptable value is THREE of the LOWERCASE OR
UPPERCASE letters from a to z:
<xs:element name="initials">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/>
</xs:restriction>
</xs:simpleType>
54. XSD Restrictions/Facets
The next example defines an element called "gender" with a restriction. The
only acceptable value is male OR female:
<xs:element name="gender">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="male|female"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
The next example defines an element called "password" with a restriction.
There must be exactly eight characters in a row and those characters must
be lowercase or uppercase letters from a to z, or a number from 0 to 9:
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z0-9]{8}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
55. XSD Restrictions/Facets
Restrictions on Whitespace Characters
• To specify how whitespace characters should be handled, we would use the whiteSpace
constraint.
• This example defines an element called "address" with a restriction. The whiteSpace
constraint is set to "preserve", which means that the XML processor WILL NOT remove any
white space characters:
• <xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="preserve"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
• This example also defines an element called "address" with a restriction. The whiteSpace
constraint is set to "replace", which means that the XML processor WILL REPLACE all white
space characters (line feeds, tabs, spaces, and carriage returns) with spaces:
• <xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="replace"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
56. XSD Restrictions/Facets
• This example also defines an element called "address" with
a restriction. The whiteSpace constraint is set to "collapse",
which means that the XML processor WILL REMOVE all
white space characters (line feeds, tabs, spaces, carriage
returns are replaced with spaces, leading and trailing spaces
are removed, and multiple spaces are reduced to a single
space):
• <xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="collapse"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
57. XSD Restrictions/Facets
Restrictions on Length
• To limit the length of a value in an element, we would use the length,
maxLength, and minLength constraints.
• This example defines an element called "password" with a restriction. The
value must be exactly eight characters:
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
• This example defines another element called "password" with a restriction.
The value must be minimum five characters and maximum eight characters:
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="5"/>
<xs:maxLength value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
58. Restrictions for Datatypes
Constraint Description
enumeration Defines a list of acceptable values
fractionDigits Specifies the maximum number of decimal places allowed.
Must be equal to or greater than zero
length Specifies the exact number of characters or list items
allowed. Must be equal to or greater than zero
maxExclusive Specifies the upper bounds for numeric values (the value
must be less than this value)
maxInclusive Specifies the upper bounds for numeric values (the value
must be less than or equal to this value)
maxLength Specifies the maximum number of characters or list items
allowed. Must be equal to or greater than zero
minExclusive Specifies the lower bounds for numeric values (the value
must be greater than this value)
59. Restrictions for Datatypes
minInclusive Specifies the lower bounds for numeric
values (the value must be greater than or
equal to this value)
minLength Specifies the minimum number of
characters or list items allowed. Must be
equal to or greater than zero
pattern Defines the exact sequence of characters
that are acceptable
totalDigits Specifies the exact number of digits
allowed. Must be greater than zero
whiteSpace Specifies how white space (line feeds,
tabs, spaces, and carriage returns) is
handled
60. XSD Complex Elements
A complex element contains other elements and/or attributes.
• We can define a complex element in an XML Schema two
different ways:
1. The "employee" element can be declared directly by naming the
element, like this:
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
If you use the method described above, only the "employee"
element can use the specified complex type. Note that the child
elements, "firstname" and "lastname", are surrounded by the
<sequence> indicator. This means that the child elements must
appear in the same order as they are declared.
61. XSD Complex Elements
2. The "employee" element can have a type attribute that refers to the name of the
complex type to use:
<xs:element name="employee" type="personinfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
If you use the method described above, several elements can refer to the same
complex type, like this:
<xs:element name="employee" type="personinfo"/>
<xs:element name="student" type="personinfo"/>
<xs:element name="member" type="personinfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
62. XSD Indicators
There are seven indicators:
Order indicators:
• All
• Choice
• Sequence
Occurrence indicators:
• maxOccurs
• minOccurs
Group indicators:
• Group name
• attributeGroup name
63. Order Indicators
Order indicators are used to define the order of the
elements.
• All Indicator
• The <all> indicator specifies that the child elements can
appear in any order, and that each child element must
occur only once:
<xs:element name="person">
<xs:complexType>
<xs:all>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:all>
</xs:complexType>
</xs:element>
64. Choice Indicator
• The <choice> indicator specifies that either one
child element or another can occur:
• <xs:element name="person">
<xs:complexType>
<xs:choice>
<xs:element name="employee" type="employee"/>
<xs:element name="member" type="member"/>
</xs:choice>
</xs:complexType>
</xs:element>
65. Sequence Indicator
• The <sequence> indicator specifies that the child
elements must appear in a specific order:
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string
"/>
</xs:sequence>
</xs:complexType>
</xs:element>
66. Occurrence Indicators
Occurrence indicators are used to define how
often an element can occur.
maxOccurs Indicator
The <maxOccurs> indicator specifies the
maximum number of times an element can
occur
minOccurs Indicator
The <minOccurs> indicator specifies the
minimum number of times an element can
occur:
67. Occurrence Indicators
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="full_name" type="xs:string"/>
<xs:element name="child_name" type="xs:string"
maxOccurs="10" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
• The example above indicates that the "child_name"
element can occur a minimum of zero times and a
maximum of ten times in the "person" element.
• Tip: To allow an element to appear an unlimited number
of times, use the maxOccurs="unbounded" statement:
68. Group Indicators
Group indicators are used to define related sets of elements.
<xs:group name="persongroup">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="birthday" type="xs:date"/>
</xs:sequence>
</xs:group>
<xs:element name="person" type="personinfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:group ref="persongroup"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
69. Attribute Groups
• Attribute groups are defined with the attributeGroup declaration,
like this:
• <xs:attributeGroup name="groupname">
...
</xs:attributeGroup>
Example
<xs:attributeGroup name="personattrgroup">
<xs:attribute name="firstname" type="xs:string"/>
<xs:attribute name="lastname" type="xs:string"/>
<xs:attribute name="birthday" type="xs:date"/>
</xs:attributeGroup>
<xs:element name="person">
<xs:complexType>
<xs:attributeGroup ref="personattrgroup"/>
</xs:complexType>
</xs:element>
70. XML SCHEMA EXAMPLE
An XML Document
Let's have a look at this XML document called "shiporder.xml":
<?xml version="1.0" encoding="UTF-8"?>
<shiporder orderid="889923"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="shiporder.xsd">
<orderperson>John Smith</orderperson>
<shipto>
<name>Ola Nordmann</name>
<address>Langgt 23</address>
<city>4000 Stavanger</city>
<country>Norway</country>
</shipto>
<item>
<title>Empire Burlesque</title>
<note>Special Edition</note>
<quantity>1</quantity>
<price>10.90</price>
</item>
<item>
<title>Hide your heart</title>
<quantity>1</quantity>
<price>9.90</price>
</item>
</shiporder>
• The XML document above consists of a root element, "shiporder", that contains a required attribute called "orderid".
The "shiporder" element contains three different child elements: "orderperson", "shipto" and "item". The "item" element
appears twice, and it contains a "title", an optional "note" element, a "quantity", and a "price" element.
• The line above: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" tells the XML parser that this document
should be validated against a schema. The line: xsi:noNamespaceSchemaLocation="shiporder.xsd" specifies WHERE
the schema resides (here it is in the same folder as "shiporder.xml").
72. 5.0 TRANSFORMATIONS AND
XSL
XSL TECHNOLOGIES
• XSL(XML style sheet language) has two independent
languages:
– The XSL Transformation Language(XSLT)
– The XSL Formatting object language(XSL-FO)
• XSLT is used to convert an XML document to another
format. XSL-FO provides a way of describing the
presentation of an XML document.
• Correct Style Sheet Declaration
• The root element that declares the document to be an
XSL style sheet is <xsl:stylesheet> or <xsl:transform>.
73. XSLT
The <xsl:template> Element
• The <xsl:template> element is used to build templates.
• The match attribute is used to associate a template with
an XML 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).
XSLT <xsl:value-of> Element
• 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.
74. XSLT
The <xsl:for-each> Element
• The XSL <xsl:for-each> element can be used to select every
XML element of a specified node-set:
• Filtering the Output
• We can also filter the output from the XML file by adding a
criterion to the select attribute in the <xsl:for-each>
element.
<xsl:for-each select="catalog/cd[artist='Bob Dylan']">
• Legal filter operators are:
• = (equal)
• != (not equal)
• < less than
• > greater than
XSLT <xsl:sort> Element
• The <xsl:sort> element is used to sort the output.
75. XSLT
XSLT <xsl:if> Element
• The <xsl:if> element is used to put a conditional test
against the content of the XML file.
The <xsl:if> Element
• 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>
XSLT <xsl:choose> Element
• The <xsl:choose> element is used in conjunction with
<xsl:when> and <xsl:otherwise> to express multiple
conditional tests.
84. Output
My CD Collection
Title Artist
Empire Burlesque Bob Dylan
Hide your heart Bonnie Tyler
Greatest Hits Dolly Parton
85. XSL FORMATTING
• The World Wide Web Consortium's specification for Extensible
Stylesheet Language (XSL) comes in two parts:
• XSLT, a language for transforming XML documents, and
• XSL Formatting Objects (XSL FO), an XML vocabulary for
specifying formatting semantics.
• XSL Formatting Objects is itself an XML-based markup language
that lets you specify in great detail the pagination, layout, and
styling information that will be applied to your content.
• The XSL FO markup is quite complex. It is also verbose; virtually
the only practical way to produce an XSL FO file is to use XSLT to
produce a source document.
• Finally, once you have this XSL FO file, you need some way to
render it to an output medium. There are few tools available to do
this final step. For these reasons, XML FO has not caught on as
quickly as XSLT.
86. Initialization
• Since the XSL FO file will be an XML document,
it must begin with the standard XML processing
instruction and the FO root element.
• <?xml version="1.0" encoding="utf-8"?>
• <fo:root>
• The structure of the remainder of the document is:
• The layout master set, which consists of
– Descriptions of the kinds of pages that can occur in the
document.
– Sequences in which those page formats can occur.
• The pages and their content.
87. Page Layouts
Document will have three kinds of pages
shown in the diagram below. To accommodate
the stapling area, the cover page and right-
hand pages will have more margin space at the
left. The content pages will also have a region
for a header and footer.
88. Page Layout
The units below are all in centimeters, but you
may use any of the CSS units, such as px
(pixel), pt (point), em, in, mm, etc. Each of
these specifications is called a simple-page-
master and must be given a master-name so
you can refer to it later.
92. The content area
This is the page content area (officially called
the page-reference-area), which can be divided
into five regions as shown below.
93. Content Area-Cover Page
• The cover page doesn't need a header or footer, so we need
only specify information for the region-body by adding the
information shown in bold below.
<fo:simple-page-master master-name="cover"
page-height="12cm"
page-width="12cm"
margin-top="0.5cm"
margin-bottom="0.5cm"
margin-left="1cm"
margin-right="0.5cm">
<fo:region-body
margin-top="3cm" />
</fo:simple-page-master>
94. Content Area-Left Page
The left and right pages will have a header and footer, so we
must specify the extent of the region-before and region-
after.
<fo:simple-page-master master-name="leftPage"
page-height="12cm"
page-width="12cm"
margin-left="0.5cm"
margin-right="1cm"
margin-top="0.5cm"
margin-bottom="0.5cm">
<fo:region-before extent="1cm"/>
<fo:region-after extent="1cm"/>
<fo:region-body
margin-top="1.1cm"
margin-bottom="1.1cm" />
</fo:simple-page-master>
96. Page sequences:<fo:page-sequence>
• A page sequence defines a series of printed pages.
• Each page sequence refers to a page master for its dimensions.
• The page sequence contains the actual content for the document.
• The <fo:page-sequence> element contains <fo:static-content> and <fo:flow>
elements
• The <fo:static-content> element is used for page headers and footers.For example
we can define a header for the company name and page number and this
information will appear on every page.
• The <fo:flow> element contains a collection of text blocks.The <fo:flow> element
is similar to a collection of paragraphs.
• A body of text is defined using the <fo:block> elements. The <fo:block> element is
a child element of <fo:flow>.The <fo:block> element contains free flowing text that
will wrap to the next line in a document if it overflows.
98. Code fragment
<fo:page-sequence master-name=”simple”>
<fo:flow flow-name=”xsl-region-body”>
<!—this defines a level 1 heading with orange
background-->
<fo:block font-size=”18pt”
Font-family=”sans-serif”
Line-height=”24pt”
Background-color=”orange”
Color=”white”
Text-align=”center”
Padding-top=”3pt”>
Ez books online
</fo:block>
<!—paragraph that contains info about the company-->
<fo:block font-size=”12pt”
Font-family=”sans-serif”
Line-height=”24pt”
Text-align=”justify”>
Welcome to Ez books online in the worlds smallest online book store
</fo:block>
</fo:flow>
</fo:page sequence>
99. Page headers and footers
The <fo:static-content> element defines content that should appear on
every page. The <fo:static-content> element is commonly used to set up
page headers and footers. The <fo:static-content> element is a
component of <fo:page-sequence>
The header is defined using the following code fragment
<!—header-->
<fo:static-content flow-name=”xsl-region-before”>
<fo:block text-align=”end”
Font-size=”10pt”
Font-family=”serif”
Line-height=”14pt”>
Ez books catalog-page <fo:page number/>
</fo:block>
</fo:static-content>
The content for the header is placed in xsl-region-before, which is the
top of the page. The <fo:block> element uses the text-align attribute to
place the text at the end of the region. The above example uses the
English language, so the text is right justified. The current page
number is determined using the <fo:page-number> element.
100. Page headers and footers
The footer is defined using the following code
fragment
<!—footer-->
<fo:static-content flow-name=”xsl-region-after”>
<fo:block text-align=”center”
Font-size=”10pt”
Font-family=”serif”
Line-height=”14pt”>
Visit our website http://www.ezbooks.web
</fo:block>
</fo:static-content>
• The footer content is placed at the bottom of the
page in xsl-region-after. A message containing the
company’s web site is listed in the footer.
101. GRAPHICS
• XSL-FO also allows for the insertion of external
graphic images.The graphic formats supported are
dependent on the XSL-FO formatting engine.The
apache-FOP formatting engine supports the popular
graphics formats:GIF,JPEG and BMP.
• The following code fragment inserts the image
smiley.jpg
• <fo:block text-align=”center”>
• <fo:external-graphic
src=”smiley.jpg”width=”200px” height=”200px”/>
• </fo:block>
102. XSL-FO Tables
• The XSL-FO table model is not very different from the HTML table model.
• There are nine XSL-FO objects used to create tables:
• fo:table-and-caption
• fo:table
• fo:table-caption
• fo:table-column
• fo:table-header
• fo:table-footer
• fo:table-body
• fo:table-row
• fo:table-cell
• XSL-FO uses the <fo:table-and-caption> element to define a table. It
contains a <fo:table> and an optional <fo:caption> element.
• The <fo:table> element contains optional <fo:table-column> elements, an
optional <fo:table-header> element, a <fo:table-body> element, and an
optional <fo:table-footer> element. Each of these elements has one or
more <fo:table-row> elements, with one or more <fo:table-cell>
elements:
106. XQUERY
What is XQuery?
• XQuery is to XML what SQL is to databases.
• XQuery is designed to query XML data.
• XQuery is built on XPath expressions
• XQuery is supported by all major databases
• XQuery is a W3C Recommendation
107. XQUERY
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
109. XQUERY Functions
• XQuery uses functions to extract data from XML documents.
The doc() function is used to open the "books.xml" file:
doc("books.xml")
Path Expressions
• XQuery uses path expressions to navigate through elements in
an XML document.
• The following path expression is used to select all the title
elements in the "books.xml" file:
• doc("books.xml")/bookstore/book/title
• (/bookstore selects the bookstore element, /book selects all the
book elements under the bookstore element, and /title selects
all the title elements under each book element)
The XQuery above will extract the following:
<title lang="en">Everyday Italian</title>
<title lang="en">Harry Potter</title>
<title lang="en">XQuery Kick Start</title>
<title lang="en">Learning XML</title>
110. Predicates
• XQuery uses predicates to limit the extracted data from
XML documents.
• The following predicate is used to select all the book
elements under the bookstore element that have a price
element with a value that is less than 30:
• doc("books.xml")/bookstore/book[price<30]
• The XQuery above will extract the following:
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
111. XQuery FLWOR Expression
What is FLWOR?
• FLWOR (pronounced "flower") is an acronym for
"For, Let, Where, Order by, Return".
• For - selects a sequence of nodes
• Let - binds a sequence to a variable
• Where - filters the nodes
• Order by - sorts the nodes
• Return - what to return (gets evaluated once for
every node)
112. XQuery FLWOR Expression
Look at the following path expression:
• doc("books.xml")/bookstore/book[price>30]/title
• The expression above will select all the title elements
under the book elements that are under the bookstore
element that have a price element with a value that is
higher than 30.
• The following FLWOR expression will select exactly
the same as the path expression above:
• for $x in doc("books.xml")/bookstore/book
where $x/price>30
return $x/title
The result will be:
<title lang="en">XQuery Kick Start</title>
<title lang="en">Learning XML</title>
113. Sorting
With FLWOR you can sort the result:
• for $x in doc("books.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title
• The result of the XQuery expression above
will be:
<title lang="en">Learning XML</title>
<title lang="en">XQuery Kick Start</title>
114. XQUERY HTML
<ul>
{
for $x in doc("books.xml")/bookstore/book/title
order by $x
return <li>{$x}</li>
}
</ul>
• The result of the above will be:
<ul>
<li><title lang="en">Everyday Italian</title></li>
<li><title lang="en">Harry Potter</title></li>
<li><title lang="en">Learning XML</title></li>
<li><title lang="en">XQuery Kick Start</title></li>
</ul>
115. XQUERY HTML
• Now we want to eliminate the title element, and show only
the data inside the title element:
<ul>
{
for $x in doc("books.xml")/bookstore/book/title
order by $x
return <li>{data($x)}</li>
}
</ul>
• The result will be (an HTML list):
<ul>
<li>Everyday Italian</li>
<li>Harry Potter</li>
<li>Learning XML</li>
<li>XQuery Kick Start</li>
</ul>
116. XQuery Syntax
XQuery 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 :)
117. 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>
• The result of the example above will be:
<adult>Everyday Italian</adult>
<child>Harry Potter</child>
<adult>XQuery Kick Start</adult>
<adult>Learning XML</adult>
118. 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
119. XQuery Adding
Elements and Attributes
Add HTML Elements and Text
• Now, we want to add some HTML elements to the result. We will
put the result in an HTML list - together with some text:
<html>
<body>
<h1>Bookstore</h1>
<ul>
{
for $x in doc("books.xml")/bookstore/book
order by $x/title
return <li>{data($x/title)}. Category: {data($x/@category)}</li>
}
</ul>
</body>
</html>
120. XQuery Adding
Elements and Attributes
The XQuery expression above will generate the
following result:
<html>
<body>
<h1>Bookstore</h1>
<ul>
<li>Everyday Italian. Category: COOKING</li>
<li>Harry Potter. Category: CHILDREN</li>
<li>Learning XML. Category: WEB</li>
<li>XQuery Kick Start. Category: WEB</li>
</ul>
</body>
</html>
121. XQUERY-Adding attributes
Add Attributes to HTML Elements
• Next, we want to use the category attribute as a class attribute in the HTML list:
<html>
<body>
<h1>Bookstore</h1>
<ul>
{
for $x in doc("books.xml")/bookstore/book
order by $x/title
return <li class="{data($x/@category)}">{data($x/title)}</li>
}
</ul>
</body>
</html>
The XQuery expression above will generate the following result:
<html>
<body>
<h1>Bookstore</h1>
<ul>
<li class="COOKING">Everyday Italian</li>
<li class="CHILDREN">Harry Potter</li>
<li class="WEB">Learning XML</li>
<li class="WEB">XQuery Kick Start</li>
</ul>
</body>
</html>
122. XQuery User-Defined Functions
Syntax
• declare function prefix:function_name($parameter as
datatype)
as returnDatatype
{
...function code here...
};
Notes on user-defined functions:
• Use the declare function keyword
• The name of the function must be prefixed
• The data type of the parameters are mostly the same as
the data types defined in XML Schema
• The body of the function must be surrounded by curly
braces
123. XQuery User-Defined Functions
Example of a User-defined Function Declared in the
Query
declare function local:minPrice($p as xs:decimal?,$d as
xs:decimal?)
as xs:decimal?
{
let $disc := ($p * $d) div 100
return ($p - $disc)
};
Below is an example of how to call the function above:
<minPrice>{local:minPrice($book/price,$book/discoun
t)}</minPrice>
125. • The Document Object Model (DOM) is a
programming API for HTML and XML
documents.
• It defines the logical structure of documents and
the way a document is accessed and manipulated.
• With the Document Object Model, programmers
can create and build documents, navigate their
structure, and add, modify, or delete elements and
content.
• Anything found in an HTML or XML document
can be accessed, changed, deleted, or added using
the Document Object Model
126. • The DOM is a W3C (World Wide Web
Consortium) standard.
• The DOM defines a standard for accessing
documents:
– "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.“
127. • The XML DOM defines a standard for accessing and
manipulating XML documents.
• The DOM presents an XML document as a tree-structure.
• The Document Object Model (DOM) is an application
programming interface (API) for HTML and XML
documents.
• It defines the logical structure of documents and the way a
document is accessed and manipulated.
• 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
128. What DOM is not
• DOM is not a mechanism for persisting or
storing objects as XML documents.
• DOM is not a set of data structures;rather it is
an object model describing XML elements
• DOM does not specify what information in a
document is relevant or how information
should be structured
129. Why do I need DOM
• To create or modify an XML document
programmatically
• It is possible to process XML documents using
simpler techniques such as XSLT
130. Disadvantages of using DOM
• DOM is memory intensive
• DOM API is too complex
• DOM is not practical for small devices such as
PDAs and cellular phones
DOM LEVELS
• Level 1 allows traversal of an XML document as
well as the manipulation of the content in that
document
• Level 2 extends level1 with additional features
such as namespace support,events,ranges.
131. 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.
132. • DOM Nodes
• According to the DOM, everything in an XML
document is a node.
• The DOM says:
• The entire document is a document node
• Every XML element is an element node
• The text in the XML elements are text nodes
• Every attribute is an attribute node
• Comments are comment nodes
134. • The root node in the XML above is named
<bookstore>.
• All other nodes in the document are contained
within <bookstore>.
• The root node <bookstore> holds four <book>
nodes.
• The first <book> node holds four nodes: <title>,
<author>, <year>, and <price>, which contains
one text node each, "Everyday Italian", "Giada De
Laurentiis", "2005", and "30.00".
135. • The XML DOM Node Tree
• The XML DOM views an XML document as a
tree-structure.
• The tree structure is called a node-tree.
• All nodes can be accessed through the tree.
• Their contents can be modified or deleted, and
new elements can be created.
• The node tree shows the set of nodes, and the
connections between them.
• The tree starts at the root node and branches out
to the text nodes at the lowest level of the tree:
136.
137. Node Parents, Children, and Siblings
• The nodes in the node tree have a hierarchical relationship
to each other.
• The terms parent, child, and sibling are used to describe the
relationships.
• Parent nodes have children.
• Children on the same level are called siblings (brothers or
sisters).
• In a node tree, the top node is called the root
• Every node, except the root, has exactly one parent node
• A node can have any number of children
• A leaf is a node with no children
• Siblings are nodes with the same parent
138.
139. Xml parser
• XML Parser provides way how to access or
modify data present in an XML document.
• Java provides multiple options to parse XML
document.
• Following are various types of parsers which
are commonly used to parse XML documents.
140. • Dom Parser - Parses the document by loading
the complete contents of the document and
creating its complete hiearchical tree in
memory.
• SAX Parser - Parses the document on event
based triggers. Does not load the complete
document into the memory.
• JDOM Parser - Parses the document in
similar fashion to DOM parser but in more
easier way.
141. • StAX Parser - Parses the document in similar
fashion to SAX parser but in more efficient way.
• XPath Parser - Parses the XML based on
expression and is used extensively in conjuction
with XSLT.
• DOM4J Parser - A java library to parse XML,
XPath and XSLT using Java Collections
Framework , provides support for DOM, SAX
and JAXP.
142. XML Parser
• A parser is a piece of program that takes a
physical representation of some data and
converts it into an in-memory form for the
program as a whole to use.
• Parsers are used everywhere in software.
• An XML Parser is a parser that is designed to
read XML and create a way for programs to
use XML.
143. • XML parser is a software library or a package
that provides interface for client applications to
work with XML documents.
• It checks for proper format of the XML
document and may also validate the XML
documents.
• Modern day browsers have built-in XML
parsers.
144.
145. • the goal of a parser is to transform XML into a readable code.
• To ease the process of parsing, some commercial products are available that
facilitate the breakdown of XML document and yield more reliable results.
• Some commonly used parsers are listed below:
• MSXML (Microsoft Core XML Services) : This is a standard set of XML tools
from Microsoft that includes a parser.
• System.Xml.XmlDocument : This class is part of .NET library, which contains a
number of different classes related to working with XML.
• Java built-in parser : The Java library has its own parser. The library is designed
such that you can replace the built-in parser with an external implementation such
as Xerces from Apache or Saxon.
• Saxon : Saxon offers tools for parsing, transforming, and querying XML.
• Xerces : Xerces is implemented in Java and is developed by the famous open
source Apache Software Foundation.
146. XML DOM Parser
• All major browsers have a built-in XML parser to read and
manipulate XML.
• The XML parser converts XML into an XML DOM object
that can be accessed with JavaScript.
• The XML DOM contains methods to traverse XML trees,
access, insert, and delete nodes.
• However, before an XML document can be accessed and
manipulated, it must be loaded into an XML DOM object.
• An XML parser reads XML, and converts it into an XML
DOM object that can be accessed with JavaScript.
• Most browsers have a built-in XML parser.
147.
148. • Advantages
• XML DOM is language and platform
independent.
• XML DOM is traversible - Information in XML
DOM is organized in a hierarchy which allows
developer to navigate around the hierarchy
looking for specific information.
• XML DOM is modifiable - It is dynamic in
nature providing developer a scope to add, edit,
move or remove nodes at any point on the tree.
149. • Disadvantages
• It consumes more memory (if the XML
structure is large) as program written once
remains in memory all the time until and
unless removed explicitly.
• Due to the larger usage of memory its
operational speed, compared to SAX is slower
150. PROGRAM 1
<!DOCTYPE html>
<html>
<head>
<script src="loadxmldoc.js"></script>
</head>
<body>
<script>
xmlDoc=loadXMLDoc("books.xml");
document.write(xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue + "<br>");
document.write(xmlDoc.getElementsByTagName("author")[0].childNodes[0].nodeValue + "<br>");
document.write(xmlDoc.getElementsByTagName("year")[0].childNodes[0].nodeValue);
</script>
</body>
</html>
OUTPUT:
Everyday Italian
Giada De Laurentiis
2005
151. • 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
152. XML DOM Methods
• x.getElementsByTagName(name) - get all
elements with a specified tag name
• x.appendChild(node) - insert a child node to x
• x.removeChild(node) - remove a child node
from x
153.
154. • You can access a node in three ways:
• 1. By using the getElementsByTagName()
method
• 2. By looping through (traversing) the nodes
tree.
• 3. By navigating the node tree, using the node
relationships.
155. • The getElementsByTagName() Method
• getElementsByTagName() returns all elements
with a specified tag name.
• Syntax
• node.getElementsByTagName("tagname");
156. • DOM Node List Length
• The length property defines the length of a
node list (the number of nodes).
• You can loop through a node list by using the
length property:
158. • Traversing Nodes
• The following code loops through the child nodes, that are also element nodes, of
the root node:
• Example
• var xmlDoc=loadXMLDoc("books.xml");
var x=xmlDoc.documentElement.childNodes;
for (i=0;i<x.length;i++)
{
// Process only element nodes (type 1)
if (x[i].nodeType==1)
{
document.write(x[i].nodeName);
document.write("<br>");
}
}
OUTPUT
book
book
book
book
159. • Navigating Node Relationships
• The following code navigates the node tree using the node relationships:
<!DOCTYPE html>
<html>
<head>
<script src="loadxmldoc.js"></script>
</head>
<body>
<script>
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book")[0].childNodes;
y=xmlDoc.getElementsByTagName("book")[0].firstChild;
for (i=0;i<x.length;i++)
{
if (y.nodeType==1)
{//Process only element nodes (type 1)
document.write(y.nodeName + "<br>");
}
y=y.nextSibling;
}
</script>
</body>
</html>
Output
title
author
year
price
160. • Node Properties
• In the XML DOM, each node is an object.
• Objects have methods and properties, that can
be accessed and manipulated by JavaScript.
• Three important node properties are:
• nodeName
• nodeValue
• nodeType
161. • The nodeName Property
• The nodeName property specifies the name of a node.
• nodeName is read-only
• nodeName of an element node is the same as the tag
name
• nodeName of an attribute node is the attribute name
• nodeName of a text node is always #text
• nodeName of the document node is always #document
162. • The nodeValue Property
• The nodeValue property specifies the value of
a node.
• nodeValue for element nodes is undefined
• nodeValue for text nodes is the text itself
• nodeValue for attribute nodes is the attribute
value
163. Get the Value of an Element
• The following code retrieves the text node value of the first <title> element:
Example
<!DOCTYPE html>
<html>
<head>
<script src="loadxmldoc.js"></script>
</head>
<body>
<script>
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0]
y=x.childNodes[0];
document.write(y.nodeValue);
</script>
</body>
</html>
OUTPUT
Everyday Italian
164. • The getAttribute() method returns an attribute value.
<!DOCTYPE html>
<html>
<head>
<script src="loadxmldoc.js">
</script>
</head>
<body>
<script>
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('book');
for (i=0;i<x.length;i++)
{
document.write(x[i].getAttribute('category'));
document.write("<br>");
}
</script>
</body>
</html>
Output
cooking
children
web
web
165. The getAttributeNode() method returns
an attribute node.
<!DOCTYPE html>
<html>
<head>
<script src="loadxmldoc.js">
</script>
</head>
<body>
<script>
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].getAttributeNode("lang");
txt=x.nodeValue;
document.write(txt);
</script>
</body>
</html>
OUTPUT
En
166. Change the Value of an Element
• The following code changes the text node value of the first
<title> element:
Example
<script>
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="Easy Cooking";
document.write(x.nodeValue);
</script>
OUTPUT:
• Easy Cooking
167. Change an Attribute Using
setAttribute()
• The setAttribute() method changes the value of an existing attribute, or
creates a new attribute.
• The following code changes the category attribute of the <book> element:
Example
<script>
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('book');
x[0].setAttribute("category","food");
document.write(x[0].getAttribute("category"));
</script>
OUTPUT:
FOOD
168. removeChild() and replaceChild()
• The removeChild() method removes a
specified node.
• When a node is removed, all its child nodes are
also removed
• Replace an Element Node
• The replaceChild() method is used to replace a
node.
169. EXAMPLE
//create a book element, title element and a text node
newNode=xmlDoc.createElement("book");
newTitle=xmlDoc.createElement("title");
newText=xmlDoc.createTextNode("A Notebook");
//add the text node to the title node,
newTitle.appendChild(newText);
//add the title node to the book node
newNode.appendChild(newTitle);
y=xmlDoc.getElementsByTagName("book")[0]
//replace the first book node with the new node
x.replaceChild(newNode,y);
z=xmlDoc.getElementsByTagName("title");
for (i=0;i<z.length;i++)
{
document.write(z[i].childNodes[0].nodeValue);
document.write("<br>");
}
</script>
</body>
</html>