XML - PowerPoint PPT Presentation

About This Presentation
Title:

XML

Description:

... consists of its name (tag), and a parenthesized description of any nested tags. ... name = the tag-name of the element being defined. type = the type of the ... – PowerPoint PPT presentation

Number of Views:16
Avg rating:3.0/5.0
Slides: 45
Provided by: jeff475
Learn more at: https://crab.rutgers.edu
Category:
Tags: xml | name | tags

less

Transcript and Presenter's Notes

Title: XML


1
XML
  • Document Type Definitions
  • XML Schema

2
Well-Formed and Valid XML
  • Well-Formed XML allows you to invent your own
    tags.
  • Valid XML conforms to a certain DTD.

3
Well-Formed XML
  • Start the document with a declaration, surrounded
    by lt?xml ?gt .
  • Normal declaration is
  • lt?xml version 1.0 standalone yes ?gt
  • standalone no DTD provided.
  • Balance of document is a root tag surrounding
    nested tags.

4
Tags
  • Tags are normally matched pairs, as ltFOOgt
    lt/FOOgt.
  • Unmatched tags also allowed, as ltFOO/gt
  • Tags may be nested arbitrarily.
  • XML tags are case-sensitive.

5
Example Well-Formed XML
  • lt?xml version 1.0 standalone yes ?gt
  • ltBARSgt
  • ltBARgtltNAMEgtJoes Barlt/NAMEgt
  • ltBEERgtltNAMEgtBudlt/NAMEgt
  • ltPRICEgt2.50lt/PRICEgtlt/BEERgt
  • ltBEERgtltNAMEgtMillerlt/NAMEgt
  • ltPRICEgt3.00lt/PRICEgtlt/BEERgt
  • lt/BARgt
  • ltBARgt
  • lt/BARSgt

6
DTD Structure
  • lt!DOCTYPE ltroot taggt
  • lt!ELEMENT ltnamegt(ltcomponentsgt)gt
  • . . . more elements . . .
  • gt

7
DTD Elements
  • The description of an element consists of its
    name (tag), and a parenthesized description of
    any nested tags.
  • Includes order of subtags and their multiplicity.
  • Leaves (text elements) have PCDATA (Parsed
    Character DATA ) in place of nested tags.

8
Example DTD
  • lt!DOCTYPE BARS
  • lt!ELEMENT BARS (BAR)gt
  • lt!ELEMENT BAR (NAME, BEER)gt
  • lt!ELEMENT NAME (PCDATA)gt
  • lt!ELEMENT BEER (NAME, PRICE)gt
  • lt!ELEMENT PRICE (PCDATA)gt
  • gt

9
Element Descriptions
  • Subtags must appear in order shown.
  • A tag may be followed by a symbol to indicate its
    multiplicity.
  • zero or more.
  • one or more.
  • ? zero or one.
  • Symbol can connect alternative sequences of
    tags.

10
Example Element Description
  • A name is an optional title (e.g., Prof.), a
    first name, and a last name, in that order, or it
    is an IP address
  • lt!ELEMENT NAME (
  • (TITLE?, FIRST, LAST) IPADDR
  • )gt

11
Use of DTDs
  • Set standalone no.
  • Either
  • Include the DTD as a preamble of the XML
    document, or
  • Follow DOCTYPE and the ltroot taggt by SYSTEM and a
    path to the file where the DTD can be found.

12
Example (a)
  • lt?xml version 1.0 standalone no ?gt
  • lt!DOCTYPE BARS
  • lt!ELEMENT BARS (BAR)gt
  • lt!ELEMENT BAR (NAME, BEER)gt
  • lt!ELEMENT NAME (PCDATA)gt
  • lt!ELEMENT BEER (NAME, PRICE)gt
  • lt!ELEMENT PRICE (PCDATA)gt
  • gt
  • ltBARSgt
  • ltBARgtltNAMEgtJoes Barlt/NAMEgt
  • ltBEERgtltNAMEgtBudlt/NAMEgt ltPRICEgt2.50lt/PRICEgtlt/BEER
    gt
  • ltBEERgtltNAMEgtMillerlt/NAMEgt ltPRICEgt3.00lt/PRICEgtlt/B
    EERgt
  • lt/BARgt
  • ltBARgt
  • lt/BARSgt

