XML Schemas - Santa Clara University

XML Schemas - Santa Clara University

XML Schemas 1 Useful Links Schema tutorial links: http://www.w3schools.com/schema/default.asp http://www.xfront.com/ http://www.w3.org/TR/xmlschema-0/ 2 Why Schemas Enhanced datatypes 44+ versus 10

Can create your own datatypes Example: "This is a new type based on the string type and elements of this type must follow this pattern: ddddddd, where 'd' represents a digit". Written in the same syntax as instance documents: less syntax to remember Can extend or restrict a type (derive new type definitions on the basis of old ones) Can express sets, i.e., can define the child elements to occur in any order Can specify element content as being unique (keys on content) and uniqueness within a region Can define multiple elements with the same name but different content Can define substitutable elements - e.g., the "Book" element is substitutable for the "Publication" element. 3 Schemas and DTDs

One difference between XML Schemas and DTDs is that the XML Schema vocabulary is associated with a name (namespace). Likewise, the new vocabulary that you define must be associated with a name (namespace). With DTDs neither set of vocabulary is associated with a name (namespace). 4 Some XML Parsers with Schema Support A comparison of some XML parsers: http://webreference.com/xml/column22/2.html 5 Example BookCatalog.dtd

6 An Example Of A Schema Definition BookCatalog.xsd

7 BookCatalog.xml Billions Of Stars Susan Boggs 1983 1-5555-555-2 Anderson-Wells Adventures Of Freddie the Frog

John Smith 1977 0-444-4444-4 Kidder Publishing Co. 8 Schema namespace Schema element: All XML schemas have schema as the root element. XML Schema namespace: The elements and datatypes that are used to construct schemas, like, schema, element, complexType, sequence and string come from the http:///XMLSchema namespace 9 Using namespaces Example plants.xsd

10 Example plants.xml Daisy perennial 11 Example BookCatalog.xsd

12 BookCatalog.xml Billions Of Stars Susan Boggs 1983 1-5555-555-2 Anderson-Wells

Adventures Of Freddie the Frog John Smith 1977 0-444-4444-4 Kidder Publishing Co. 13 Simple vs Complex Types

A schema document can be divided into content types: Simple and complex types. Simple types: can contain only text. There are several simple builtin types String, Boolean, Float, Double, ID, IDREF, Entity etc. Derived data types: The built-in data types can be customized to control the content. Complex types: can contain elements that can contain other elements or attributes. Local vs global declarations: Schema components, elements, attributes, named simple and complex types, declared under xsd:schema element are considered globally declared. A global declaration has to be explicitly referenced. New elements can be declared within the context of a complex type, in which case they are considered locally declared. 14 Simple Types: exampleA.xsd

15 exampleA.xml Billions Of Stars 2000-01-02 1983 true

16 Deriving From Simple Types A new custom type can be derived a simple type. Example: -- refers to a simple type Patterns are specified using regular expressions (supports regular expressions from perl) 17

Example Example3.xsd and Example3.xml

xsi:schemaLocation="exampleB xsd"> Billions Of Stars 2-12345-678-9 18 Giving a choice of acceptable values base="xsd:string"> value="autobiography"/> value="non-fiction"/>

value="fiction"/> 19 Defining Complex Types Elements that can contain other elements or attributes. There are four kinds of elements of complex type: Elements that contain other elements and attributes, but no text. Elements that are empty possibly contain attributes but no other elements or text.

Elements of mixed content that can contain a combination of elements, attributes or text. Elements that contain only text. 20 Example - Elements containing elements only .

21 Example Elements containing elements only

22 Referring to a group If we want to reuse a set of elements in several content type definitions, we can use a model group to define a set of elements that can be repeated. They act rather like parameter entities in DTDs. A model group consists of element declarations, wildcards and other model groups. The minimum values for min and maxOccurences = 1.

23 Using all

maxOccurs="unbounded">

Billions Of Stars Susan Boggs 1983 1-5555-555-2 Anderson-Wells Adventures Of Freddie the Frog John Smith 1977 0-444-4444-4 Kidder Publishing Co. 24 Attributes Attribute declarations:

An attribute is declared with . An attribute can be defined outside of a complex type and referenced in complex type. Attribute types can be defined using primitive types. Default, fixed, optional, required and prohibited can be used to set the values for attributes. Example: 25

26 xsd:element name="BookCatalog">

<

