exemel.ppt

Embed Size (px)

Citation preview

  • 7/27/2019 exemel.ppt

    1/91

    Extensible Markup

    Language

  • 7/27/2019 exemel.ppt

    2/91

    Contents

    Why XML? Introduction HTML & XML

    Types of XML Documents Namespaces

    DTD

    Schema

    2

  • 7/27/2019 exemel.ppt

    3/91

    Why XML?

    It is a standard, easily extensible way toencode information

    To resolve issues in large scale electronicinformation publishing

    To create richly structured arbitrarydocument

    Due to its easy parseability, software cantransform it in countless ways

    3

  • 7/27/2019 exemel.ppt

    4/91

    Introduction

    XML stands for eXtensible Markup Language A technology from W3C

    A method of creating document with Userdefined tags

    In XML, data speaks what it is? Not how itlooks

    XML can serve web with HTML and WMLtransformation

    4

  • 7/27/2019 exemel.ppt

    5/91

    HTML & XML

    Comparison

  • 7/27/2019 exemel.ppt

    6/91

    HTML Data Sample

    Following is an HTML Sample Code Guess what is talks about!!!

    VishwanathanNalasupara

    6

  • 7/27/2019 exemel.ppt

    7/91

    Umm

    There is some bold and italic text But, we could still guess what data it is

    representing

    Bold text Must be the name of some person

    Italic text

    Must be the name of some place Finally, Good guesses pays.

    7

  • 7/27/2019 exemel.ppt

    8/91

    Sample XML Document

    Here is the XML version of the same data

    VishwanathanNalasupara

    Do you still need to guess what it is?

    8

  • 7/27/2019 exemel.ppt

    9/91

    Comparison

    HTML Data Presentation

    Un-Structured

    Non-Case-Sensitive Vocabulary of build-in tags

    No error checks

    Current Version - 4.x Extension - .htm, .html

    9

  • 7/27/2019 exemel.ppt

    10/91

    Comparison

    XML Data Definition

    100% Structured100% Case-Sensitive

    User defined tags

    Checks for errors

    Version since beginning - 1.0 Extension - .xml

    10

  • 7/27/2019 exemel.ppt

    11/91

    Types of XML

    Documents

  • 7/27/2019 exemel.ppt

    12/91

    Two Types of XML document

    Well-Formed An XML document follows all the

    structuredness rules of the XML language

    Valid A well-formed XML document is Valid if it

    conforms to its Vocabulary

    Every valid XML document is a well-formedXML document but, every well-formeddocument is not a valid XML document

    12

  • 7/27/2019 exemel.ppt

    13/91

    Well-Formedness Rules

    Document should contain at least Prolog

    XML Declaration (Mandatory)

    Root Element (Mandatory) All tags are grouped within a single tag

    13

  • 7/27/2019 exemel.ppt

    14/91

    Continued

    Should follow rules given by W3C There can be only one root element

    Root element should include all otherelements

    Every element should have a closing tag

    Empty elements should be self-closing

    Proper nesting of tags should be there

    14

  • 7/27/2019 exemel.ppt

    15/91

    Continued

    Attribute values should be enclosed indouble quotes

    Naming rules should be followed forelements and attributes Should start with A-Z or a-z or _

    Can contain A-Z or a-z or 0-9 or _ or - or .(dot)

    Any external data embedded in XML shouldalso be well-formed

    15

  • 7/27/2019 exemel.ppt

    16/91

    Is it a Well-Formed Document?

    Zubair Shaikh29

    Mumbai

    16

  • 7/27/2019 exemel.ppt

    17/91

    Why to Validate XML Document?

    Lets consider the Person example A well-formedness check will make it sure

    that there is no syntax error but,

    What if you want to make it sure, that The attribute value for gender is male/female

    The name element is the first child of person

    All the elements are appeared and that too inthe desired sequence

    17

  • 7/27/2019 exemel.ppt

    18/91

    Valid XML Document

    Follows a vocabulary Vocabulary is a user defined set of elements

    and rules such as

    The element that can or must appear How often the element can appear

    How the element can be nested

    The allowable, required and defaultattributes

    18

  • 7/27/2019 exemel.ppt

    19/91

    Defining Vocabulary

    Vocabulary can be defined using DTD - Document Type Definition

    XSD - XML Schema Definition

    19

  • 7/27/2019 exemel.ppt

    20/91

    Namespaces

  • 7/27/2019 exemel.ppt

    21/91

    Why Namespaces?

    Naming standards to the element/attributeare too brittle

    Element and Attribute names can beambiguous

    There is no way to know if a name is alreadyin use

    21

  • 7/27/2019 exemel.ppt

    22/91

    Introduction

    It is introduced to makes naming unique andfree of ambiguity

    It refers to two-part name as QualifiedNames or QNames

    It deals with the mechanics of associating aURI qualifier with element and attributenames URIs are used because they provide a unique

    naming convention

    22

  • 7/27/2019 exemel.ppt

    23/91

    Qualified Names

    QNames are used in place of element names QName have a prefix and a local part They are like

    It can be interpreted as

    {http://www.example.com/person}name

    It is important to understand that the

    significance of prefixes lies in the URI

    23

  • 7/27/2019 exemel.ppt

    24/91

    Declaring Namespaces

    The syntax of namespace declaration is

    The following example declare thenamespace person for URIhttp://www.example.com/persons

    24

  • 7/27/2019 exemel.ppt

    25/91

    An Example

    Zubair29

    Mumbai

    25

  • 7/27/2019 exemel.ppt

    26/91

    Default Namespace Example

    Zubair

    29Mumbai

    26

  • 7/27/2019 exemel.ppt

    27/91

    Document Type

    Definition

    DTD

  • 7/27/2019 exemel.ppt

    28/91

    Why DTD?

    A DTD adds syntactical requirements inaddition to the well-formed requirement It helps in eliminating errors when creating

    or editing XML documents It clarifies the intended semantics It simplifies the processing of XML

    documents

    28

  • 7/27/2019 exemel.ppt

    29/91

    Introduction

    Document Type Definitions (DTDs) imposestructure on XML documents The DTD is a syntactic specification The DTD follows regular expressions

    29

  • 7/27/2019 exemel.ppt

    30/91

    Requirements

    Consider the following example

    Zubair Shaikh29

    Mumbai

    022-22872183

    +91-9829748328

    [email protected]

    30

  • 7/27/2019 exemel.ppt

    31/91

    Requirements

    person is an element name, age and city appears in a sequence There can be one or more occurrences of

    phone email is the optional element

    Person can be either Indian or NRI

    31

  • 7/27/2019 exemel.ppt

    32/91

    Specifying the Structure

    person

    name,age,city

    phone+

    email?

    indain|nri

    To specify the person element

    To specify the sequence of

    name age and city

    To specify one or moreoccurrences of phone

    To specify email is optional

    (Zero or One)To specify either indian or nri

    32

  • 7/27/2019 exemel.ppt

    33/91

    Summary of Regular Expressions

    ee1,e2

    e*e?e+

    e1|e2

    (e)

    An elementSequence

    Zero or more occurrencesZero or One occurrencesOne or more occurrencesEither

    Grouping

    33

  • 7/27/2019 exemel.ppt

    34/91

    An Example

    (indian|nri))>

    34

  • 7/27/2019 exemel.ppt

    35/91

    XML Schema Definition

    Schema

    S ?

  • 7/27/2019 exemel.ppt

    36/91

    Why XML Schema?

    Have you realized any issues with DTD? Is it an XML document

    Need for Data types

    Validating element values Specifying upper limit of occurrences

    Defining custom types for similar elements

    36

    I d i

  • 7/27/2019 exemel.ppt

    37/91

    Introduction

    XML Schema is an XML based alternative toDTD An XML vocabulary for expressing your

    data's business rules Object-oriented'ish

    With the support for data types

    Better support for mixed content model

    37

  • 7/27/2019 exemel.ppt

    38/91

    XML Path

    XPath

    I t d ti

  • 7/27/2019 exemel.ppt

    39/91

    Introduction

    XPath is a W3C recommendation (1999) According to W3C

    It is a Language for addressing path of anXML document

    XPath models XML documents as trees ofnodes using the Document Object Model

    XPath uses path expressions to locate nodeswith XML documents

    39

    I t d ti

  • 7/27/2019 exemel.ppt

    40/91

    Introduction

    XPath is an efective and powerful way ofquery XML documents XPath is a language for specifying parts of an

    XML document

    XPath also inludes a library of standardfunctions for node-sets, strings, boolean andnumbers

    XPath uses path expressions to identify XMLdocument parts

    40

    A E l

  • 7/27/2019 exemel.ppt

    41/91

    An Example

    Java

    Frank450

    XMLMark390

    41

    L ti N d

  • 7/27/2019 exemel.ppt

    42/91

    Locating Nodes

    Path = /library/book/price

    JavaFrank

    450

    XML

    Mark

    390

    42

    Wild C d

  • 7/27/2019 exemel.ppt

    43/91

    Wild Cards

    Path = /library/book/*

    JavaFrank450

    XML

    Mark

    390

    43

    S l ti B h

  • 7/27/2019 exemel.ppt

    44/91

    Selecting Branches

    Path = /library/book[1]

    JavaFrank450

    XML

    Mark

    390

    44

    S l ti B h

  • 7/27/2019 exemel.ppt

    45/91

    Selecting Branches

    Path = /library/book[last()]

    JavaFrank450

    XML

    Mark

    390

    45

    S l t S l P th

  • 7/27/2019 exemel.ppt

    46/91

    Select Several Paths

    Path =/library/book/title|/library/book/price

    Java

    Frank

    450

    XML

    Mark

    390

    46

    Selecting Attributes

  • 7/27/2019 exemel.ppt

    47/91

    Selecting Attributes

    Path = //book[@isbn=43-4749]

    JavaFrank450

    XML

    Mark

    390

    47

    Location Paths

  • 7/27/2019 exemel.ppt

    48/91

    Location Paths

    One of the most important xpathexpressions is the location path. A location path expression results in a node

    set Each location path consist of one or more

    location steps

    48

    Location Step Syntax

  • 7/27/2019 exemel.ppt

    49/91

    Location Step Syntax

    Each location step has An axis

    A node test

    One or more predicates (optional) Location step syntax

    axisname::nodetest[predicate]

    child::book[author=Mark]

    49

    XPath Axis

  • 7/27/2019 exemel.ppt

    50/91

    XPath Axis

    There are several axes in XPath Each of which selects a different subset of

    the nodes in the document

    That is depending on the context node

    50

    Location Path: Axes

  • 7/27/2019 exemel.ppt

    51/91

    Location Path: Axes

    Ancestor Ancestor-or-selfAttribute Child

    Descendent Descendent-or-self

    Following Following-sibling

    Namespace Parent

    Preceding Preceding-sibling

    Self Sibling

    51

    Location Path: Child Axis

  • 7/27/2019 exemel.ppt

    52/91

    Location Path: Child Axis

    The child axis with library context node

    library

    book book

    title author price title author price

    52

    Location Path: Sibling Axis

  • 7/27/2019 exemel.ppt

    53/91

    Location Path: Sibling Axis

    The preceding-sibling axis with pricecontext nodelibrary

    book book

    title author price title author price

    53

    Location Path: Nodes

  • 7/27/2019 exemel.ppt

    54/91

    Location Path: Nodes

    There are seven types of node Root nodes

    Element nodes

    Text nodes Attribute nodes

    Namespace nodes

    Processing instruction nodes Comment nodes

    54

    Location Path: Node Test

  • 7/27/2019 exemel.ppt

    55/91

    Location Path: Node Test

    Node test determine what kind of nodes beselected along a given axis The node test is applied to each node in the

    axis Example location path with node test

    child::foo - foo child element

    parent::text() - text node of parent descendent::comment() - comment node

    below

    55

    Location Path: Predicates

  • 7/27/2019 exemel.ppt

    56/91

    Location Path: Predicates

    Predicate is a further test to retain nodes It filters the node set into a new node set Each node is evaluated in turn

    If predicate is true for a given node, it is keptin the new node set

    If false it is removed

    Predicates consist of a well-formedexpressions

    56

    Predicate Expressions

  • 7/27/2019 exemel.ppt

    57/91

    Predicate Expressions

    Expression consist of boolean operators

    functions

    numbers, strings comparison operators

    Example

    child::book[author=Mike] descendent::book[position()=2]

    descendent::library*attribute::type=isbn+

    57

    Predicate Expressions

  • 7/27/2019 exemel.ppt

    58/91

    Predicate Expressions

    Expressions describes a set of nodes in adocuments Path expression may be absolute or relative

    Absolute begins from the root node /library/book/title

    Relative begins from the context node

    book/author

    58

    Expressions

  • 7/27/2019 exemel.ppt

    59/91

    Expressions

    XPath supports four types of expression Numerical

    + - * div mod

    Equality = !=

    Relational

    < > =

    Boolean

    and or

    59

    Functions

  • 7/27/2019 exemel.ppt

    60/91

    Functions

    XPath defines a number of useful functionsfor converting and translating data Node set functions

    count() - returns number of nodes in node set

    String functions

    concat() - returns the concatenations of all itsarguments

    contains() - returns true if the second of 2 givenstrings is in the first

    60

    Functions

  • 7/27/2019 exemel.ppt

    61/91

    Functions

    Number functions sum() - returns the total value of a set of numeric

    values in a node set

    Boolean functions

    not() - returns true if the argument is false, andfalse if the argument is true

    61

  • 7/27/2019 exemel.ppt

    62/91

    Presenting XML

    Presenting XML

  • 7/27/2019 exemel.ppt

    63/91

    Presenting XML

    XML data can be presented The available options for the same

    CSS - Cascading Style Sheets

    XSL - XML Stylesheet Language

    63

    Cascading Style Sheet

  • 7/27/2019 exemel.ppt

    64/91

    Cascading Style Sheet

    CSS can be used to present XML documentMost of the browsers supports CSS

    Eg: MSIE, Firefox, Opera

    CSS is used to view XML as if it were HTML A style must be defined for each of the XML

    element

    Or the browser displays it in its defaultformat

    64

    Cascading Style Sheet

  • 7/27/2019 exemel.ppt

    65/91

    Cascading Style Sheet

    All display characteristics of each elementmust be explicitly defined Elements are displayed in the order they are

    encountered in the XML document No reordering of elements or other

    processing is possible

    Must put a processing instruction at the topof your XML file Below XML declaration

    65

    XML Processing Instruction

  • 7/27/2019 exemel.ppt

    66/91

    XML Processing Instruction

    CSS is written in a separate fileSaved with the extension .css Below is an example of processing

    instruction

    66

    An Example

  • 7/27/2019 exemel.ppt

    67/91

    An Example

    Rosy Dsouza

    Happy Diwali

    67

    Style Sheet

  • 7/27/2019 exemel.ppt

    68/91

    Style Sheet

    greeting{display:block;

    background-color:pink;

    }

    person{

    font:20pt verdana;color:blue;

    }

    message{

    font:24pt georgia;color:red;font-style:itlaic;

    }

    68

  • 7/27/2019 exemel.ppt

    69/91

    XML Style Sheet

    Language

    Transformation

    Introduction

  • 7/27/2019 exemel.ppt

    70/91

    Introduction

    XML Style Sheet Language - Transformation A markup language and programming

    syntax for processing XML

    Most often used to Transform XML to HTML to deliver to

    standard web clients

    Transform XML from one set of tag to

    another

    Transform XML into another syntax/system

    70

    Introduction

  • 7/27/2019 exemel.ppt

    71/91

    Introduction

    An XML document is a collection of nodesthat can be identified, selected and actedupon using an XPath expression

    XSLT is based on the process of matching

    templates to nodes of the XML tree

    An XSLT style sheet is a collection of

    templates that act against specified nodes inthe XML source tree

    71

    An Example

  • 7/27/2019 exemel.ppt

    72/91

    An Example

    Sam Mendes

    Eid Mubarak

    72

    Tree Nodes

  • 7/27/2019 exemel.ppt

    73/91

    Tree Nodes

    / Root

    greeting Root element

    person message Child elements

    73

    Templates

  • 7/27/2019 exemel.ppt

    74/91

    Templates

    A separate template is defined for eachelement Syntax for defining template

    74

    Calling Templates

  • 7/27/2019 exemel.ppt

    75/91

    g p

    A template can call other templates

    Tree Processing

    Call templates of all the children nodes

    Call all title templates of context node

    Call the named template, regardless of the sourcetree

    75

    Pull and Push Processing

  • 7/27/2019 exemel.ppt

    76/91

    g

    Push Processing of templates In this the source document controls the

    order of processing

    Eg:

    Pull Processing of templates

    It address particular element in the sourcetree regardless of the position

    Eg:

    76

    Selecting Element and Attributes

  • 7/27/2019 exemel.ppt

    77/91

    g

    To select the content of a particular elementor attribute element is

    Takes xpath expression to search node

    For selecting an attribute, attribute name

    precedes with an @ symbol

    77

    Decision Structure

  • 7/27/2019 exemel.ppt

    78/91

    For processing data based on a specifiedcriteria XSL provides

    78

    Looping & Sorting

  • 7/27/2019 exemel.ppt

    79/91

    p g g

    XSLT provides support for looping Looping too is using xpath expression for

    selecting a node to perform operation oneach node

    For looping XSL provide

    For sorting

    79

  • 7/27/2019 exemel.ppt

    80/91

    XML Query Language

    XQuery

    Why XQuery?

  • 7/27/2019 exemel.ppt

    81/91

    y y

    One of the great strengths of XML is its

    flexibility in representing many different

    kinds of information from diverse sources

    As increasing amounts of information are

    stored, exchanged, & presented using XML

    The ability to intelligently query XML datasources becomes increasingly important

    To exploit this flexibility, an XML querylanguage must provide features forretrieving and interpreting information

    81

    Querying XML Data

  • 7/27/2019 exemel.ppt

    82/91

    y g

    XPath Simple Navigation through the tree

    XSLT

    Recursive Traversal XQuery

    The SQL of XML

    82

    What is XQuery

  • 7/27/2019 exemel.ppt

    83/91

    y

    SQL for Database

    SQL Database

    XQuery for XML Document / Database

    XML

    DocumentXQuery or

    Database

    83

    Introduction

  • 7/27/2019 exemel.ppt

    84/91

    XQuery is a query language for XML data

    XQuery follows FLWR (flower)

    FOR

    LET WHERE

    RETURN

    84

    FLWOR

  • 7/27/2019 exemel.ppt

    85/91

    FOR and/or LET Appear one or more times

    WHERE

    Optional RETURN

    Always present

    85

    An Example

  • 7/27/2019 exemel.ppt

    86/91

    Java

    Frank450

    XMLMark390

    86

    FOR

  • 7/27/2019 exemel.ppt

    87/91

    FOR variable IN expressionFOR $x IN

    doc(library.xml)/library/bookRETURN {$x}

    Result:

    Java

    XML

    87

    LET

  • 7/27/2019 exemel.ppt

    88/91

    LET variable = expression

    LET $x =

    doc(library.xml)/library/bookRETURN {$x}

    Result:

    ......

    88

    Query Example

  • 7/27/2019 exemel.ppt

    89/91

    Find all book titles priced more than 300FOR $x IN

    doc(library.xml)/library/book

    WHERE $x/price > 400

    RETURN{$x/title}

    Output:

    Java

    XML

    89

    Query Example

  • 7/27/2019 exemel.ppt

    90/91

    Find all book titles priced more than 300LET $x :=

    doc(library.xml)/library/book[price > 300]

    RETURN{$x/title}

    Output:

    JavaXML

    90

  • 7/27/2019 exemel.ppt

    91/91

    Thank You

    Zubair Shaikh