Presentation is loading. Please wait.

Presentation is loading. Please wait.

Schemas Deitel XML chapter 7 Peltzer, XML Language Mechanics and Applications (Addison Wesley) Chapter 4 – has much more on W3C schemas.

Similar presentations


Presentation on theme: "Schemas Deitel XML chapter 7 Peltzer, XML Language Mechanics and Applications (Addison Wesley) Chapter 4 – has much more on W3C schemas."— Presentation transcript:

1 Schemas Deitel XML chapter 7 Peltzer, XML Language Mechanics and Applications (Addison Wesley) Chapter 4 – has much more on W3C schemas

2 Schemas vs DTD DTDs are inherited from SGML (Standard Generalized Markup Language). They can’t be manipulated (searched or transformed into another format, like HTML) the way XML documents can, because DTDs are not XML. Schemas are XML. XML documents conforming to schema require validating parsers like DTDs do. Schemas themselves conform to DTDs which are bundled with the parser. Repositories of existing DTDs and Schema are available for download free.

3 Schemas DTDs define document structure, not content, so although 5 contains legal PCDATA, it can’t be checked to insure the content is numeric. Markup like Hello Bob would also be valid PCDATA. The application using this XML document would itself have to test if value were numeric and take appropriate action if the test failed. Schemas are XML documents conforming to DTDs and must be validated to be processed. Schema do not use EBNF but use XML syntax. Schema can be manipulated (eg., searched, or elements added or removed) as with any XML document. W3C XML Schema are not covered (much) in Deitel’s book, only MS Schema. Many W3C examples of schema are in Peltzer, XML Language Mechanics and Applications (Addison Wesley)

4 Schemas Schemas view xml docs as a collection of datatypes DTDs view xml docs as a single entitity W3C 2001 schema specification lists 44 datatypes. There are 19 primitive types and 25 built-in, derived types. User derived and built-in types are both defined using the simpleType definitions, which restrict the type of data that can appear as content for an attribute value or text-only element. A schema datatype has 3 components: a value space, a lexical space, a set of facets.

5 Examples 1.In learning XLM the value space and lexical space are both string. (the ‘value’ is the literal string “learning XML” and lexical space is the type string). 2.In 123 the value space is a set of literals (digits), the lexical representation might be a specified number of digit chars.

6 A simple schema for Author (saved as.xsd)

7 In validator

8 And an instance of the Author schema Dwight Peltzer Po Box 555 Oyster Bay NY 11771

9 Elements make up XML documents in MS Schema, ElementType defines an element. It contains attributes describing content, data type, name and so on for this element. MSXML (microsoft’s validating parser) is part of IE5, and is needed to build MS Schema. Element Schema is the root element for every MS Schema document.

10 facets Facets define the value space and properties for a specified data type. They consist of two types: fundamental and non- fundamental facets. fundamental facets define a type non-fundamental facets impose restrictions on the type by limiting the range

11 fundamental facets Equal – allows comparison Ordered- allows words to be placed in a predefined ordering Bounded- allows a lower and upper limit to be provided Cardinality- defines numeric relationship between occurrences of an entity (as in minOccurs=“0” maxOccurs=“unbounded” ) Recall, we used the “+” for this in DTDs. Numeric – a value can be classified as numeric or nonnumeric as in numeric(value=“true”) or numeric(value=“false”) Example…we might define isbn to consist of exactly 10 digit chars: <xs:simpleType name =“isbnType” xs:pattern value=“[0-9]{10}”/> Note- this is not precisely the way isbns are defined, since the last character might be alpha and provides a parity check

12 Euros…up to 10 decimal digits and exactly 2 decimal places <xs:simpleType name =“EuroDollarType” A document instance 55.63

13 Derived user types: use simpleType definitions and one of 3 methods: restriction, list and Union Restriction uses one or more constraining facets to restrict the value or lexical space for the base type. A postal code might use:

14 Derived user types: list List uses a predetermined itemType sequence of attributes to derive a new type. A “whitespace-delimited” list of decimal values for some lottery might be With a document instance of 94 33 12 76

15 Text version is not validated

16 Derived user types:union Union creates a datatype derived from more than one base type. A number of basetypes participate in the union. Here the two types could be any base types. This would enable using eg., string or int to define a month as in: Jan Feb Mar 1 2 3

17 A complex type xsd (see next slide for discussion of sequence)

18 Compositors Allow us to specify: 1. Sequential order of elements 2.Choice of elements 3.The ALL compositor allowing no restrictions for order and selection The previous slide used sequence.

19 Choice We might use, as part of a schema:

20 ALL ALL is similar to ANY

21 namespaces Xsd and xs are used interchangeably. Xs serves as the default prefix for all XSD schemas. There are 3 distinct namespaces 1.The XML schema namespace 2.The XML schema data type namespace 3.The XML schema instance namespace An example of the first appeared above as:

22 Global elements- anything declared before complexType is global (see below)

23 Global elements-document instance <Author xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="global.xsd"> String

