Xml Session No 1

51

description

XML Language,

Transcript of Xml Session No 1

Page 1: Xml Session No 1
Page 2: Xml Session No 1

•XML stands for EXtensible Markup Language •XML is a markup language much like HTML •XML was designed to carry data, not to display data •XML tags are not predefined. You must define your own tags •XML is designed to be self-descriptive •XML is a W3C Recommendation

Page 3: Xml Session No 1

There are two current versions of XML. The first XML 1.0 was initially defined in 1998 and it has undergone minor changes/revisions since then. Though changes have been done, a new revision number has not been assigned to this version. It is called XML 1.0 5th edition, with the latest edition (5th) being released on 26th Nov 2008.

The second version XML 1.1 was initially published on 4th Feb 2004, the same day as the XML 1.0, 3rd edition. XML 1.1 is currently in its 2nd edition which was released on 16th Aug 2006. It contains certain features that are expected to make XML easier to use.

Page 4: Xml Session No 1

Web Application

Web Services

.

04/11/23Ashok K Sharma 4

XML

Windows Application

Mobile Application

SQL ServerDB2 Oracle

AccessData Sources

Page 5: Xml Session No 1

Domain-specific vocabulary

Data interchange

Smart searches

Granular updates

User-selected view of data

Message transformation

04/11/23Ashok K Sharma 5

Page 6: Xml Session No 1

The various components of an XML document used for representing data in a hierarchical order are:

Processing Instruction (PI)TagsElementsContentAttributesEntitiesComments

04/11/23Ashok K Sharma 6

Page 7: Xml Session No 1

<?xml version=“1.0” encoding=“UTF-8”?><STOREDATA><!--STOREDATA is the root element--> <STORE STOREID=“S101”>

<PRODUCTNAME>Toys</PRODUCTNAME>

<QUANTITY>100</QUANTITY>

<DISPLAY>The price of this toy is &lt; 200 </DISPLAY>

</STORE></STOREDATA>

04/11/23Ashok K Sharma 7

Processing Instruction (PI)

Provides information on how the XML file should be processed.

Page 8: Xml Session No 1

<?xml version=“1.0” encoding=“UTF-8”?><STOREDATA><!--STOREDATA is the root element--> <STORE STOREID=“S101”>

<PRODUCTNAME>Toys</PRODUCTNAME>

<QUANTITY>100</QUANTITY>

<DISPLAY>The price of this toy is &lt; 200 </DISPLAY>

</STORE></STOREDATA>

04/11/23Ashok K Sharma 8

Tags

Is a means of identifying data. Tags consist of start tag and end tag.

Page 9: Xml Session No 1

<?xml version=“1.0” encoding=“UTF-8”?><STOREDATA><!--STOREDATA is the root element--> <STORE STOREID=“S101”>

<PRODUCTNAME>Toys</PRODUCTNAME>

<QUANTITY>100</QUANTITY>

<DISPLAY>The price of this toy is &lt; 200 </DISPLAY>

</STORE></STOREDATA>

04/11/23Ashok K Sharma 9

Root Element

Contains all other elements in the document.

Page 10: Xml Session No 1

<?xml version=“1.0” encoding=“UTF-8”?><STOREDATA><!--STOREDATA is the root element--> <STORE STOREID=“S101”>

<PRODUCTNAME>Toys</PRODUCTNAME>

<QUANTITY>100</QUANTITY>

<DISPLAY>The price of this toy is &lt; 200 </DISPLAY>

</STORE></STOREDATA>

04/11/23Ashok K Sharma 10

Comments

Are statements used to explain the XML code.

Page 11: Xml Session No 1

<?xml version=“1.0” encoding=“UTF-8”?><STOREDATA><!--STOREDATA is the root element--> <STORE STOREID=“S101”>

<PRODUCTNAME>Toys</PRODUCTNAME>

<QUANTITY>100</QUANTITY>

<DISPLAY>The price of this toy is &lt; 200 </DISPLAY>

</STORE></STOREDATA>

04/11/23Ashok K Sharma 11

Child Elements

Are the basic units used to identify and describe data in XML.

Page 12: Xml Session No 1

<?xml version=“1.0” encoding=“UTF-8”?><STOREDATA><!--STOREDATA is the root element--> <STORE STOREID=“S101”>

<PRODUCTNAME>Toys</PRODUCTNAME>

<QUANTITY>100</QUANTITY>

<DISPLAY>The price of this toy is &lt; 200 </DISPLAY>

