Download presentation
Presentation is loading. Please wait.
1
1 XML Document Type Definitions XML Schema
2
2 Well-Formed and Valid XML uWell-Formed XML allows you to invent your own tags. uValid XML conforms to a certain DTD.
3
3 Well-Formed XML uStart the document with a declaration, surrounded by. uNormal declaration is: “standalone” = “no DTD provided.” uBalance of document is a root tag surrounding nested tags.
4
4 Tags uTags are normally matched pairs, as …. uUnmatched tags also allowed, as uTags may be nested arbitrarily. uXML tags are case-sensitive.
5
5 Example: Well-Formed XML Joe’s Bar Bud 2.50 Miller 3.00 … A NAME subelement A BEER subelement Root tag Tags surrounding a BEER element
6
6 DTD Structure [ ( )>... more elements... ]>
7
7 DTD Elements uThe description of an element consists of its name (tag), and a parenthesized description of any nested tags. wIncludes order of subtags and their multiplicity. uLeaves (text elements) have #PCDATA (Parsed Character DATA ) in place of nested tags.
8
8 Example: DTD <!DOCTYPE BARS [ ]> A BARS object has zero or more BAR’s nested within. A BAR has one NAME and one or more BEER subobjects. A BEER has a NAME and a PRICE. NAME and PRICE are text.
9
9 Element Descriptions uSubtags must appear in order shown. uA tag may be followed by a symbol to indicate its multiplicity. w* = zero or more. w+ = one or more. w? = zero or one. uSymbol | can connect alternative sequences of tags.
10
10 Example: Element Description uA name is an optional title (e.g., “Prof.”), a first name, and a last name, in that order, or it is an IP address: <!ELEMENT NAME ( (TITLE?, FIRST, LAST) | IPADDR )>
11
11 Use of DTD’s 1.Set standalone = “no”. 2.Either: a)Include the DTD as a preamble of the XML document, or b)Follow DOCTYPE and the by SYSTEM and a path to the file where the DTD can be found.
12
12 Example: (a) <!DOCTYPE BARS [ ]> Joe’s Bar Bud 2.50 Miller 3.00 … The DTD The document
13
13 Example: (b) uAssume the BARS DTD is in file bar.dtd. Joe’s Bar Bud 2.50 Miller 3.00 … Get the DTD from the file bar.dtd
14
14 Attributes uOpening tags in XML can have attributes. uIn a DTD, declares attributes for element E, along with its datatype.
15
15 Example: Attributes Bars can have an attribute kind, a character string describing the bar. Character string type; no tags Attribute is optional opposite: #REQUIRED
16
16 Example: Attribute Use uIn a document that allows BAR tags, we might see: Homma’s Sapporo 5.00...
17
17 ID’s and IDREF’s uAttributes can be pointers from one object to another. wCompare to HTML’s NAME = ”foo” and HREF = ”#foo”. uAllows the structure of an XML document to be a general graph, rather than just a tree.
18
18 Creating ID’s uGive an element E an attribute A of type ID. uWhen using tag in an XML document, give its attribute A a unique value. uExample:
19
19 Creating IDREF’s uTo allow elements of type F to refer to another element with an ID attribute, give F an attribute of type IDREF. uOr, let the attribute have type IDREFS, so the F -element can refer to any number of other elements.
20
20 Example: ID’s and IDREF’s uA 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
21 The DTD <!DOCTYPE BARS [ ]> Beer elements have an ID attribute called name, and a soldBy attribute that is a set of Bar names. SELLS elements have a number (the price) and one reference to a beer. Bar elements have name as an ID attribute and have one or more SELLS subelements. Explained next
22
22 Example: A Document 2.50 3.00 … <BEER name = ”Bud” soldBy = ”JoesBar SuesBar …” /> …
23
23 Empty Elements uWe can do all the work of an element in its attributes. wLike BEER in previous example. Another example: SELLS elements could have attribute price rather than a value that is a price.
24
24 Example: Empty Element uIn the DTD, declare: uExample use: Note exception to “matching tags” rule
25
25 XML Schema uA more powerful way to describe the structure of XML documents. uXML-Schema declarations are themselves XML documents. wThey describe “elements” and the things doing the describing are also “elements.”
26
26 Structure of an XML-Schema Document <xs:schema xmlns:xs = ”http://www.w3.org/2001/XMLschema”>... Defines ”xs” to be the namespace described in the URL shown. Any string in place of ”xs” is OK. So uses of ”xs” within the schema element refer to tags from this namespace.
27
27 The xs:element Element uHas attributes: 1.name = the tag-name of the element being defined. 2.type = the type of the element. uCould be an XML-Schema type, e.g., xs:string. uOr the name of a type defined in the document itself.
28
28 Example: xs:element <xs:element name = ”NAME” type = ”xs:string” /> uDescribes elements such as Joe’s Bar
29
29 Complex Types uTo describe elements that consist of subelements, we use xs:complexType. wAttribute name gives a name to the type. uTypical subelement of a complex type is xs:sequence, which itself has a sequence of xs:element subelements. wUse minOccurs and maxOccurs attributes to control the number of occurrences of an xs:element.
30
30 Example: a Type for Beers <xs:element name = ”NAME” type = ”xs:string” minOccurs = ”1” maxOccurs = ”1” /> <xs:element name = ”PRICE” type = ”xs:float” minOccurs = ”0” maxOccurs = ”1” /> Exactly one occurrence Like ? in a DTD
31
31 An Element of Type beerType Bud 2.50 We don’t know the name of the element of this type.
32
32 Example: a Type for Bars <xs:element name = ”NAME” type = ”xs:string” minOccurs = ”1” maxOccurs = ”1” /> <xs:element name = ”BEER” type = ”beerType” minOccurs = ”0” maxOccurs = ”unbounded” /> Like * in a DTD
33
33 xs:attribute uxs:attribute elements can be used within a complex type to indicate attributes of elements of that type. uattributes of xs:attribute: wname and type as for xs.element. wuse = ”required” or ”optional”.
34
34 Example: xs:attribute <xs:attribute name = ”name” type = ”xs:string” use = ”required” /> <xs:attribute name = ”price” type = ”xs:float” use = ”optional” />
35
35 An Element of This New Type beerType <xxx name = ”Bud” price = ”2.50” /> We still don’t know the element name. The element is empty, since there are no declared subelements.
36
36 Restricted Simple Types uxs:simpleType can describe enumerations and range-restricted base types. uname is an attribute u xs:restriction is a subelement.
37
37 Restrictions uAttribute base gives the simple type to be restricted, e.g., xs:integer. uxs:{min, max}{Inclusive, Exclusive} are four attributes that can give a lower or upper bound on a numerical range. uxs:enumeration is a subelement with attribute value that allows enumerated types.
38
38 Example: license Attribute for BAR
39
39 Example: Prices in Range [1,5) <xs:restriction base = ”xs:float” minInclusive = ”1.00” maxExclusive = ”5.00” />
40
40 Keys in XML Schema uAn xs:element can have an xs:key subelement. uMeaning: within this element, all subelements reached by a certain selector path will have unique values for a certain combination of fields. uExample: within one BAR element, the name attribute of a BEER element is unique.
41
41 Example: Key...... XPath is a query language for XML. All we need to know here is that a path is a sequence of tags separated by /. And @ indicates an attribute rather than a tag.
42
42 Foreign Keys uAn xs:keyref subelement within an xs:element 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
43 Example: Foreign Key uSuppose that we have declared that subelement NAME of BAR is a key for BARS. wThe name of the key is barKey. uWe 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
44 Example: Foreign Key in XML Schema <xs:element name = ”DRINKERS”... <xs:keyref name = ”barRef” refers = ”barKey”
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.