Download - Why XML is Required
Manohar – [email protected] 1
Why XML is RequiredProblem: We want to save the data and retrieve it further or to transfer over the network. This is most common requirement for any business.
Solution: Design a format in which the data can saved and understand further.- This requirement was first identified by IBM and came up with GML - GML is used only by IBM for internal projects
SGML – Standardized General Markup Language- It is an improvement over GML- Initially released by IBM and later took by W3C
SGML|||----HTML|||----XML
Manohar – [email protected] 2
What is XML W3C - Set some standards to develop markup language.
To develop a MARKUP language 1. Declare elements, attributes2. Set grammar rules (i.e. describe how the above declared elements, attributes can
be used)
Manohar – [email protected] 3
XML W3C - Set some standards to develop markup language.
To develop a MARKUP language 1. Declare elements, attributes2. Set grammar rules (i.e. describe how the above declared elements, attributes can
be used)
Manohar – [email protected] 4
XML StandardsThe document must have exactly one top-level element (the document element or
root element).
All other elements must be nested within it.
Elements must be properly nested. That is, if an element starts with another element, it must also end within that same element.
Elements must have both start-tag and end-tag.
The element-type name in a start-tag must exactly match in the corresponding end-tag.
Element-type names are case-sensitive.
Manohar – [email protected] 5
XSD An XML Schema describes the structure of an XML document. An XML Schema tells about
- elements that can appear in a document- attributes that can appear in a document- which elements are child elements- the order of child elements- the number of child elements- whether an element is empty or can include text- data types for elements and attributes- default and fixed values for elements and attributes
XML Schema supports- to describe allowable document content- to validate the correctness of data- to work with data from a database- to define data facets (restrictions on data)- to define data patterns (data formats)- to convert data between different data types
Manohar – [email protected] 6
Schema file can contains either 1. Simple element - Element containing only text2. Complex element – Element containing one or more elements
Note: If an element has attribute then the element is treated as complex element
1. Simple Element:
<schema> - element is the root element of every XML Schema<element> - used to define element<attribute>- used to define attribute<restriction> - defines the acceptable values for an element or attribute.<minInclusive> - minimum no allowed<maxInclusive> - maximum no allowed< totalDigits>- exact no of digits allowed<enumeration> - limits to set of acceptable values.<pattern> - limit value to series of characters<length> - limit the length of the value<minLength> - minimum length of the value<maxLength> - maximum length of the value
Tags used in XSD:
Manohar – [email protected] 7
Simple Element E.g.:
<?xml version="1.0"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace=" http://manoharsr.wordpress.com "xmlns="http://manoharsr.wordpress.com" elementFormDefault="qualified“>
<xs:element name="id" type="xsd:int" minOccurs="0" maxOccurs="unbounded"/>
Cont…
Manohar – [email protected] 8
2. Complex Element:
<complexType> - used to define complex element or complex type<complexContent> - extending the compexType with another complexType<extension> - used of extending
<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>
Cont…
Manohar – [email protected] 9
<xs:element name="employee" type="personinfo"/><xs:element name="student" type="personinfo"/><xs:element name=“customer" 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>
Cont…
Manohar – [email protected] 10
<xs:element name="employee" type=" fullpersoninfo "/><xs:element name="student" type="personinfo"/><xs:element name=“customer" type=" fullpersoninfo "/>
<xs:complexType name="personinfo"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence></xs:complexType>
<xs:complexType name="fullpersoninfo"> <xs:complexContent> <xs:extension base="personinfo"> <xs:sequence> <xs:element name="address" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="country" type="xs:string"/> </xs:sequence> </xs:extension> </xs:complexContent></xs:complexType>
Cont…
Manohar – [email protected] 11
Used to control how the elements are used in the document Order indicators:
1. All - Specifies that all child elements can appear in any order2. Choice - Specifies that either one child element or another can occur3. Sequence- Specifies that all child elements should occur in same order
Occurrence indicators:1. maxOccurs - Maximum no of times an element can occur2. minOccurs - Minimum no of times an element can occur
Indicators
Manohar – [email protected] 12
<any> - Enable to extend the xml document with elements not specified in schema Mixed – Allows an element to contain both elements and text
XSD<xs:element name=“Receipt"> <xs:complexType mixed="true"> <xs:sequence> <xs:element name=“receiptno" type="xs:positiveInteger"/> <xs:element name="name" type="xs:string"/> <xs:element name=“receiptdate" type="xs:date"/> </xs:sequence> </xs:complexType></xs:element>
XML<letter> Receipt No< receiptno >123456</ receiptno >. To < name >Manohar</name> Date<receiptdate>2012-07-13</receiptdate>.</letter>
Miscellaneous
Manohar – [email protected] 13
Namespaces TargetNamespaceDefaultNamespaceNames with prefix
Schema ImportSchema Include
Manohar – [email protected] 14
Is an path expression to select the nodes from XML document
XPATH:
Expression Description
Nodename selects all the nodes with the name “Nodename”
/ selects from the root node
// selects the node from the current node and no matter where they are if the selection matches
. Selects the current node
.. Selects parent of the current node
@ Selects attribute
Manohar – [email protected] 15
<bookstore><book>
<title lang="eng">Harry Potter</title><price>29.99</price>
</book><book>
<title lang="eng">Learning XML</title><price>39.95</price>
</book></bookstore>
Path Expression Resultbookstore Selects all nodes with the name "bookstore“/bookstore Selects the root element bookstore
Note: If the path starts with a slash ( / ) it always represents an absolute path to an element!
bookstore/book Selects all book elements that are children of bookstore//book Selects all book elements no matter where they are in the documentbookstore//book Selects all book elements that are descendant of the bookstore element,
no matter where they are under the bookstore element//@lang Selects all attributes that are named lang
XPATH:
Manohar – [email protected] 16
Used to transform XML document into XHTML or any other XML document
<stylesheet> - Root element that declares xml document as xsl style sheet<template> - template contains rules to apply when a specified node is matched<value-of> - used to extract data from the selected node.<for-each> - used to iterate the array nodes and select the node.<if> - Conditionally selecting the node<choose> - used when you have multiple conditions using <when> and <otherwise><sort> - sort the output nodes
XSLT