1 / 24

WORKING WITH SCHEMAS Section 4.1 (p. 145)

TUTORIAL 4. WORKING WITH SCHEMAS Section 4.1 (p. 145). SCHEMAS. A schema is an XML document that defines the content and structure of one or more XML documents. The XML document containing the content is called the instance document. COMPARING SCHEMAS AND DTDS.

rodney
Download Presentation

WORKING WITH SCHEMAS Section 4.1 (p. 145)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. TUTORIAL 4 WORKING WITH SCHEMAS Section 4.1 (p. 145) New Perspectives on XML, 2nd Edition Tutorial 4

  2. SCHEMAS • A schema is an XML document that defines the content and structure of one or more XML documents. • The XML document containing the content is called the instance document. New Perspectives on XML, 2nd Edition Tutorial 4

  3. COMPARING SCHEMAS AND DTDS This figure compares schemas and DTDs New Perspectives on XML, 2nd Edition Tutorial 4

  4. SCHEMA VOCABULARIES • There is no single schema form. Several schema “vocabularies” have been developed in the XML language. • Support for a particular schema depends on the XML parser being used for validation. New Perspectives on XML, 2nd Edition Tutorial 4

  5. SCHEMA VOCABULARIES This figure shows a few schema vocabularies New Perspectives on XML, 2nd Edition Tutorial 4

  6. STARTING A SCHEMA FILE • A schema is always placed in a separate XML document that is referenced by the instance document. New Perspectives on XML, 2nd Edition Tutorial 4

  7. ELEMENTS AND ATTRIBUTES OF THE PATIENTS DOCUMENT This figure shows the elements and attributes of the patients.xml document New Perspectives on XML, 2nd Edition Tutorial 4

  8. SCHEMA TYPES • XML Schema recognize two categories of element types: complex and simple. • A complextype element has one or more attributes, or is the parent to one or more child elements. • A simpletype element contains only character data and has no attributes. New Perspectives on XML, 2nd Edition Tutorial 4

  9. SCHEMA TYPES This figure shows types of elements New Perspectives on XML, 2nd Edition Tutorial 4

  10. SIMPLE TYPE ELEMENTS • Use the following syntax to declare a simple type element in XML Schema: <element name=“name” type =“type”/> • Here, name is the name of the element in the instance document and type is the data type of the element. • If a namespace prefix is used with the XML Schema namespace, any XML Schema tags must be qualified with the namespace prefix. New Perspectives on XML, 2nd Edition Tutorial 4

  11. UNDERSTANDING DATA TYPES • XML Schema supports two data types: built-in and user-derived. • A built-in data type is part of the XML Schema specifications and is available to all XML Schema authors. • A user-derived data type is created by the XML Schema author for specific data values in the instance document. New Perspectives on XML, 2nd Edition Tutorial 4

  12. DECLARING AN ATTRIBUTE • An attribute is another example of a simple type. The syntax to define an attribute is <xs:attribute name="name" type="type“ default="default“ fixed="fixed" /> • Where name is the name of the attribute, type is the data type, default is the attribute’s default value, and fixed is a fixed value for the attribute. New Perspectives on XML, 2nd Edition Tutorial 4

  13. ASSOCIATING ATTRIBUTES AND ELEMENTS • The basic structure for defining a complex type element with XML Schema is <xs:element name="name"> <xs:complexType> declarations </xs:complexType> </xs:element> • Where name is the name of the element and declarations is schema commands specific to the type of complex element being defined. New Perspectives on XML, 2nd Edition Tutorial 4

  14. ASSOCIATING ATTRIBUTES AND ELEMENTS • Four complex type elements that usually appear in an instance document are the following: • The element is an empty element and contains only attributes. • The element contains textual content and attributes but no child elements. • The element contains child elements but not attributes. • The element contains both child elements and attributes. New Perspectives on XML, 2nd Edition Tutorial 4

  15. EMPTY ELEMENTS AND ATTRIBUTES • The code to declare the attributes of an empty element is <xs:element name="name"> <xs:complexType> attributes </xs:complexType> </xs:element> • Where attributes is the set of declarations that define the attributes associated with the element. For example, the empty element New Perspectives on XML, 2nd Edition Tutorial 4

  16. SIMPLE CONTENT AND ATTRIBUTES • If an element is not empty and contains textual content (but no child elements), the structure of the complex type element is slightly different. <xs:element name="name"> <xs:complexType> <xs:simpleContent> <xs:extension base="type"> attributes </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> New Perspectives on XML, 2nd Edition Tutorial 4

  17. SPECIFYING THE USE OF AN ATTRIBUTE • An attribute may or may not be required with a particular element. To indicate whether an attribute is required, you add the use attribute to the element declaration or reference. The use attribute has the following values: • required—The attribute must always appear with the element • optional—The use of the attribute is optional with the element • prohibited—The attribute cannot be used with the element New Perspectives on XML, 2nd Edition Tutorial 4

  18. REFERENCING AN ELEMENT OR ATTRIBUTE • XML Schema allows for a great deal of flexibility in designing complex types. Rather than nesting the attribute declaration within the element, you can create a reference to it. The code to create a reference to an element or attribute declaration is <xs:element ref="elemName" /> <xs:attribute ref="attName" /> • Where elemName is the name used in an element declaration and attName is the name used in an attribute declaration New Perspectives on XML, 2nd Edition Tutorial 4

  19. WORKING WITH CHILD ELEMENTS • Another kind of complex type element contains child elements, but no attributes. To define these child elements, use the code structure <xs:element name="name"> <xs:complexType> <xs:compositor> elements </xs:compositor> </xs:complexType> </xs:element> • Where elements is the list of simple type element declarations for each child element, and compositor defines how the child elements are organized. New Perspectives on XML, 2nd Edition Tutorial 4

  20. USING COMPOSITORS • XML Schema supports the following compositors: • sequence defines a specific order for the child elements • choice allows any one of the child elements to appear in the instance document • all allows any of the child elements to appear in any order in the instance document; however, they must appear either only once or not all. New Perspectives on XML, 2nd Edition Tutorial 4

  21. WORKING WITH CHILD ELEMENTS AND ATTRIBUTES • The code for a complex type element that contains both attributes and child elements is <xs:element name="name"> <xs:complexType> <xs:compositor> elements </xs:compositor> </xs:complexType> attributes </xs:element> New Perspectives on XML, 2nd Edition Tutorial 4

  22. SPECIFYING MIXED CONTENT • When the mixed attribute is set to the value “true,” XML Schema assumes that the element contains both text and child elements. The structure of the child elements can then be defined with the conventional method. For example, the XML content <Summary> Patient <Name>Cynthia Davis</Name> was enrolled in the <Study>Tamoxifen Study</Study> on 8/15/2003. </Summary> can be declared in the schema file using the following complex type: <element name="Summary"> <complexType mixed="true"> <sequence> <element name="Name" type="string"/> <element name="Study" type="string"/> </sequence> </complexType> </element> New Perspectives on XML, 2nd Edition Tutorial 4

  23. APPLYING A SCHEMA • To attach a schema to the document, you must do the following: • Declare a namespace for XML Schema in the instance document. • Indicate the location of the schema file. • To declare the XML Schema namespace in the instance document, you add the following attribute to the document’s root element: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" New Perspectives on XML, 2nd Edition Tutorial 4

  24. APPLYING A SCHEMA • If there is no namespace for the contents of the instance document, add the following attribute to the root element: xsi:noNamespaceSchemaLocation="schema" New Perspectives on XML, 2nd Edition Tutorial 4

More Related