Web Technology - XML

Embed Size (px)

Citation preview

  • 7/30/2019 Web Technology - XML

    1/67

    EXtensible Markup

    Language(XML)

    U. K. Roy

  • 7/30/2019 Web Technology - XML

    2/67

    U.K.R., 2008

    An HTML system

    HTML

    documentWeb Server

    Web Client

    Internet

    Parser,

    formatter,

    interface

  • 7/30/2019 Web Technology - XML

    3/67

    U.K.R., 2008 XML 3

    Role of HTML

    HTML Designed to display data

    Focuses on appearance

    Has a fixed set of predefined tags

    Ambiguity

  • 7/30/2019 Web Technology - XML

    4/67

    U.K.R., 2008 XML 4

    Role of XML

    EXtensible Markup Language W3C recommendation, 1998

    Designed to structure, transport and store data

    Transformation and Dynamic datacustomization

    Interoperable way to represent and process

    documents (not necessarily on web)

    Self descriptive

  • 7/30/2019 Web Technology - XML

    5/67

    U.K.R., 2008 XML 5

    Example

    John

    Ani

    Reminder

    Return my book on Monday

  • 7/30/2019 Web Technology - XML

    6/67

    U.K.R., 2008 XML 6

    Another Example

    RequiemMozart

    Equivalent HTML code:

    Requiem is a song composed byMozart

  • 7/30/2019 Web Technology - XML

    7/67

    U.K.R., 2008 XML 7

    Role of XML

    Not a replacement of HTML XML focuses on what data are

    HTML focuses on how data look

    Tags are custom defined (not predefined)

    Functional meaning depends on application

    Everything must be marked up correctly

  • 7/30/2019 Web Technology - XML

    8/67

    U.K.R., 2008

    XML and Databases

    XML brings benefits of DBs to documents Schema to model information directly

    Formal validation, locking, versioning, rollback...

    But

    Not all traditional database concepts map

    cleanly, because documents are fundamentally

    different in some ways

  • 7/30/2019 Web Technology - XML

    9/67

    U.K.R., 2008 XML 9

    XML Building blocks

    Element Delimited by angular brackets

    Identifies the nature of the content it surrounds

    General format:

    Empty element:

    Attribute

    Name-value pairs that occur inside start-tags afterelement name, like:

  • 7/30/2019 Web Technology - XML

    10/67

    U.K.R., 2008 XML 10

    XML Building blocks--Prolog

    The part of an XML document that precedes theXML data

    Includes

    A declaration: version [, encoding, standalone]

    An optional DTD (Document Type Definition )

    Processing Instructions (Optional)

  • 7/30/2019 Web Technology - XML

    11/67

    U.K.R., 2008 XML 11

    XML Elements

    XML Elements are Extensible

    More and more elements may be added to carry moreinformation

    XML Elements have Relationships

    Elements are related as parents and children

    Elements have Content

    Elements can have different types of content: empty content

    simple content

    element content mixed content

    attributes

    XML elements must follow the naming rules

  • 7/30/2019 Web Technology - XML

    12/67

    U.K.R., 2008 XML 12

    XML Elements naming rules

    Names can only contain letters, digits and some

    other special characters.

    Names can not start with a number or punctuation

    marks

    Names must not contain the string xml, XML orXml

    Names can not contain while space(s).

  • 7/30/2019 Web Technology - XML

    13/67

    U.K.R., 2008

    Anatomy of an element

    Use a hyphen: .

    Start-tag Content End-tag

    Element

    Elementtype

    Attribute

    name

    Attribute

    value

    (character)

    entity

    reference Elementtype

    Attribute

  • 7/30/2019 Web Technology - XML

    14/67

    U.K.R., 2008 XML 14

    The Basic Rules

    XML is case sensitive

    This is incorrect

    This is correct

  • 7/30/2019 Web Technology - XML

    15/67

    U.K.R., 2008 XML 15

    The Basic Rules

    All start tags must have end tagsMozart

    Mozart



    Empty Element

  • 7/30/2019 Web Technology - XML

    16/67

    U.K.R., 2008 XML 16

    The Basic Rules

    Elements must be properly nested

    This is incorrect nesting

    This is correct nesting

  • 7/30/2019 Web Technology - XML

    17/67

    U.K.R., 2008 XML 17

    The Basic Rules

    XML declaration must be the first statement

  • 7/30/2019 Web Technology - XML

    18/67

    U.K.R., 2008 XML 18

    The Basic Rules

    Every document must contain a root element

    .....

  • 7/30/2019 Web Technology - XML

    19/67

    U.K.R., 2008 XML 19

    The Basic Rules

    Attribute values must be quoted with invertedcommas

    Ani

    John

  • 7/30/2019 Web Technology - XML

    20/67

    U.K.R., 2008 XML 20

    The Basic Rules

    Certain characters are reserved for parsing

    if salary< 1000 then

    if salary < 1000 then

  • 7/30/2019 Web Technology - XML

    21/67

    U.K.R., 2008 XML 21

    Predefined entities

    < < less than

    > > greater than

    & & &ampersand

    ' ' apostrophe

    " " quotation mark

  • 7/30/2019 Web Technology - XML

    22/67

    U.K.R., 2008 XML 22

    The Basic Rules

    With XML, white space is preserved

    With XML, a new line is always stored as LF

    Comments in XML:

    Can go almost anywhere (not inside tags) Schemas can contain comments, too

  • 7/30/2019 Web Technology - XML

    23/67

    U.K.R., 2008 XML 23

    Common Errors for Element Naming

    Do not use white space when creating names

    for elements

    Element names cannot begin with a digit,

    although names can contain digits

    Only certain punctuation allowed periods,

    colons, and hyphens

  • 7/30/2019 Web Technology - XML

    24/67

    U.K.R., 2008 XML 24

    XML Attributes

    Located in the start tag of elements Provide additional information about

    elements

    Often provide information that is not apart of data

    Must be enclosed in quotes

    Should I use an element or an attribute? metadata (data about data) should be stored as attributes,

    and that data itself should be stored as elements

  • 7/30/2019 Web Technology - XML

    25/67

    U.K.R., 2008 XML 25

    Types of XML Documents

    XML document

    Well Formed XML.

    Syntax is correct

    Valid XML.

    Well formed Validated against a DTD/Schema

  • 7/30/2019 Web Technology - XML

    26/67

    U.K.R., 2008 XML 26

    Well-Formed XML

    Properties

    Documents must have a root element

    Elements must have a closing tag

    Elements must be properly nested

    Attribute values must be quoted

    Advantage

    Avoids fixed nature like HTML

    Flexible

    Expandable

  • 7/30/2019 Web Technology - XML

    27/67

    U.K.R., 2008 XML 27

    Valid XML

    Properties

    Well Formed

    Comply with the rules defined in a DTD/Schema

    Advantage

    Clear Understanding

    Data verification

    Interoperability

    Better document processing

  • 7/30/2019 Web Technology - XML

    28/67

    U.K.R., 2008 XML 28

    XML Validation

    XML

    schema

    XML

    Parser

    XML

    document

    Error

    messages

    Optimized

    XML

    document

    xmllint --valid sample.xml

  • 7/30/2019 Web Technology - XML

    29/67

    U.K.R., 2008 XML 29

    Dislaying XML

    Sanchoita

    Rabindranath Tagore

    2009

    200.00

  • 7/30/2019 Web Technology - XML

    30/67

    U.K.R., 2008 XML 30

    Document Type Definition

    Allows developers to create a set of rules to

    specify legal content and place restrictions on

    an XML file

    Parser generates error, if XML document does

    not follow the rules contained within DTD

    Including a DTD

    Using internal declaration

    Using external file Both

  • 7/30/2019 Web Technology - XML

    31/67

    U.K.R., 2008 XML 31

    Internal (standalone) DTD

    For custom documents

    Uses DOCTYPE declaration

    ]>

    Hello, world!

    Specify in XML declaration

  • 7/30/2019 Web Technology - XML

    32/67

    U.K.R., 2008 XML 32

    External DTD

    Most common

    Use DOCTYPE declaration before root element

    Hello, world!

  • 7/30/2019 Web Technology - XML

    33/67

    U.K.R., 2008 XML 33

    External plus Internal DTD

    Usually to declare entities

    Use DOCTYPE declaration before root element

    Hello, world&excl;

  • 7/30/2019 Web Technology - XML

    34/67

    U.K.R., 2008 XML 34

    DTD XML Building Blocks

    XML documents consist of following blocks

    Elements

    Attributes

    Entities

    < > & " &apos

    PCDATA

    Parsed CharacterDATA

    Entities will be expanded

    CDATA

    CharacterDATA

    Entities will not be expanded

  • 7/30/2019 Web Technology - XML

    35/67

    U.K.R., 2008 XML 35

    Declaring Elements

    An empty element

    Example

    Usage:

  • 7/30/2019 Web Technology - XML

    36/67

    U.K.R., 2008 XML 36

    Declaring Elements

    Element with data

    Example

  • 7/30/2019 Web Technology - XML

    37/67

    U.K.R., 2008 XML 37

    Declaring Elements

    Usage:

    U. K. Roy

    What is the full form of DTD?

    [email protected]

    This is an XML document

  • 7/30/2019 Web Technology - XML

    38/67

    U.K.R., 2008 XML 38

    DTD Declarations

    Example : Elements with Data

    Valid Usage

    April

    This is a month

    Invalid Usage:

    cant have children!-->JanMarch

  • 7/30/2019 Web Technology - XML

    39/67

    U.K.R., 2008 XML 39

    Declaring Elements

    Element with Children (sequential)

    Example

    body)>

  • 7/30/2019 Web Technology - XML

    40/67

    U.K.R., 2008 XML 40

    Declaring Elements

    Inner elements must also be declared

  • 7/30/2019 Web Technology - XML

    41/67

    U.K.R., 2008 XML 41

    Declaring Elements

    Usage:

    [email protected]

    [email protected]

    Learn DTD from

    www.w3schools.com

  • 7/30/2019 Web Technology - XML

    42/67

    U.K.R., 2008 XML 42

    Declaring Elements

    Usage:

    S. C. Mallick Road

    Kolkata

    700032

  • 7/30/2019 Web Technology - XML

    43/67

    U.K.R., 2008 XML 43

    Using internal DTD

    ]>

    [email protected]

    [email protected] DTD from w3schools.com

  • 7/30/2019 Web Technology - XML

    44/67

    U.K.R., 2008 XML 44

    DTD Declarations

    John Doe1234 Preston Ave.

    Charlottesville, Va22903

  • 7/30/2019 Web Technology - XML

    45/67

    U.K.R., 2008 XML 45

    Declaring Elements

    Occurrence Indicators

    Term Meaning Example

    , Sequence Operators a, b, c

    | Choice operators a | b | c

    + One or more a+

    * Zero or more a*

    ? Single optional a?

    () grouping (a)

  • 7/30/2019 Web Technology - XML

    46/67

    U.K.R., 2008 XML 46

    Examples

  • 7/30/2019 Web Technology - XML

    47/67

    U.K.R., 2008 XML 47

    Declaring Elements

    Example

    Publisher?)>

  • 7/30/2019 Web Technology - XML

    48/67

    U.K.R., 2008 XML 48

    Cautions concerning DTDs

    All element declarations begin with

    The ELEMENT declaration is case sensitive

    Elements declared with the #PCDATA content

    model can not have children

    When describing sequences, the XML

    document must contain exactly those elements

    in exactly that order.

  • 7/30/2019 Web Technology - XML

    49/67

    U.K.R., 2008 XML 49

    Declaring Attributes

    General Syntax

    Example

    10

  • 7/30/2019 Web Technology - XML

    50/67

    U.K.R., 2008 XML 50

    Declaring Attributes

    The attribute-type can be one of the following:Type DescriptionCDATA The value is character data(en1|en2|..) The value must be one from an enumerated listID The value is a unique idIDREF The value is the id of another elementIDREFS The value is a list of other idsNMTOKEN The value is a valid XML nameNMTOKENS The value is a list of valid XML namesENTITY The value is an entityENTITIES The value is a list of entitiesNOTATION The value is a name of a notationxml: The value is a predefined xml value

  • 7/30/2019 Web Technology - XML

    51/67

    U.K.R., 2008 XML 51

    Declaring Attributes

    The default-value can be one of the following:Value Explanationvalue The default value of the attribute#REQUIRED The attribute is required#IMPLIED The attribute is not required#FIXED value The attribute value is fixed

  • 7/30/2019 Web Technology - XML

    52/67

    U.K.R., 2008 XML 52

    Examples

    id ID #REQUIRED

    name CDATA #IMPLIED>

    type (bullets|ordered|glossary) "ordered">

  • 7/30/2019 Web Technology - XML

    53/67

    U.K.R., 2008 XML 53

    Examples

    Valid XML:

    E l #REQUIRED

  • 7/30/2019 Web Technology - XML

    54/67

    U.K.R., 2008 XML 54

    Examples--#REQUIRED

    DTD:

    Valid XML:

    Invalid XML:

    E l #IMPLIED

  • 7/30/2019 Web Technology - XML

    55/67

    U.K.R., 2008 XML 55

    Examples--#IMPLIED

    DTD:

    Valid XML:

    valid XML:

    E l #FIXED

  • 7/30/2019 Web Technology - XML

    56/67

    U.K.R., 2008 XML 56

    Examples--#FIXED

    DTD:

    Valid XML:

    Invalid XML:

    E l E t d

  • 7/30/2019 Web Technology - XML

    57/67

    U.K.R., 2008 XML 57

    Examples--Enumerated

    DTD:

    XML example: or

    El t f Att ib t ?

  • 7/30/2019 Web Technology - XML

    58/67

    U.K.R., 2008 XML 58

    Elements of Attributes?

    attributes cannot contain multiple values (child

    elements can)

    attributes are not easily expandable (for future

    changes)

    attributes cannot describe structures (childelements can)

    attributes are more difficult to manipulate by

    program code

    attribute values are not easy to test against a DTD

    D l i E titi

  • 7/30/2019 Web Technology - XML

    59/67

    U.K.R., 2008 XML 59

    Declaring Entities

    General Syntax

    Example // //

    &language; is standardized by &W3C;&copyright; UKR

    Displaying XML

  • 7/30/2019 Web Technology - XML

    60/67

    U.K.R., 2008 XML 60

    Displaying XML

    XHTML

    XML namespace

    XML DOM

    XPath

    XSL (XSLT+XPath)

    Client side

    By browser

    Explicitly by author (using JavaScript)

    Server side

    Schema

    Displaying XML

  • 7/30/2019 Web Technology - XML

    61/67

    U.K.R., 2008 XML 61

    Displaying XML

    XML documents do not carry information about

    how to display the data

    We can add display information to XML with

    CSS (Cascading Style Sheets)

    XSL (eXtensible Stylesheet Language) --- preferred

    XML i t HTML

  • 7/30/2019 Web Technology - XML

    62/67

    U.K.R., 2008 XML 62

    XML into HTML

    XSLT can transform into (called "output

    method"):

    XML

    HTML

    text

    Server-side XSLT engine

    content in XML

    served as HTML browser never knows

    Cli t id XSL

  • 7/30/2019 Web Technology - XML

    63/67

    U.K.R., 2008 XML 63

    Client-side XSL

    XML

    XSLT

    FO

    Server side XSL

  • 7/30/2019 Web Technology - XML

    64/67

    U.K.R., 2008 XML 64

    Server-side XSL

    XML

    XSLT

    XSLTengine HTML

    XML DOM

  • 7/30/2019 Web Technology - XML

    65/67

    U.K.R., 2008 XML 65

    XML DOM

    Everyday ItalianGiada De Laurentiis200530.00

    Harry PotterJ K. Rowling200529.99

    Learning XMLErik T. Ray200339.95

    XML DOM Tree

  • 7/30/2019 Web Technology - XML

    66/67

    U.K.R., 2008 XML 66

    XML DOM Tree

  • 7/30/2019 Web Technology - XML

    67/67

    Questions?