INF 523 Web Technologies with XML - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

INF 523 Web Technologies with XML

Description:

body Don't forget me this weekend /body /note 11. CDATA and PCDATA. CDATA means character data ... forget me this weekend! /body /note 32. Referencing ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 35
Provided by: Baha2
Category:
Tags: inf | xml | technologies | web | weekend

less

Transcript and Presenter's Notes

Title: INF 523 Web Technologies with XML


1
INF 523 Web Technologies (with XML)
  • Week 3
  • Bahadir K. Akcam
  • Fall 2006

2
Case Assignment
  • Due on November 13
  • Read By Invitation Only by Galen Gruman at CIO
  • http//www.cio.com/archive/090106/col_tec.html
  • Find an XML implementation case
  • Evaluate the Grumans article and your case to
    discuss
  • how organizations use xml technologies in their
    work,
  • their expectations from these technologies,
  • and current opportunities and obstacles for
    implementing XML
  • Max 4 pages

3
XML Namespaces
  • XML Namespaces provide a method to avoid element
    name conflicts.
  • When different XML documents are shared, same
    element names may be used for different purposes

4
XML Namespaces
  • Document 1)
  • lttablegt
  • lttrgt
  • lttdgtAppleslt/tdgt
  • lttdgtBananaslt/tdgt
  • lt/trgt
  • lt/tablegt
  • Document 2)
  • lttablegt
  • ltnamegtTea tablelt/namegt ltwidthgt80lt/widthgt
    ltlengthgt120lt/lengthgt lt/tablegt

5
Using Prefix
  • Document 1)
  • lthtablegt
  • lthtrgt
  • lthtdgtAppleslt/htdgt
  • lthtdgtBananaslt/htdgt
  • lt/htrgt
  • lt/htablegt
  • Document 2)
  • ltftablegt
  • ltfnamegtTea tablelt/fnamegt ltfwidthgt80lt/fwidthgt
    ltflengthgt120lt/flengthgt lt/ftablegt

6
Using namespaces
  • An XML Namespace is simply a group of names,
    usually with a related purpose or context, where
    the group has a globally unique name (the
    namespace name).
  • This is often ensured by using a domain name
    (from Internet DNS) as the first part of the
    namespace name.
  • xmlnsnamespace-prefix"namespaceURI"

7
Using namespaces
  • ltftable xmlnsf"http//furniturez.com/furniture"
    gt ltfnamegtAfrican Coffee Tablelt/fnamegt
    ltfwidthgt80lt/fwidthgt ltflengthgt120lt/flengthgt
  • lt/ftablegt

8
Namespaces
  • When a namespace is defined in the start tag of
    an element, all child elements with the same
    prefix are associated with the same namespace.
  • Default Namespaces Defining a default namespace
    for an element saves us from using prefixes in
    all the child elements.
  • xmlns"namespaceURI"

9
Namespaces
  • Namespaces are NOT
  • Resources that exist anywhere (other than
    conceptually)
  • Directly related to DTDs or XML schemas

10
DTDs and Valid XML Documents
  • lt?xml version"1.0"?gt
  • lt!DOCTYPE note
  • lt!ELEMENT note (to,from,heading,body)gt
    lt!ELEMENT to (PCDATA)gt
  • lt!ELEMENT from (PCDATA)gt
  • lt!ELEMENT heading (PCDATA)gt
  • lt!ELEMENT body (PCDATA)gt
  • gt
  • ltnotegt
  • lttogtTovelt/togt
  • ltfromgtJanilt/fromgt
  • ltheadinggtReminderlt/headinggt
  • ltbodygtDon't forget me this weekendlt/bodygt
  • lt/notegt

11
CDATA and PCDATA
  • CDATA means character data
  • PCDATA means parsed character data.
  • All text in an XML document will be parsed by the
    parser.
  • Only text inside a CDATA section will be ignored
    by the parser.

12
Parsed Data
  • ltmessagegtThis text is also parsedlt/messagegt
  • ltnamegtltfirstgtBilllt/firstgtltlastgtGateslt/lastgtlt/namegt
  • ltnamegt
  • ltfirstgtBilllt/firstgt
  • ltlastgtGateslt/lastgt
  • lt/namegt

