XML Schema and Namespaces - PowerPoint PPT Presentation

1 / 61
About This Presentation
Title:

XML Schema and Namespaces

Description:

It is conceivable that several XML languages may want to use the same name for ... cname Julia Roberts /cname email jr_at_pw.com /email /person /addressBook ... – PowerPoint PPT presentation

Number of Views:225
Avg rating:3.0/5.0
Slides: 62
Provided by: ganeshsch
Category:

less

Transcript and Presenter's Notes

Title: XML Schema and Namespaces


1
XML Schema and Namespaces
2
XML Namespaces
  • Solve the problem   Tag name conflicts
  • XHTML   - HTML 4.0 written in XML
  • MathML   - Display mathematics equations
  • XHTML and MathML overlap   ,

3
Why do we need namespaces?
  • It is conceivable that several XML languages may
    want to use the same name for an element or
    attribute, e.g.
  • The meaning of is different in each
    context
  • Therefore we need some way of distinguishing
    between the two, Namespaces guarantee unique
    naming of XML elements and attributes

4
Namespaces
  • The W3C suggests the following
  • Associate each XML language with a URI
  • (e.g. www.xml.org)
  • because each URI is unique
  • A namespace is the collection of elements
    attributes associated with that URI
  • The URI doesn't need to point to an actual web
    page!!!

5
Namespaces . contd
  • The mechanism introduces three new concepts to
    XML
  • Namespace URI
  • Prefix
  • Qualified name

6
Example
7
Namespace URIs
  • 0'
  • Order
  • .com'
  • Large Oak
  • 2.00 m
  • 1.45 m
  • xmlnsprefixURI

8
Default Namespace
  • Order
  • m'
  • Large Oak
  • 2.00 m
  • 1.45 m

9
Prefixes
  • '
  • Order
  • m'
  • Large Oak
  • 2.00 m
  • 1.45 m
  • hx is the prefix bound to the html namespace
  • fx is the prefix bound to the furnitureML
  • namespace

10
Local Names
  • html, table, tr, td
  • furniture, table
  • '
  • Order
  • '
  • Large Oak
  • 2.00 m
  • 1.45 m

11
Qualified Names
  • qName prefixlocalName
  • hxhtml, hxtable, hxtr, hxtd
  • fxfurniture, fxtable
  • '
  • Order
  • '
  • Large Oak
  • 2.00 m
  • 1.45 m

12
Scope
  • A Namespace comes into scope on the element it is
    declared and remains in scope until that element
    closes
  • It is in scope for all of children of that
    element
  • Unless the prefix associated with that namespace
    is (temporarily) overridden by another namespace
  • An overridden namespace comes back into scope
    when the one over-ridding it goes out of scope

13
Scope
  • /core

14
Scope
  • e
  • 20
  • 0.54
  • jon
  • xxxx

  • 1
  • ...
  • n

15
Scope
  • xmlnshttp//www.xml-cml.org/schema/cml2/core
  • xmlnsbhttp//www.w3.org/TR/MathML2/
  • 20
  • 0.54
  • jon
  • xxxx
  • 1
  • ...
  • n

16
Review
  • Namespaces
  • A collection of attribute and element names
    associated with some URI
  • Namespace URI
  • xmlnsprefixuri
  • Default Namespace
  • xmlnsuri
  • Scope
  • Namespace scope

17
Review .... contd
prefix
Local name
  • l2/core/

Namespace URI
Qualified name
18
XML Schemas
  • Schemas is a general term--DTDs are a form of
    XML schemas
  • According to the dictionary, a schema is a
    structured framework or plan
  • When we say XML Schemas, we usually mean the
    W3C XML Schema Language
  • This is also known as XML Schema Definition
    language, or XSD
  • Ill use XSD frequently, because its short
  • DTDs, and XML Schemas are all XML schema languages

19
Why XML Schemas?
  • DTDs provide a very weak specification language
  • You cant put any restrictions on text content
  • You have very little control over mixed content
    (text plus elements)
  • You have little control over ordering of elements
  • DTDs are written in a strange (non-XML) format
  • You need separate parsers for DTDs and XML
  • The XML Schema Definition language solves these
    problems
  • XSD gives you much more control over structure
    and content
  • XSD is written in XML

20
Why not XML schemas?
  • DTDs have been around longer than XSD
  • Therefore they are more widely used
  • Also, more tools support them
  • XSD is very verbose, even by XML standards
  • More advanced XML Schema instructions can be
    non-intuitive and confusing
  • Nevertheless, XSD is not likely to go away quickly