24 Using a target namespace for your document - -- this binds the document to the schema Dwight Peltzer PO Box 555 Oyster Bay NY 11771 Addison Wesley Boston Massachusetts XML Language Mechanics 0-1-23458-0

25 target namespace for your document may be omitted. This means you are using built-in types and mapping all elements/attributes to the default namespace. You are then prevented from reusing locally declared elements.

26 In validator

27 The document instance would not then have a namespace <Author xmlns:xsi=http://www.w3.org.2001.XMLSchema-instance" xsi:noNamespaceSchemaLocation="Author.xsd"> Dwight Peltzer PO Box 555 Oyster Bay NY 11771 Addison Wesley Boston Massachusetts XML Language Mechanics 0-1-23458-0

28 Adding the target namespace to the schema root defines a namespace for your user-defined declarations <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.dpsoftware.com/namespaces/Author" xmlns="http://www.dpsoftware.com/namespaces/Author">

29 An example document <dp:Author xmlns:dp="http://www.dpsoftware.com/namespaces/Author" xmlns:xsi="http://www.w3.org/20011/XMLSchema-instance" xsi:schemaLocation="http://www.dpsoftware.com/namespaces/author/AuthorV1.xsd"> Dwight Peltzer Po Box 555 Oyster Bay NY 11771 Addison Wesley XML Language Mechanics 0-1-23458-0

30 W:\internet programming\validate_js.htm

31 Namespace prefix can be used to qualify each element in a doc <dp:Author xmlns:dp="http://www.dpsoftware.com/namespaces/author" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:SchemaLocation="http://www.dpsoftware.com/namespaces/author Author.xsd"> Dwight Peltzer PO Box 555 Oyster Bay NY 11771 XML Language Mechanics 0-1-23458-0

32 Russian doll model XML Dwight Peltzer

33 Salami slice model

34 Venetian blind model Venetian blind model uses elementFormDefault and attributeFormDefault to switch back and forth (hiding/exposing namespaces) in the document instance Comment describing your root element

35 All NamedType components in this xsd are reusable

36 ContentModel template- use type attribute to reference the named complex type definition /xs:complexType>

37 An ms schema <ElementType name = "message" content = "textOnly" model = "closed"> Text messages <ElementType name = "greeting" model = "closed" content = "mixed" order = "many"> <ElementType name = "myMessage" model = "closed" content = "eltOnly" order = "seq"> <element type = "greeting" minOccurs = "0" maxOccurs = "1"/> <element type = "message" minOccurs = "1" maxOccurs = "*"/>

38 schema elements xmlns specifies the default namespace for the Schema element and the elements it contains. Attribute value urn:... is the uri for this namespace. Microsoft’s xml parser recognizes element Schema and this namespace and validates the schema. Element Schema can contain only elements of ElementType for defining elements, AttributeType for their attributes and description for describing the element. This example specifies that element message may contain textOnly. The closed model attribute specifies that only elements declared in this schema may appear in conforming xml documents, anything else would invalidate the document. Element greeting has mixed content, indicating that both elements and character data may appear here. Order =“many” indicates that any number of message elements and text may be contained in the greeting.

39 a conforming xml document Welcome to XML Schema! This is the first message. This is the second message.

40 msxml validator

41 a well-formed but non- conforming xml document Welcome to XML Schema! This is a message that contains another message. This is the inner message.

42 using validator

43 Namespaces and declaring schema The namespace declaration xmlns=“…” references the schema being used. For MS Schema, the URI must begin with x-schema followed by a colon and the name of the schema document. Element greeting may have mixed content and in this example greeting marks up text and has a child message element.

44 Element attributes ElementType has attributes: content, dt:type, name, model and order. Element ElementType’s child elements are: description, datatype, element, group, AttributeType and attribute. Element element has attributes type, minOccurs, maxOccurs. Element group has attributes order, minOccurs, maxOccurs. Element AttributeType has attributes: default, dt:type, dt:values, name and required. Element attribute has attributes: default, type, required.

45 An example of AttributeType and attribute <ElementType name = "contact" content = "eltOnly" order = "seq" model = "closed">

46 An example of AttributeType and attribute (part2) <ElementType name = "name" content = "textOnly" model = "closed"/> <ElementType name = "address1" content = "textOnly" model = "closed"/> <ElementType name = "address2" content = "textOnly" model = "closed"/> <ElementType name = "city" content = "textOnly" model = "closed"/> <ElementType name = "state" content = "textOnly" model = "closed"/>

47 A conforming xml document Jane Doe 123 Main St. Sometown Somestate 12345 617-555-1234 978-555-4321

48 Contact.xml in validator

49 MS Schema datatypes DTD did not permit the specification of allowable datatypes (content) an element or attribute might contain. Namespace prefix dt is defined by the document author and assigned to urn:schemas-microsoft-com:datatypes Msdn.microsoft.com/xml/reference/schema/datatypes.asp has a complete list of types supported.

50 MS Schema datatypes boolean: 0 or 1 char: a character, “X” string: a sequence of char as in “XYZ” float and int: as in C or Java date: YYYY-MM-DD time:HH:MM:SS id: text which uniquely identifies an element or its attribute. idref: a reference to an id. enumeration: a series of values from which one is chosen.