13
Escape Characters
  • Illegal XML characters (lt, gt, , , ) have to be
    replaced by entity references.
  • ltmessagegtif salary lt 1000 thenlt/messagegt
  • ltmessagegtif salary lt 1000 thenlt/messagegt
  • Everything inside a CDATA section is ignored by
    the parser.
  • "lt!CDATA" and ends with "gt"
  • If your text contains a lot of "lt" or ""
    characters - as program code often does - the XML
    element can be defined as a CDATA section.

14
XML Validation
  • (1) XML with correct syntax is Well Formed XML.
  • (2) XML validated against a DTD is Valid XML.

15
Well Formed XML Documents
  • A "Well Formed" XML document has correct XML
    syntax.
  • XML documents must have a root element
  • XML elements must have a closing tag
  • XML tags are case sensitive
  • XML elements must be properly nested
  • XML attribute values must always be quoted

16
Valid XML Documents
  • A "Valid" XML document is a "Well Formed" XML
    document, which also conforms to the rules of a
    Document Type Definition (DTD)
  • Remember DTD rules!
  • Preventing having different elements with
    different formats into your XML files

17
XML Schema
  • XML Schema is an XML-based alternative to DTDs.
  • An XML Schema describes the structure of an XML
    document.
  • The XML Schema language is also referred to as
    XML Schema Definition (XSD).

18
XML Schema
  • The purpose of an XML Schema is to define the
    legal building blocks of an XML document, just
    like a DTD.
  • An XML Schema defines
  • 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

19
XML Schema
  • XML Schemas are the Successors of DTDs
  • XML Schemas are extensible to future additions
  • XML Schemas are richer and more powerful than
    DTDs
  • XML Schemas are written in XML
  • XML Schemas support data types
  • XML Schemas support namespaces

20
Why Use XML Schemas?
  • XML Schemas Support Data Types
  • It is easier
  • 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

21
Why Use XML Schemas?
  • XML Schemas use XML Syntax
  • Another great strength about XML Schemas is that
    they are written in XML.
  • You don't have to learn a new language
  • You can use your XML editor to edit your Schema
    files
  • You can use your XML parser to parse your Schema
    files
  • You can manipulate your Schema with the XML DOM
  • You can transform your Schema with XSLT

22
Why Use XML Schemas?
  • XML Schemas Secure Data Communication
  • When sending data from a sender to a receiver, it
    is essential that both parts have the same
    "expectations" about the content.
  • With XML Schemas, the sender can describe the
    data in a way that the receiver will understand.
  • A date like "03-11-2004" will, in some
    countries, be interpreted as 3.November and in
    other countries as 11.March.
  • However, an XML element with a data type like
    this ltdate type"date"gt2004-03-11lt/dategt
  • ensures a mutual understanding of the content,
    because the XML data type "date" requires the
    format "YYYY-MM-DD".

23
Why Use XML Schemas?
  • XML Schemas are Extensible
  • XML Schemas are extensible, because they are
    written in XML.
  • With an extensible Schema definition you can
  • Reuse your Schema in other Schemas
  • Create your own data types derived from the
    standard types
  • Reference multiple schemas in the same document

24
Why Use XML Schemas?
  • Well-Formed is not Enough
  • A well-formed XML document is a document that
    conforms to the XML syntax rules, like
  • it must begin with the XML declaration
  • it must have one unique root element
  • start-tags must have matching end-tags
  • elements are case sensitive
  • all elements must be closed
  • all elements must be properly nested
  • Even if documents are well-formed they can still
    contain errors, and those errors can have serious
    consequences.

25
XSD How To?
  • XML documents can have a reference to a DTD or to
    an XML Schema
  • Remember our famous XML document
  • lt?xml version"1.0"?gt
  • ltnotegt
  • lttogtTovelt/togt
  • ltfromgtJanilt/fromgt
  • ltheadinggtReminderlt/headinggt
  • ltbodygtDon't forget me this weekend!lt/bodygt
  • lt/notegt