21
The XSD document
  • The file extension is .xsd
  • The root element is
  • The XSD starts like this
  • xmlnsxs"http//www.w3.rg/2001/XMLSchema"

22
  • The element may have attributes
  • xmlnsxs"http//www.w3.org/2001/XMLSchema"
  • This is necessary to specify where all our XSD
    tags are defined
  • elementFormDefault"qualified"
  • This means that all XML elements must be
    qualified (use a namespace)
  • It is highly desirable to qualify all elements,
    or problems will arise when another schema is
    added

23
XML Schema
  • To define a "class" of XML Documents
  • "instance document" - XML document that conforms
    to a particular schema
  • W3C XML Shema Specification
  • XML Schema Part 0 Primerhttp//www.w3.org/TR/xml
    schema-0/
  • XML Schema Part 1 Structures"to express
    syntactic, structural and value constraints
    applicable to its document instances"http//www.w
    3.org/TR/xmlschema-1/
  • XML Schema Part 2 Datatypes"higher degree of
    type checking to ensure robustness in document
    understanding and data interchange"http//www.w3.
    org/TR/xmlschema-2/

24
Complete Example
  • Document
  • John Punin
  • puninj_at_cs.rpi.edu
  • Harrison Ford hford_at_famou
    s.org
  • Julia Roberts
  • jr_at_pw.com

25
Complete Example (2)
  • DTD

26
Complete Example (3)
  • XML Schema
  • Schema"
  • minOccurs"0 maxOccurs"unbounded"/

27
Parts of XML Schema Document
  • Root element schema element
  • Schema" ...

28
Parts of XML Schema Document (2)
  • Named Types
  • Simple Type - only values
  • John Punin
  • Complex Type - elements and/or attributes
  • John Punin
  • puninj_at_cs.rpi.edu


29
Simple and complex elements
  • A simple element is one that contains text and
    nothing else
  • A simple element cannot have attributes
  • A simple element cannot contain other elements
  • A simple element cannot be empty
  • However, the text can be of many different types,
    and may have various restrictions applied to it
  • If an element isnt simple, its complex
  • A complex element may have attributes
  • A complex element may be empty, or it may contain
    text, other elements, or both text and other
    elements

30
Defining a simple element
  • A simple element is defined as name"name" type"type" /where
  • name is the name of the element
  • the most common values for type are
    xsboolean xsinteger xsdate xsstring
    xsdecimal xstime
  • Other attributes a simple element may have
  • default"default value if no other value is
    specified
  • fixed"value" no other value may be specified

31
Simple Types
  • string - "Programming XML in Java"
  • integer - 32, -45
  • decimal - 12.4, -2.3
  • float - 12.4, 3.6E-5, INF, NaN
  • boolean - true, false
  • time - 152300.000-0400
  • date - 2001-02-18
  • ID - same as in DTD
  • NMTOKEN - same as in DTD

32
Simple Type Examples

33
Simple Type Examples (2)
  • Derived type'
  • Using the enumeration facetname"myWeek"
  • .....

34
Restrictions, or facets
  • The general form for putting a restriction on a
    text value is
  • (or
    xsattribute)
    ... the restrictions ...
  • For example
  • base"xsinteger"
    value"0"
    value"140"

35
Restrictions on numbers
  • minInclusive -- number must be the given value
  • minExclusive -- number must be the given value
  • maxInclusive -- number must be the given value
  • maxExclusive -- number must be
  • totalDigits -- number must have exactly value
    digits
  • fractionDigits -- number must have no more than
    value digits after the decimal point

36
Restrictions on strings
  • length -- the string must contain exactly value
    characters
  • minLength -- the string must contain at least
    value characters
  • maxLength -- the string must contain no more than
    value characters
  • pattern -- the value is a regular expression that
    the string must match
  • whiteSpace -- not really a restriction--tells
    what to do with whitespace
  • value"preserve" Keep all whitespace
  • value"replace" Change all whitespace
    characters to spaces
  • value"collapse" Remove leading and trailing
    whitespace, and replace all sequences of
    whitespace with a single space

37
Enumeration
  • An enumeration restricts the value to be one of a
    fixed set of values
  • Example








  • nt