51 using data types <Schema xmlns = "urn:schemas-microsoft-com:xml-data" xmlns:dt = "urn:schemas-microsoft-com:datatypes"> <ElementType name = "bookstore" content = "eltOnly" order = "many" model = "closed"> <ElementType name = "shipping" content = "eltOnly" order = "seq" model = "closed"> <AttributeType name = "shipID" dt:type = "id" required = "yes"/> <ElementType name = "duration" content = "textOnly" model = "closed" dt:type = "date"/> <ElementType name = "book" content = "textOnly" model = "closed" dt:type = "string">

52 using data types: a conforming xml document 2000-08-01 2000-08-20 Java How to Program 3rd edition. C How to Program 3rd edition. C++ How to Program 3rd edition.

53 id.xml in validator

54 Schema for an inventory database <Schema xmlns = "urn:schemas-microsoft-com:xml-data" xmlns:dt = "urn:schemas-microsoft-com:datatypes"> <ElementType name = "inventory" content = "eltOnly" model = "closed"> <ElementType name = "book" content = "eltOnly" order = "seq" model = "closed"> <AttributeType name = "isbn" dt:type = "string" required = "yes"/> <AttributeType name = "inStock" dt:type = "enumeration" dt:values = "yes no" default = "no"/>

55 inventory database continued <ElementType name = "name" content = "textOnly" model = "closed" dt:type = "string"/> <ElementType name = "price" content = "textOnly" model = "closed" dt:type = "float"/> <ElementType name = "quantity" content = "textOnly" dt:type = "int" model = "closed"/> <ElementType name = "available" content = "textOnly" dt:type = "date" model = "closed"/>

56 a conforming document: inventory.xml Java How to Program 3/e 68.00 200 Perl How to Program 68.00 2000-12-15

57 inventory in validator with one book node opened

58 Here’s what inventory looks like opened in IE

59 My own database schema <Schema xmlns = "urn:schemas-microsoft-com:xml-data" xmlns:dt = "urn:schemas-microsoft-com:datatypes"> <ElementType name = "classlist" content = "eltOnly" model = "closed"> <ElementType name = "student" content = "eltOnly" order = "seq" model = "closed"> <AttributeType name = "ssn" dt:type = "string" required = "yes"/> <AttributeType name = "Matriculated" dt:type = "enumeration" dt:values = "yes no" default = "no"/>

60 my own database schema continued <ElementType name = "name" content = "textOnly" model = "closed" dt:type = "string"/> <ElementType name = "year" content = "textOnly" model = "closed" dt:type = "string"/> <ElementType name = "address" content = "textOnly" dt:type = "string" model = "closed"/> <ElementType name ="credits" content = "textOnly" dt:type="int" model="closed"/> <ElementType name = "birthdate" content = "textOnly" dt:type = "date" model = "closed"/>

61 Check if your schema is well-formed in validator

62 an xml document satisfying this schema Bob Smith sophomore 123 Park St 15 1988-04-15

63 myclass has 4 students and is well-formed

64 Schema following w3c standards xsd file <xsd:element ref = "greeting" minOccurs = "0" maxOccurs = "1"/> <xsd:element ref = "message" minOccurs = "1" maxOccurs = "unbounded"/>

65 A conforming xml document <myMessage xmlns:xsd = "http://www.w3.org/2000/10/XMLSchema-in­ stance" xsd:noNamespaceSchemaLocation = "xml-schema.xsd"> Welcome to W3C XML Schema! This is a message. This is another message.

66 It gets errors in MS Validator

67 W3C schema example (previous slide) parsed by Xerces (not sure about weird chars in output) C:\xerces-1_2_0>java dom.DOMWriter intro3.xml intro3.xml: <myMessage xmlns:xsd="http://www.w3.org/2000/10/XMLSchema- inεûÅ?nce" xsd:noNamespaceSchemaLocation= "xml-schema.xsd"> Welcome to W3C XML Schema! This is a message. This is another message.

68 intro3.xml parsed again by Xerces using DOMCount2 C:\XERCES~1>DOMCount2.bat C:\XERCES~1>set PATH=%PATH%;c:\progra~1\java\jdk15~1.0_0\bin;C:\Pr ogra~1\Java\jdk15.0_0\bin C:\XERCES~1>set CLASSPATH=%CLASSPATH%;c:\xerces- 1_2_0\xerces.jar;c:\xerces-1_2_0 \xercesSamples.jar;c:\xerces-1_2_0\xerces.jar;c:\xerces- 1_2_0\xercesSamples.jar C:\xerces-1_2_0>java dom.DOMCount -p dom.wrappers.DOMParser intro3.xml intro3.xml: 90 ms (4 elems, 2 attrs, 0 spaces, 83 chars) C:\XERCES~1>


Download ppt "Schemas Deitel XML chapter 7 Peltzer, XML Language Mechanics and Applications (Addison Wesley) Chapter 4 – has much more on W3C schemas."

Similar presentations


Ads by Google