13
Example (b)
  • Assume the BARS DTD is in file bar.dtd.
  • lt?xml version 1.0 standalone no ?gt
  • lt!DOCTYPE BARS SYSTEM bar.dtdgt
  • ltBARSgt
  • ltBARgtltNAMEgtJoes Barlt/NAMEgt
  • ltBEERgtltNAMEgtBudlt/NAMEgt
  • ltPRICEgt2.50lt/PRICEgtlt/BEERgt
  • ltBEERgtltNAMEgtMillerlt/NAMEgt
  • ltPRICEgt3.00lt/PRICEgtlt/BEERgt
  • lt/BARgt
  • ltBARgt
  • lt/BARSgt

14
Attributes
  • Opening tags in XML can have attributes.
  • In a DTD,
  • lt!ATTLIST E . . . gt
  • declares attributes for element E, along with
    its datatype.

15
Example Attributes
  • Bars can have an attribute kind, a character
    string describing the bar.
  • lt!ELEMENT BAR (NAME BEER)gt
  • lt!ATTLIST BAR kind CDATA IMPLIEDgt

16
Example Attribute Use
  • In a document that allows BAR tags, we might see
  • ltBAR kind sushigt
  • ltNAMEgtHommaslt/NAMEgt
  • ltBEERgtltNAMEgtSapporolt/NAMEgt
  • ltPRICEgt5.00lt/PRICEgtlt/BEERgt
  • ...
  • lt/BARgt

17
IDs and IDREFs
  • Attributes can be pointers from one object to
    another.
  • Compare to HTMLs NAME foo and HREF foo.
  • Allows the structure of an XML document to be a
    general graph, rather than just a tree.

18
Creating IDs
  • Give an element E an attribute A of type ID.
  • When using tag ltE gt in an XML document, give its
    attribute A a unique value.
  • Example
  • ltE A xyzgt

19
Creating IDREFs
  • To allow elements of type F to refer to another
    element with an ID attribute, give F an
    attribute of type IDREF.
  • Or, let the attribute have type IDREFS, so the F
    -element can refer to any number of other
    elements.

20
Example IDs and IDREFs
  • A new BARS DTD includes both BAR and BEER
    subelements.
  • BARS and BEERS have ID attributes name.
  • BARS have SELLS subelements, consisting of a
    number (the price of one beer) and an IDREF
    theBeer leading to that beer.
  • BEERS have attribute soldBy, which is an IDREFS
    leading to all the bars that sell it.

21
The DTD
  • lt!DOCTYPE BARS
  • lt!ELEMENT BARS (BAR, BEER)gt
  • lt!ELEMENT BAR (SELLS)gt
  • lt!ATTLIST BAR name ID REQUIREDgt
  • lt!ELEMENT SELLS (PCDATA)gt
  • lt!ATTLIST SELLS theBeer IDREF REQUIREDgt
  • lt!ELEMENT BEER EMPTYgt
  • lt!ATTLIST BEER name ID REQUIREDgt
  • lt!ATTLIST BEER soldBy IDREFS IMPLIEDgt
  • gt

22
Example A Document
  • ltBARSgt
  • ltBAR name JoesBargt
  • ltSELLS theBeer Budgt2.50lt/SELLSgt
  • ltSELLS theBeer Millergt3.00lt/SELLSgt
  • lt/BARgt
  • ltBEER name Bud soldBy JoesBar
  • SuesBar /gt
  • lt/BARSgt

23
Empty Elements
  • We can do all the work of an element in its
    attributes.
  • Like BEER in previous example.
  • Another example SELLS elements could have
    attribute price rather than a value that is a
    price.

24
Example Empty Element
  • In the DTD, declare
  • lt!ELEMENT SELLS EMPTYgt
  • lt!ATTLIST SELLS theBeer IDREF REQUIREDgt
  • lt!ATTLIST SELLS price CDATA REQUIREDgt
  • Example use
  • ltSELLS theBeer Bud price 2.50 /gt

25
XML Schema
  • A more powerful way to describe the structure of
    XML documents.
  • XML-Schema declarations are themselves XML
    documents.
  • They describe elements and the things doing the
    describing are also elements.

26
Structure of an XML-Schema Document
  • lt? xml version ?gt
  • ltxsschema xmlnsxs
  • http//www.w3.org/2001/XMLschemagt
  • . . .
  • lt/xsschemagt

27
The xselement Element
  • Has attributes
  • name the tag-name of the element being defined.
  • type the type of the element.
  • Could be an XML-Schema type, e.g., xsstring.
  • Or the name of a type defined in the document
    itself.

