160 likes | 279 Views
Why XML is Required. Problem: 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.
E N D
Why XML is Required • Problem: 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
What is XML • W3C - Set some standards to develop markup language. • To develop a MARKUP language • Declare elements, attributes • Set grammar rules (i.e. describe how the above declared elements, attributes can be used)
XML • W3C - Set some standards to develop markup language. • To develop a MARKUP language • Declare elements, attributes • Set grammar rules (i.e. describe how the above declared elements, attributes can be used)
XML Standards • The 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.
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
Tags used in XSD: • Schema file can contains either • Simple element - Element containing only text • Complex element – Element containing one or more elements • Note: If an element has attribute then the element is treated as complex element • 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
Cont… • Simple Element E.g.: • <?xml version="1.0"?><xs:schemaxmlns: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… • 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… • <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… • <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>
Indicators • Used to control how the elements are used in the document • Order indicators: • All - Specifies that all child elements can appear in any order • Choice - Specifies that either one child element or another can occur • Sequence - Specifies that all child elements should occur in same order • Occurrence indicators: • maxOccurs - Maximum no of times an element can occur • minOccurs - Minimum no of times an element can occur
Miscellaneous • <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>
Namespaces • TargetNamespace • DefaultNamespace • Names with prefix • Schema Import • Schema Include
XPATH: • Is an path expression to select the nodes from XML document
XPATH: <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 Result bookstore 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 document bookstore//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
XSLT • 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