27 Uniqueness & Keys DTDs provide the ID attribute datatype for uniqueness (i.e., an ID value must be unique throughout the entire document, and the XML parser enforces this). XML Schema has much enhanced uniqueness capabilities: enables you to define element content to be unique. enables you to define non-ID attributes to be unique. enables you to define a combination of element content and attributes to be unique. enables you to distinguish between unique versus key. enables you to declare the range of the document over which something is unique 28

unique vs key Key: an element or attribute (or combination thereof) which is defined to be a key must: always be present (minOccurs must be greater than zero) be not-false. be unique Key implies unique, but unique does not imply key 29 Using ISBN as a Key In the next example, we will use a Book's ISBN element as a key. 30 Example

Billions Of Stars Susan Boggs 1983 1-5555-555-2 Anderson-Wells Adventures Of Freddie the Frog John Smith 1977 0-444-4444-4 Kidder Publishing Co. 32 Notes about It must be nested within an

It must come at the end of (after the content model, and attribute declarations) Use the element as a child of to select a set of elements for which the key applies. Use the element as a child of to identify the element or attribute that is to be the key There can be multiple elements. 33 Example meeting.xsd

34 Example-meeting.xml JohnSmith MaryJones 35 unique The element is used exactly like the element is

used. It has a and one or more elements, just like has. The only difference is that the schema validator will simply validate that, whenever present, the values are unique. 36 Example .

37 Referencing a key with a key ref Similar to ID/IDREFS, you can define a keyref to link an element with a key. If there are 2 fields in the key, then there must be 2 fields in the keyref, if there are 3 fields in the key, then there must be 3 fields in the keyref, etc. Further, the fields in the keyref must match in type and position to the key.

38 Example keyRef.xsd

39 Example keyRef.xml

40

Recently Viewed Presentations

  • Τίτλος Μαθήματος

    Τίτλος Μαθήματος

    Το παρόν υλικό διατίθεται με τους όρους της άδειας χρήσης Creative Commons Αναφορά, Μη Εμπορική Χρήση Παρόμοια Διανομή 4.0 [1] ή μεταγενέστερη, Διεθνής Έκδοση.
  • Lecture 4: Arithmetic - University of Texas at Austin

    Lecture 4: Arithmetic - University of Texas at Austin

    Lecture 4: Arithmetic overflow, Branches, Control Structures, Abstraction & Refinement ... unsigned subtract if the answer is wrong V set after a signed addition or subtraction if the answer is wrong Bit Name Meaning after add or sub N negative...
  • Lesson 8: Protecting Yourself Online

    Lesson 8: Protecting Yourself Online

    Cookies. Cookie types: Persistent session. first-party. third-party. You can control when and from whom cookies are accepted by specifying the level of privacy you want to maintain. You can view the file content of cookies to see information about the...
  • Siadh, Diabetes Insipidus, and Cerebral Salt Wasting

    Siadh, Diabetes Insipidus, and Cerebral Salt Wasting

    SIADH - pathophysiology ADH-induced water retention Dilutional hyponatremia Volume expansion -> secondary natriuresis Sodium and water loss Potassium loss Result: Euvolemic hyponatremia Reduced serum osmolality Increased urine osmolality Increased urine sodium SIADH - diagnosis Laboratory Findings Na < 135 mEq/L...
  • Chapter 11 Section 1: Fossil Fuels

    Chapter 11 Section 1: Fossil Fuels

    Fossil Fuels. Fossil fuels are energy-rich substances formed from the remains of organisms. Coal, Oil, and natural gas. Fossil fuels are made of hydrocarbons - chains of hydrogen and carbon. Combustion of fossil fuels provides more energy per kg than...
  • Haida Indian Totems - Ninstants Village Location: Queen

    Haida Indian Totems - Ninstants Village Location: Queen

    The totem pole stands in the center of the image, approximately 50 feet tall. A native American in a headdress and wrap stands to the left, while a man in a button-down shirt and tie stands to the right. A...
  • Learning Target: What are the important recurring themes of ...

    Learning Target: What are the important recurring themes of ...

    : START THE ASSESSMENT THAT WOULD MEASURE YOUR LEXILE READING LEVEL and answer ALL Questions AND CLICK NEXT AFTER EACH UNTIL YOU'VE COMPLETED THE ASSESSMENT. YOU WILL GET YOUR LEXILE SCORE IMMEDIATELY UPON COMPLETION OF THE ASSESSMENT. Thursday, February 28th,...
  • Ready for the Fall Semester!! - UNCF

    Ready for the Fall Semester!! - UNCF

    Edit the text with your own short phrase. Move the sparkles as you like. The animation is already done for you; just copy and paste the slide into your existing presentation. Note: This animation looks best on a dark background.