28
Example xselement
  • ltxselement name NAME
  • type xsstring /gt
  • Describes elements such as
  • ltNAMEgtJoes Barlt/NAMEgt

29
Complex Types
  • To describe elements that consist of subelements,
    we use xscomplexType.
  • Attribute name gives a name to the type.
  • Typical subelement of a complex type is
    xssequence, which itself has a sequence of
    xselement subelements.
  • Use minOccurs and maxOccurs attributes to control
    the number of occurrences of an xselement.

30
Example a Type for Beers
  • ltxscomplexType name beerTypegt
  • ltxssequencegt
  • ltxselement name NAME
  • type xsstring
  • minOccurs 1 maxOccurs 1 /gt
  • ltxselement name PRICE
  • type xsfloat
  • minOccurs 0 maxOccurs 1 /gt
  • lt/xssequencegt
  • lt/xscomplexTypegt

31
An Element of Type beerType
  • ltxxxgt
  • ltNAMEgtBudlt/NAMEgt
  • ltPRICEgt2.50lt/PRICEgt
  • lt/xxxgt

32
Example a Type for Bars
  • ltxscomplexType name barTypegt
  • ltxssequencegt
  • ltxselement name NAME
  • type xsstring
  • minOccurs 1 maxOccurs 1 /gt
  • ltxselement name BEER
  • type beerType
  • minOccurs 0 maxOccurs unbounded /gt
  • lt/xssequencegt
  • lt/xscomplexTypegt

33
xsattribute
  • xsattribute elements can be used within a
    complex type to indicate attributes of elements
    of that type.
  • attributes of xsattribute
  • name and type as for xs.element.
  • use required or optional.

34
Example xsattribute
  • ltxscomplexType name beerTypegt
  • ltxsattribute name name
  • type xsstring
  • use required /gt
  • ltxsattribute name price
  • type xsfloat
  • use optional /gt
  • lt/xscomplexTypegt

35
An Element of This New Type beerType
  • ltxxx name Bud
  • price 2.50 /gt

36
Restricted Simple Types
  • xssimpleType can describe enumerations and
    range-restricted base types.
  • name is an attribute
  • xsrestriction is a subelement.

37
Restrictions
  • Attribute base gives the simple type to be
    restricted, e.g., xsinteger.
  • xsmin, maxInclusive, Exclusive are four
    attributes that can give a lower or upper bound
    on a numerical range.
  • xsenumeration is a subelement with attribute
    value that allows enumerated types.

38
Example license Attribute for BAR
  • ltxssimpleType name licensegt
  • ltxsrestriction base xsstringgt
  • ltxsenumeration value Full /gt
  • ltxsenumeration value Beer only /gt
  • ltxsenumeration value Sushi /gt
  • lt/xsrestrictiongt
  • lt/xssimpleTypegt

39
Example Prices in Range 1,5)
  • ltxssimpleType name pricegt
  • ltxsrestriction
  • base xsfloat
  • minInclusive 1.00
  • maxExclusive 5.00 /gt
  • lt/xssimpleTypegt

40
Keys in XML Schema
  • An xselement can have an xskey subelement.
  • Meaning within this element, all subelements
    reached by a certain selector path will have
    unique values for a certain combination of
    fields.
  • Example within one BAR element, the name
    attribute of a BEER element is unique.

41
Example Key
  • ltxselement name BAR gt
  • . . .
  • ltxskey name barKeygt
  • ltxsselector xpath BEER /gt
  • ltxsfield xpath _at_name /gt
  • lt/xskeygt
  • . . .
  • lt/xselementgt

42
Foreign Keys
  • An xskeyref subelement within an xselement says
    that within this element, certain values (defined
    by selector and field(s), as for keys) must
    appear as values of a certain key.

43
Example Foreign Key
  • Suppose that we have declared that subelement
    NAME of BAR is a key for BARS.
  • The name of the key is barKey.
  • We wish to declare DRINKER elements that have
    FREQ subelements. An attribute bar of FREQ is a
    foreign key, referring to the NAME of a BAR.

44
Example Foreign Key in XML Schema
  • ltxselement name DRINKERS
  • . . .
  • ltxskeyref name barRef
  • refers barKey
  • ltxsselector xpath
    DRINKER/FREQ /gt
  • ltxsfield xpath _at_bar /gt
  • lt/xskeyrefgt
  • lt/xselementgt
Write a Comment
User Comments (0)
About PowerShow.com