26
XSD How To?
  • A DTD File
  • lt!ELEMENT note (to, from, heading, body)gt
  • lt!ELEMENT to (PCDATA)gt
  • lt!ELEMENT from (PCDATA)gt
  • lt!ELEMENT heading (PCDATA)gt
  • lt!ELEMENT body (PCDATA)gt

27
XSD How To?
  • An XML Schema called note.xsd
  • lt?xml version"1.0"?gt
  • ltxsschema xmlnsxs"http//www.w3.org/2001/XMLSch
    ema" targetNamespace"http//www.albany.edu"
    xmlns"http//www.albany.edu" elementFormDefault
    "qualified"gt
  • ltxselement name"note"gt
  • ltxscomplexTypegt
  • ltxssequencegt
  • ltxselement name"to" type"xsstring"/gt
  • ltxselement name"from" type"xsstring"/gt
    ltxselement name"heading" type"xsstring"/gt
  • ltxselement name"body" type"xsstring"/gt
  • lt/xssequencegt
  • lt/xscomplexTypegt
  • lt/xselementgt
  • lt/xsschemagt

28
XSD How To?
  • A Reference to an XML Schema
  • lt?xml version"1.0"?gt
  • ltnote xmlns"http//www.albany.edu"
    xmlnsxsi"http//www.w3.org/2001/XMLSchema-instan
    ce" xsischemaLocation"http//www.albany.edu
    note.xsd"gt
  • lttogtTovelt/togt
  • ltfromgtJanilt/fromgt
  • ltheadinggtReminderlt/headinggt
  • ltbodygtDon't forget me this weekend!lt/bodygt
  • lt/notegt

29
ltSchemagt Element
  • The ltschemagt element is the root element of every
    XML Schema.
  • lt?xml version"1.0"?gtltxsschemagt...
    ...lt/xsschemagt
  • ltxsschema xmlnsxs"http//www.w3.org/2001/XMLSch
    ema" targetNamespace"http//www.albany.edu"
    xmlns"http//www.albany.edu" elementFormDefault"
    qualified"gt... ...lt/xsschemagt

30
ltSchemagt Element
  • xmlnsxs"http//www.w3.org/2001/XMLSchema"
  • the elements and data types used in the schema
    come from the "http//www.w3.org/XMLSchema"
    namespace.
  • It also specifies that the elements and data
    types that come from the "http//www.w3.org/2001/X
    MLSchema" namespace should be prefixed with xs
  • targetNamespace"http//www.albany.edu"
  • the elements defined by this schema (note, to,
    from, heading, body) come from the
    "http//www.albany.edu" namespace.
  • xmlns"http//www.albany.edu"
  • the default namespace is "http//www.albany.edu".
  • elementFormDefault"qualified"
  • any elements used by the XML instance document
    which were declared in this schema must be
    namespace qualified.

31
Referencing a Schema in an XML Document
  • lt?xml version"1.0"?gt
  • ltnote xmlns"http//www.albany.edu
  • xmlnsxsi"http//www.w3.org/2001/XMLSchema-instan
    ce
  • xsischemaLocation"http//www.albany.edu
    note.xsd"gt
  • lttogtTovelt/togt ltfromgtJanilt/fromgt
  • ltheadinggtReminderlt/headinggt
  • ltbodygtDon't forget me this weekend!lt/bodygt
  • lt/notegt

32
Referencing a Schema in an XML Document
  • xmlns"http//www.albany.edu"
  • specifies the default namespace declaration.
  • xmlnsxsi"http//www.w3.org/2001/XMLSchema-instan
    ce"
  • Refer to XML Schema Instance namespace
  • xsischemaLocation"http//www.albany.edu
    note.xsd"
  • This attribute has two values. The first value is
    the namespace to use. The second value is the
    location of the XML schema to use for that
    namespace.

33
NEXT WEEK
  • Continue on XML Schema
  • More time to develop our projects XSD in class
  • Make a start to XPath on Wednesday

34
References
  • www.w3schools.com
  • Birbeck et.al. (2001) Professional XML 2nd
    Edition, Wrox Press Ltd. Birmingham, UK
Write a Comment
User Comments (0)
About PowerShow.com