</STORE></STOREDATA>

04/11/23Ashok K Sharma 12

Components of an XML Document (Contd.)

Attributes

Provide additional information about the elements for which they are declared.

Page 13: Xml Session No 1

<?xml version=“1.0” encoding=“UTF-8”?><STOREDATA><!--STOREDATA is the root element--> <STORE STOREID=“S101”>

<PRODUCTNAME>Toys</PRODUCTNAME>

<QUANTITY>100</QUANTITY>

<DISPLAY>The price of this toy is &lt; 200 </DISPLAY>

</STORE></STOREDATA>

04/11/23Ashok K Sharma 13

Content

Refers to the information represented by the elements of an XML document. An element can contain:

• Character or data content

• Element content

• Combination or mixed content

Page 14: Xml Session No 1

<?xml version=“1.0” encoding=“UTF-8”?><STOREDATA><!--STOREDATA is the root element--> <STORE STOREID=“S101”>

<PRODUCTNAME>Toys</PRODUCTNAME>

<QUANTITY>100</QUANTITY>

<DISPLAY>The price of this toy is &lt; 200 </DISPLAY>

</STORE></STOREDATA>

04/11/23Ashok K Sharma 14

Entities

Is a set of information that can be used by specifying a single name.

Page 15: Xml Session No 1

Every start tag must have an end tag.

Empty tags must be closed using a forward slash (/).

All attribute values must be given in double quotation marks.

Tags must have proper nesting.

XML tags are case sensitive.

04/11/23Ashok K Sharma 15

Page 16: Xml Session No 1
Page 17: Xml Session No 1

04/11/23Ashok K Sharma 17

Element declaration

Syntax <!ELEMENT element-name (element-content)>