38
Complex elements
  • A complex element is defined as name"name"
    ... information about the complex type...
  • Example

    type"xsstring" / name"lastName" type"xsstring" /

  • says that elements must occur in
    this order
  • Remember that attributes are always simple types

39
Complex Types
  • (title?,firstname,lastname)
  • maxOccurs"unbounded"/

40
Complex Types (2)
  • Derived Type by Extension(title?, firstname,
    lastname, genMark?)


41
Complex Types (3)
  • Derived Type by Restriction(firstname,
    lastname)

42
A Simple Example
  • DTD
  • XML Schema
  • ref"name"/
  • use"required"/
  • XML Document
  • The deadline of homework 1 is
    Nov 10 2006

43
xssequence
  • Weve already seen an example of a complex type
    whose elements must occur in a specific order
  • sequencetype"xsstring" /type"xsstring" /e

44
xsall
  • xsall allows elements to appear in any order
  • alltype"xsstring" /type"xsstring" /
  • Despite the name, the members of an xsall group
    can occur once or not at all
  • You can use minOccurs"0" to specify that an
    element is optional (default value is 1)
  • In this context, maxOccurs is always 1

45
Referencing
  • Once you have defined an element or attribute
    (with name"..."), you can refer to it with
    ref"..."
  • Example
  • all
    type"xsstring" /
    type"xsstring" /
    xselement
  • Or just

46
Compositors
  • Sequence
  • (title?,firstname,lastname)
  • maxOccurs"unbounded"/

47
Compositors (2)
  • Choice
  • (PRODUCT, NUMBER, (PRICE CHARGEACCT SAMPLE))

48
Groups
  • (PRODUCT, NUMBER, (PRICE CHARGEACCT SAMPLE))

49
Elements
  • Elements with Simple typesname"cname" type"xsdstring"/
  • Elements with Anonymous Complex types
  • Elements with Named Complex typese name"record"

50
References to Elements
  • DTD
  • XML Schema
  • maxOccurs"unbounded"/

51
Attributes and Complex Types from Simple Types
  • DTD
  • XML Schema
  • type"xsdstring"/
  • XML Instance Documentlanguage"Spanish" Hola!

52
Empty Content
  • icon"/

53
Empty elements
  • Empty elements are (ridiculously) complex

  • base"xsanyType"/ name"count" type"xsinteger"/

54
Text element with attributes
  • If a text element has attributes, it is no longer
    a simple type
  • xssimpleContent
    base"xsinteger"
    type"xsinteger"
    ent

55
Mixed elements
  • Mixed elements may contain both text and elements
  • We add mixed"true" to the xscomplexType element
  • The text itself is not mentioned in the element,
    and may go anywhere (it is basically ignored)

  • name"someName
    type"xsanyType"/ xType

56
Extensions
  • You can base a complex type on another complex
    type

  • base"otherType" ...new stuff...


57
Predefined date and time types
  • xsdate -- A date in the format CCYY-MM-DD, for
    example, 2002-11-05
  • xstime -- A date in the format hhmmss (hours,
    minutes, seconds)
  • xsdateTime -- Format is CCYY-MM-DDThhmmss
  • The T is part of the syntax
  • Allowable restrictions on dates and times
  • enumeration, minInclusive, minExclusive,
    maxInclusive, maxExclusive, pattern, whiteSpace

58
Global and local definitions
  • Elements declared at the top level of a
    are available for use throughout the
    schema
  • Elements declared within a xscomplexType are
    local to that type
  • Thus, inypetype"xsstring" /type"xsstring" /eelements firstName and lastName
    are only locally declared
  • The order of declarations at the top level of a
    do not specify the order in the XML data
    document

59
Declaration and use
  • So far weve been talking about how to declare
    types, not how to use them
  • To use a type we have declared, use it as the
    value of type"..."
  • Examples
  • Scope is important you cannot use a type if is
    local to some other type

60
Defining an attribute
  • Attributes themselves are always declared as
    simple types
  • An attribute is defined as name"name" type"type" /where name and type
    are the same as for xselement
  • Other attributes a simple element may have
  • default"default valueif no other value is
    specified
  • fixed"value no other value may be specified
  • use"optional the attribute is not required
    (default)
  • use"required the attribute must be present

61
Attribute Groups
  • DTD
  • src CDATA REQUIRED
  • width CDATA IMPLIED
  • height CDATA IMPLIED
  • XML Schema
  • use"required"/
  • use"required"/


Write a Comment
User Comments (0)
About PowerShow.com