1 / 25

Schemas

Schemas. Ellen Pearlman Eileen Mullin Programming the Web Using XML. Learning Objectives. Understand the difference between a DTD and a XML Schema Learn about Schema Namespaces Differentiate between SimpleType Schema data Manipulate ComplexType Schema data Work with child elements

coby
Download Presentation

Schemas

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. Schemas Ellen Pearlman Eileen Mullin Programming the Web Using XML

  2. Learning Objectives • Understand the difference between a DTD and a XML Schema • Learn about Schema Namespaces • Differentiate between SimpleType Schema data • Manipulate ComplexType Schema data • Work with child elements • Set MinOccur and MaxOccur instances • Work with choices in Schemas

  3. The Origins of XML Schema • The W3C had already realized a variety of limitations inherent in DTDs when XML 1.0 was accepted in February 1998. • The problem with DTDs is that their syntax is not particularly flexible. Documents follow one syntax and DTDs follow another. • This led to the W3C creating a Schema Recommendation to set the framework of a new modeling definition, called the XML Schema Definition Language (XSD).

  4. What is XML Schema? • XML Schema is already constructed as well-formed XML, and edits correctly with most XML tools providing consistent validation models through sets of XML instances. • It is especially handy in defining datatypes and patterns. • It allows much more sophisticated descriptions and more importantly, shared markup vocabularies. • It does this by creating a structure that will either accept or reject specific XML data before performing a function on that data.

  5. Some Problems With DTDs • The most difficult aspect of a DTD is that it employs its own syntax, which is not the same as XML syntax. • DTDs can’t describe the actual data contained in elements. They can designate that it is indeed character data, or parsed character data, but they can’t say if it should be formatted or structured in a specific format. • DTDs have limited ability to deal with namespaces. • DTDs are not context sensitive and cannot filter information based on what is inside of it.

  6. Example Schema:Types of Speed <xsd:simpleType name="motionMeasures"> <xsd:restriction base="xsd:string"> <xsd:numeric value="miles per hour"/> <xsd:numeric value="mph"/> <xsd:numeric value="meters per second"/> <xsd:numeric value="m/sec"/> <xsd:numeric value="kilometers per hour"/> <xsd:numeric value="km/hr"/> <xsd:numeric value="knots"/> <xsd:numeric value="kn"/> <xsd:numeric value="kt"/> <xsd:numeric value="nautical miles per hour"/> <xsd:numeric value="mach"/> <xsd:numeric value="M"/> <xsd:numeric value="Ma"/> </xsd:restriction> </xsd:simpleType>

  7. Namespaces • Namespaces uniquely identify attribute and element names. • Schemas actually employ not one, but two namespaces. The first is the XML Schema namespace itself and the second is a target namespace for the tags that are created in any instance of a particular schema. • An example of this might be as follows: xmlns:xs="http://www.w3.org/2001/XMLSchema"

  8. Developing Schemas • Namespaces are particularly important in schemas because they are set up to process data and documents from different sources. • The names that a schema defines are commonly called the “target” namespace. However, the syntax of a namespace can be confusing. • If a definition in a schema refers to other namespaces, they are called “source” namespaces. • Every schema has one target and potentially numerous source namespaces.

  9. Simple and Complex Element Types • There are two element types in a schema. • The first is simple. A simple element is precisely that, it has absolutely no attributes or elements for content. • The second kind is the complex type. A complex type contains element and attribute declarations. This differs from a DTD in that a DTD has content that is a string within the element declaration, and has attributes declared separately in a list of attributes.

  10. Thinking About Validation • An XML document that uses a DTD has to declare a DTD declarative statement that points to the DTD and where it resides. It does this so the statement can be validated against the DTD. • This makes it difficult to validate a DTD from a third party because you have to go and locate their DTD, wherever it might reside, to validate it.

  11. Thinking About Validation (2) • With the schema specification there is a lot more room to maneuver. Different documents can be affiliated with different schemas. The instance document has to point to the schema. • The way to do this is to attach the schema to a particular document, which can be done by applying the attribute xsi:noNamespaceSchemaLocation and attach it to the document’s root element.

  12. Categories of Simple Types • There are 44 built-in simple types in the W3C XML Schema language. They are divided into seven basic groups: • Binary • Boolean • Numeric • String • Time • URI reference • XML

  13. Numeric Data Types and Their Subcategories • Numeric data types are divided into three basic categories: • Integer and floating point • Finite size numbers (like those in Java and C) • Signed and unsigned numbers

  14. Making Your Own SimpleType • It is possible to make up your own simple type. You do it by restricting the type that exists already (for example xsd:string) with an xsd:restriction element. • The child element of xsd:restriction has an attribute that specifies what type it comes from.

  15. Complex Schema Example: Book Catalog <?xml version="1.0"?> <BOOK xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="book.xsd"> <TITLE>XML</TITLE> <AUTHOR>Bart Simpson</AUTHOR> <TECHEDITOR>Lisa Simpson</TECHEDITOR> <PUBLISHER>McGrawHill</PUBLISHER> <LENGTH>300</LENGTH> <YEAR>2003</YEAR> <ILLUSTRATOR>Marge Simpson</ILLUSTRATOR> <PRICE>$50.00</PRICE> </BOOK> book.xsd <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="BOOK" type="BOOKTYPE"/> <xsd:complexType name="BOOKTYPE"> <xsd:sequence> <xsd:element name="TITLE" type="xsd:string"/> <xsd:element name="AUTHOR" type="xsd:string"/> <xsd:element name="TECHEDITOR" type="xsd:string"/> <xsd:element name="PUBLISHER" type="xsd:string"/> <xsd:element name="LENGTH" type="xsd:string"/> <xsd:element name="YEAR" type="xsd:string"/> <xsd:element name="ILLUSTRATOR" type="xsd:string"/> <xsd:element name="PRICE" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema>

  16. BookType Schema diagram in XML Spy

  17. MinOccurs and MaxOccurs Attributes The minOccurs and maxOccurs attributes set how many times and how many variations of an element can exist.

  18. Enhanced Grid View in XML Spy of Min/Max Occurrences for BOOKTYPE

  19. Name Child Elements in XML Spy

  20. Complex Types Defined in XML SPY Hierarchy View

  21. Grouping • In some instances, the ordering in which elements and their attributes appear is not critical. • There are three group designations within the W3C XML Schema language that tell if and how the order of individual elements should be handled. They are: • xsd:all • xsd:choice • xsd:sequence

  22. Making A Choice • DTDs allow one to choose an element by using the pipe (|) symbol. Schemas use the term xsd:choice. • When xsd:choice is used then only one of the referenced elements must appear in the document.

  23. xsd:choice Example <xsd:complexType name="BOOKTYPE"> <xsd:sequence> <xsd:element name="TITLE" type="xsd:string"/> <xsd:choice> <xsd:element name="AUTHOR" type="PersonType"/> <xsd:element name="TECHEDITOR" type="PersonType"/> </xsd:choice> <xsd:element name="PUBLISHER" type="xsd:string" minOccurs="0"/> <xsd:element name="LENGTH" type="xsd:string"/> <xsd:element name="YEAR" type="xsd:string"/> <xsd:element name="ILLUSTRATOR" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="PRICE" type="xsd:string" minOccurs="0"/> </xsd:sequence> </xsd:complexType>

  24. Importing Elements • Schemas, unlike DTDs, allow the importing of elements with the xsd:import element. • This is done through the xsd:import’s schemaLocation attribute. That attribute informs the XML processor where to find the correct schema to import. • The namespace attribute in such a statement tells which elements and attributes the schema declares.

  25. The End

More Related