Example <!ELEMENT employee(#PCDATA)>

Any Element content <!ELEMENT employee ANY>

Other Child Element as Content<!ELEMENT employee (name,phone,age,dept)>

At least One occurrence of child element<!ELEMENT employee (name,contact+)>

Zero or more occurrence of element<!ELEMENT employee(name,phone,email*)>

Page 18: Xml Session No 1

04/11/23Ashok K Sharma 18

Attribute Declaration Syntax<!ATTLIST element-name attribute-name attribute-type default-value>

Example<!ATTLIST payment type CDATA "check">

Entity References Character

&lt; <&gt; >&amp; &&quot; "&apos; '

Entities

Page 19: Xml Session No 1

04/11/23Ashok K Sharma 19

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

http://www.xmlvalidation.com/

Page 20: Xml Session No 1

04/11/23Ashok K Sharma 20

Note.dtd<!ELEMENT note (to,from,heading,body)><!ELEMENT to (#PCDATA)><!ELEMENT from (#PCDATA)><!ELEMENT heading (#PCDATA)><!ELEMENT body (#PCDATA)>

Referring DTD from XML<!DOCTYPE root-element SYSTEM "filename">

Note.xml<?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>

Page 21: Xml Session No 1
Page 22: Xml Session No 1

An XML schema defines the list of elements and attributes that can be used in an XML document. An XML schema specifies the order in which the elements appear in the XML document, and their data types. Microsoft has developed the XML Schema Definition (XSD) language to define the schema of an XML document.

04/11/23Ashok K Sharma 22

Page 23: Xml Session No 1

Some of the advantages of creating an XML schema by using XSD are:

XSD provides control over the type of data that can be assigned to elements and attributes. XSD enables you to create your own data types. XSD enables you to specify restrictions on data. The syntax for defining an XSD is the same as the syntax used for XML documents. XML schema content models can be used to validate mixed content. XML schema is extensible.XML schema is self documenting.

04/11/23Ashok K Sharma 23

Page 24: Xml Session No 1

In an XML schema created using XSD, every element must be associated with a data type.XSD Data Types

Primitive

User DefinedSimple Type

Complex Type

04/11/23Ashok K Sharma 24

Data Types in XML Schemas (Contd.)

Page 25: Xml Session No 1

boolean

A Boolean true or false value. Representations of true are "true" and "1"; false is denoted as "false" or "0".

byte A signed 8-bit integer in the range [-128, 127]. date Represents a specific date

dateTime Represents a specific instant of time. It has the form YYYY-MM-DDThh:mm:ss folowed by an optional time-zone suffix

decimal Any base-10 fixed-point number.double A 64-bit floating-point decimal numberfloat A 32-bit floating-point decimal numberint Represents a 32-bit signed integer in the range [-2,147,483,648, 2,147,483,647].integer Represents a signed integerlanguage One of the standardized language codeslong A signed, extended-precision integer; at least 18 digits are guaranteednegativeInteger Represents an integer less than zerononNegativeInteger An integer greater than or equal to zerononPositiveInteger An integer less than or equal to zero.positiveInteger An extended-precision integer greater than zerostring Any sequence of zero or more characters.

Page 26: Xml Session No 1

1. Complex Type : A data type which contains other elements. 2. Simple Type : A data type which contains one formatted element.

Page 27: Xml Session No 1
Page 28: Xml Session No 1

A CSS is a text file containing one or more rules or definitions for the style characteristics of a particular element. It controls how tags are formatted in XML and HTML documents. The CSS file can be included in XML documents with the same data structure.

04/11/23Ashok K Sharma 28

Page 29: Xml Session No 1

A CSS can be applied to an XML document using the following syntax: <?xml:stylesheet type="text/css" href="path-name"?>

04/11/23Ashok K Sharma 29

Specifies the type of formatting that is being used.

Page 30: Xml Session No 1

CSS does not support the reorder, sort, and display of elements based on a condition. For such advanced formatting, XML supports Extensible Style Sheet Language (XSL). XSL has two parts:

XSL Transformations (XSLT)XML Path (XPath)

XSL:Contains instructions on how an XML document should be transformed into an HTML or an XHTML document.Uses XPath expressions to extract specific data from an XML document.

The XSLT processor transforms the XML document into an HTML or XHTML or into another XML document.

04/11/23Ashok K Sharma 30

Introducing XSL

Page 31: Xml Session No 1

The XSLT processor applies the transformation information to the source document and builds the result tree as shown in the following figure.

04/11/23Ashok K Sharma 31

MSXML Parser

XSLT tree

XSLT processor

Source tree

Result tree

XSLT style sheet

XML document

Page 32: Xml Session No 1

XSLT provides the following elements to select and format data:stylesheet

value-of

for-each

sort

text

04/11/23Ashok K Sharma 32

Page 33: Xml Session No 1

XSLT provides the following elements to select and format data:stylesheet

value-of

for-each

sort

text

04/11/23Ashok K Sharma 33

Instructs the browser that the document is a style sheet file.

Is the root element for all XSLT style sheets.

Is written as:

<xsl:stylesheet xmlns:xsl=

"http://www.w3.org/1999/XSL/Transform" version="1.0">

Page 34: Xml Session No 1

XSLT provides the following elements to select and format data:stylesheet

value-of

for-each

sort

text

04/11/23Ashok K Sharma 34

Displays the value of the specified element or attribute.

Follows the syntax:<xsl:value‑of

select="elementname/attributename"/>

Page 35: Xml Session No 1

XSLT provides the following elements to select and format data:stylesheet

value-of

for-each

sort

text

04/11/23Ashok K Sharma 35

Instructs the XSLT processor to process the information for each instance of the specified pattern.

Follows the syntax:

<xsl:for-each select="pattern"> [action to be performed] </xsl:for-each>

Page 36: Xml Session No 1

XSLT provides the following elements to select and format data:stylesheet

value-of

for-each

sort

text

04/11/23Ashok K Sharma 36

Sorts data based on the values assigned to elements and attributes.

Follows the syntax:<xsl:sort select="expression" order="ascending | descending" case-order="upper-first | lower-first“data-type="text | number | qname"/>

Page 37: Xml Session No 1

XSLT provides the following elements to select and format data:stylesheet

value-of

for-each

sort

text

04/11/23Ashok K Sharma 37

Generates constant text in the output and displays labels.

Follows the syntax: <xsl:text> Text to be displayed as label </xsl:text>

Page 38: Xml Session No 1

Used with the if and choose elements to narrow down the formatting criteria.The following table lists various comparison and Boolean operators.

04/11/23Ashok K Sharma 38

Operator Meaning Example

Equal to=PRICE[. = 20]PRODUCTNAME[. = ‘Mini Bus’]

!=

&lt;

and

&lt;=

&gt;=

or

&gt;

not

Not equal to

Less than

Less than or equal to

Greater than

Logical AND

Logical OR

Negation operator

Greater than or equal to

PRICE[. != 20]PRODUCTNAME[. != ‘Barbie Doll’]

PRICE[. &lt; 20]

PRICE[. &gt; 20]

PRICE[. &lt;= 20]

PRICE[. &gt;= 20]

PRICE[. &gt 20 and . &lt; 30]

PRICE[. = 20 or . = 45]

PRICE[not(. = 30)]

Page 39: Xml Session No 1

Operator/Special Character

Example Description

@ @PRODUCTID Used as a prefix for the attribute.

@* @* Selects all attributes.

: : Separates the namespace prefix from the element or attribute name.

( ) (PRICE*QUANTITY) Used to group operations.

[ ] [@PRODUCTID='P001'] Applies a filter pattern.

+ num1 + num2 Returns the sum of two numbers.

- num1 - num2 Returns the difference of two numbers.

* num1 * num2 Returns the product of two numbers.

div num1 div num2 Returns the quotient of two numbers.

mod num1 mod num2 Returns the modulus, that is, the remainder of integer division.

Page 40: Xml Session No 1
Page 41: Xml Session No 1

DOM defines the logical structure of documents.DOM provides an Application Programming Interface (API) for dynamically accessing and manipulating a document. The DOM objects have associated methods and properties to access and manipulate a document. A DOM-enabled parser is required to use the features provided by DOM.A DOM-enabled parser:

Parses an XML document to ascertain its validity. Creates an in‑memory representation of the XML document as a tree structure.

04/11/23Ashok K Sharma 41

Page 42: Xml Session No 1

MSXML parser:Is the Microsoft implementation of DOM.

Provides fundamental as well as added interfaces to access documents.

The following figure represents how a DOM tree is used by applications to access data.

04/11/23Ashok K Sharma 42

XML Document

MSXML Library

ParsedDocument

DOM Tree

Root

ApplicationParser

Child

ChildText

Text

Page 43: Xml Session No 1

Following are the key DOM objects:Document

Element

Node

NodeList

Attr

Text

ParseError

04/11/23Ashok K Sharma 43

Page 44: Xml Session No 1

Following are the key DOM objects:Document

Element

Node

NodeList

Attr

Text

ParseError

04/11/23Ashok K Sharma 44

It is the top-level object that implements all the basic DOM methods.

It also has methods that support XSLT.It has methods that can be used to navigate,

query, and modify the content and structure of an XML document.

Some of the methods provided by this object are createElement(), createAttribute(), createComment() , and createTextNode().

Some of the properties provided by this object that help in manipulating the information contained in the object are async, childNodes, firstChild, documentElement, xml, and readyState.

Page 45: Xml Session No 1

Following are the key DOM objects:Document

Element

Node

NodeList

Attr

Text

ParseError

04/11/23Ashok K Sharma 45

It represents all the element nodes in an XML document.

The attributes associated with the elements are considered to be the properties of the elements rather than their child elements.

Some of the methods of this object are also inherited from the Node object.

Some of the methods provided by this object are getAttribute(), getElementsByTagName(), normalize(), and removeAttributeNS().

Page 46: Xml Session No 1

Following are the key DOM objects:Document

Element

Node

NodeList

Attr

Text

ParseError

04/11/23Ashok K Sharma 46

It represents a single node in the XML document tree structure.It provides methods to work with child elements.Some of the methods of this object are

appendChild(newChild),insertBefore(newNode,refNode),and removeChild(nodeName).

Page 47: Xml Session No 1

Following are the key DOM objects:Document

Element

Node

NodeList

Attr

Text

ParseError

04/11/23Ashok K Sharma 47

It provides a list of nodes present in an XML document for manipulation.This object enables you to iterate through a collection of nodes. Some of the method of this object are item() and nextNode().

Page 48: Xml Session No 1

Following are the key DOM objects:Document

Element

Node

NodeList

Attr

Text

ParseError

04/11/23Ashok K Sharma 48

It represents an attribute of the Element object.

It is also a Node and inherits various attributes and methods of Node object.

An attribute is not considered by the DOM to be a child node of an element, but rather a property.

Page 49: Xml Session No 1

Following are the key DOM objects:Document

Element

Node

NodeList

Attr

Text

ParseError

04/11/23Ashok K Sharma 49

It represents the text inside an XML element in

the node tree.The splitText() method is associated with

this object.

Page 50: Xml Session No 1

The DOM objects can be used within scripting languages such as JavaScript and VBScript.Using DOM objects in scripts allow dynamically applying a style sheet to an XML document.The code for using DOM objects for accessing an XML document needs to be used as an HTML page.

04/11/23Ashok K Sharma 50

XML DOM Objects in Scripts

Page 51: Xml Session No 1