Presentation is loading. Please wait.

Presentation is loading. Please wait.

XML Validation II Schemas Robin Burke ECT 360. Outline Namespaces Documents  Data types XML Schemas Elements Attributes Derived data types RELAX NG.

Similar presentations


Presentation on theme: "XML Validation II Schemas Robin Burke ECT 360. Outline Namespaces Documents  Data types XML Schemas Elements Attributes Derived data types RELAX NG."— Presentation transcript:

1 XML Validation II Schemas Robin Burke ECT 360

2 Outline Namespaces Documents  Data types XML Schemas Elements Attributes Derived data types RELAX NG

3 XML so far Languages defined by DTDs names assigned by designers OK for standalone systems Doesn't have The ability to handle naming conflicts The ability to partition work among different developers

4 Namespaces A way to identify a set of labels element / attribute names attribute values As belonging to a particular application

5 Example recordings title artist group | artist-name+ date label artworks title artist date exhibit books title author date publisher

6 Problem Want to create a list of items related to 50s Beat-era culture includes music, art, literature Could create a new DTD better to reuse existing ones

7 Namespace idea Associate a short prefix with an application Schema or DTD Use the prefix with a colon to "qualify" names music:artist art:artist book:author

8 Namespace idea, cont'd A namespace is an association between a set of names a unique identifier (URI) a prefix used to identify them

9 Namespace declaration Standalone Part of element in this case, no prefix

10 Namespace URI Not a URL there is no resource at the given location just a unique identifier URL-like identifiers are good associated with an organization must be unique on the Internet

11 Example DTDs Document Problem how to import the namespaces?

12 Solution Fully-qualified names everywhere yuk! DTDs & namespaces don't work well together

13 XML so far Languages defined by DTDs contain text elements string attributes OK for text documents Not enough for Databases Business process integration

14 Other DTD problems Not XML different syntax different processor No support for namespaces

15 Solution Write language definition in XML Allow more control over document contents XML document becomes a complex data type XML language definition becomes complex data type specification

16 XML Schema Always a separate document no internal option Written in XML very verbose Can be complex

17 Schemas and namespaces A schema uses elements from one application the XML Schema language to define another Namespaces are necessary Namespaces apply to elements not values Namespace of element assumed to apply to attributes can have attributes from different namespaces

18 Example 1, XML Jane Doe A John Doe B

19 Example 1, DTD <!ATTLIST grades assignment CDATA #IMPLIED> <!ATTLIST student id CDATA #REQUIRED>

20 Data types grades a collection of items of type grade can never have more than 40 students grade a structure containing a student and an assigned grade student a structure containing an id and some text probably should constrain the student id assigned-grade is text probably should constrain to A-D,F,I

21 Built-in types Part of the schema language Base types 19 fundamental types Examples: string, decimal Derived types 25 more types that use the base types Examples: ID, positiveInteger

22 Built-in types, cont'd

23 To declare an element <xs:element name="assigned-grade" type="string"> Equivalent to

24 Simple data type A renaming of an existing data type <xs:element name="assigned-grade" type="xs:string"> Or a restriction of a existing type strings beginning with "D" more on this later

25 Complex datatype compositor element declarations attribute declarations

26 Compositor sequence choice all

27 Sequence compositor like "," in DTD DTD Schema

28 Elements in sequences Can specify optional / # of occurrences ? * + What about...

29 Choice compositor like "|" in DTD DTD Schema

30 All compositor no simple DTD equivalent DTD Schema

31 Nesting Compositors can be combined DTD Schema

32 Example

33 Local naming Suppose we want to reuse an element name different place in the structure Example not a legal DTD schema?

34 Using namespaces Schema must say to use schema namespace what namespace it is defining targetNamespace Document must say that it is using the Schema Instance namespace what namespace(s) it is using what prefix(es) are used where to find the relevant schemas

35 Multi-schema documents Possible to validate multi-schema documents Must use any element to import namespace can't restrict to certain elements

36 Attributes DTD attribute types CDATA, enumeration, token Schema can be any of the basic or derived types can also be user-defined types Declaration <xs:attribute name="x" type="xs:string" use="required" default="abc" />

37 Attribute declaration Part of complex type follows compositor (one exception) Declaration What if the attribute is a more complex type itself? we'll get to that

38 Example grades element? add homework attribute

39 Exception: simple content If an element has "simple content" no compositor used instead simpleContent element and extension to declare type of the content

40 Example <!ATTLIST student id CDATA #REQUIRED > <xs:attribute name="id" type="xs:string" use="required"/>

41 How to read this student is a complex type it is not simply a renaming of an existing type its content is simple being of only one type string but with an attribute id of type string which is required

42 Standalone types A type can stand outside of an element definition must have a name Used in element definition

43 Deriving types DTDs do not allow types restrictions beyond enumeration, CDATA, token for attributes PCDATA for content Schemas have built-in types also capability to create your own

44 Derivation operations list sequence of values union combine two types allowing either restriction placing limits on the legal values

45 List PN334-04 PN223-89 PQ1112-03 Must be separated by spaces probably more useful to do this with document structure partList -> partNo*

46 Union Allows data of either type to be used Example Bogus!

47 Restriction Most useful Allow design to state exactly what values are legal prices must be non-negative SSN must follow a certain pattern in-stock must yes or no etc.

48 Restriction, cont'd Restrict a base type according to "facets" Different facets available for different data types

49 Facets

50 Example: enumeration

51 Example: numeric

52 Example: pattern Regular expressions again derived from perl

53 Extended example Complete schema for grades

54 RELAX NG XML Schemas are big a lot of the page consists of / repeated element names RELAX NG created as an alternate validation language compact, non-XML syntax also XML syntax

55 Example element grades { element grade { element student { text }, element assigned-grade { text } }* } Equivalent to

56 Attributes element grades { element grade { element student { text, attribute id { text } }, element assigned-grade (text) }* attribute assignment { text } }

57 Types instead of { text } use appropriate built-in data type attribute age { xsd:positiveInteger } facets qualify with name / value pair attribute drinkingAge { xsd:positiveInteger { minInclusive="21" } }

58 What does this one say? element grade { element student...., { element assigned-grade { text { pattern="([A-D](\+|\-)?|F)" } | ( element assigned-grade { text "I" }, element reason { text } ) }

59 The point A schema language has two purposes lets the language designer state a design lets the system validate documents against that design Any language that serves this purposes can be used

60 Validation languages DTD SGML holdover ugly a dead-end fairly simple to express Schema complete extensible baroque unreadable RELAX NG readable esp. compact syntax more expressive than Schema fewer tools

61 Homework #3 Create a schema for "Grills.xml" Generate a schema for your "books.xml" file using XML Spy's "generate" feature edit generated schema

62 Next week CSS SVG an XML application for generating graphics online reading


Download ppt "XML Validation II Schemas Robin Burke ECT 360. Outline Namespaces Documents  Data types XML Schemas Elements Attributes Derived data types RELAX NG."

Similar presentations


Ads by Google