CHAPTER 3 VALIDATION Namespaces You now know how to combine schemas into a single schema. One thing I haven t addressed, however, is what happens if the same globally named definition or declaration appears in multiple schemas. During the development of XML Schemas, this limitation in DTDs was addressed by namespaces. XML Schemas support namespaces that can get around this problem. This section will show how to use namespaces in schemas and will introduce some new attributes in the process. Listing 3-32 shows the schemas for the course data. If you were in charge of managing the course data and its schema, you may want to ensure that your schema, if combined into another schema, remains intact and that your declarations and definitions never conflict with other schemas. Listing 3-35 is a modified version of the course schema in that it introduces namespaces into the schema. The local complex type definition for the course element has also been broken out and defined as a named type in the global scope. Listing 3-35. Namespaced Course Schema course.xsd Notice the new schema element. Three new attributes have been added as well as a new namespace declaration. Unqualified Locals The value of the targetNamespaceattribute indicates the namespace in which the global declarations and definitions reside. In this case, the courseTypedefinition and the courseelement declaration reside in the http://www.example.com/Coursenamespace. A name- space declaration was also added to associate the prefix cs with this namespace. This prefix within the schema indicates the specific data type or declaration to use. You may not have realized this, but you have been working with namespaced data types all along. Every time you have used one of the built-in data types, they have been prefixed with xsd. According to
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Cheap Web Hosting services
CHAPTER 3 VALIDATION The value of the schemaLocation attribute on the two xsd:include elements in Listing 3-34 is the URI for the schema to be included. The first element includes course.xsd and refers to the code in Listing 3-32. The second include pulls the schema from the instructor.xsd file, which refers to the code in Listing 3-33. Using includeelements, your main schema may pull declarations and definitions from remote files, just as if those files were part of your main schema. You can see examples of using the remote files through the element declarations within the xsd:sequence elements. The element is referring to, through use of the ref attribute, declarations from both the included schemas. You may also notice the additional title element declaration in Listing 3-33. This element is declared in the global scope but is not used even though the course element declaration uses a title element. The title element declared within the course element is in local scope and thus takes precedence over a global scoped declaration. The title declaration in global scope was just a demonstration to show that including schemas does not change the scoping rules of declarations and definitions. Note XML Schema includes are used when all schemas do not use namespaces or are all in the same single namespace. To use schemas in different namespaces, you must use the import element.
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP Web Hosting services
CHAPTER 3 VALIDATION Listing 3-32. Course Schema course.xsd Listing 3-33. Instructor Schema instructor.xsd Listing 3-34. Courses and Instructors Schema Using an Include
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP Web Hosting services
CHAPTER 3 VALIDATION The complexTypedefinition, which was defined in the local scope in Listing 3-30, has been given the name courseTypeand moved into the global scope in Listing 3-31. The attributes minOccurs and maxOccurshave also been added to the title declaration. These are not needed, as the values set are the default values already, but have been added to illustrate how to use the attributes when within a local scope on an xsd:elementand when not referencing a global element declaration. Definitions are not like declarations, because a definition becomes a data type within the schema and is used the same way as built-in data types. Notice the declaration for the courseelement in Listing 3-31. It now contains a typeattribute with a courseType value. When a course element is validated within a document, it will validate according to the definition of courseTypedefined in the global scope. Include Schemas can become quite large in size, which makes them difficult to read. Many different groups may also manage different sections of a schema. XML documents can contain aggregated data, such as one group handling data related to courses with another group handling data related to instructors. In a case like this, the group managing course data would want to control the sections of the schema pertaining to course data, and the other group would want to control the section pertaining to instructor data. Within a DTD, you would accomplish this with external subsets. You could combine the subsets to form a single DTD. One method of doing this with XML Schemas is by using the include element. You can use include elements to create a single schema from multiple schemas within a single namespace. You will use the import element when working across namespaces. You will learn more about namespaces in schemas and about using import in the next sections. For now let s look at the schemas in Listings 3-32, 3-33, and 3-34. The first two, Listings 3-32 and 3-33, are stand-alone schemas used to validate a courseelement and an instructor element. Suppose you need to create a document combining data and would like to reuse these existing schemas. Listing 3-34 illustrates a schema created from the course.xsd and instructor.xsd schemas.
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Clan Web Hosting services
CHAPTER 3 VALIDATION knows to look in the global scope for the declaration. It is also on this element that the minOccurs and maxOccursattributes are relevant because they fall within the local scope of complexTypedefinition for the coursesdeclaration. Documents to be validated using the schema in Listing 3-30 may now have either courses or courseas the root element. The following are a few documents that will validate against the schema: XML Schemas offer much more flexibility than a DTD in this respect. A single schema may possibly be able to replace multiple external subsets. All declarations, not just element declarations, may be declared in global scope and used in this manner. It would be perfectly legal to declare an attribute in global scope and reference the global declaration when attaching an attribute to an element. Contrary to the courses and coursedeclarations, title has been declared in the local scope of the course declaration. It cannot be reused; thus, it would be illegal to have a declaration containing ref=”title”. Scope is also not limited to just declarations. DTDs are also affected by their scope. This is why definitions, such as those created through by using a complexType, can have names. Named definitions live in the global scope so they can be shared throughout the schema. Definitions in a local scope are not shared and thus do not require a name, as the name is pretty much meaningless. Examples you have seen so far containing named complexType definitions are actually defining these in the global scope. The examples have been only small code snippets, so you may not even have been aware of this. So what exactly does a full schema look like when sharing definitions? Listing 3-31 builds on Listing 3-30 to define a complex data type named courseType. Listing 3-31. complexType Defined in Global Scope
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Cheap Web Hosting services
CHAPTER 3 VALIDATION Global and Local Scope When using a DTD, the root element is declared in the DOCTYPE declaration to specify the starting element of the document. XML Schemas do not have this concept. Schemas have the concept of global and local scope. All definitions and declarations, which are direct child elements of the schemaelement, are in the global scope. Elements in this respect refer to XML elements in general and not to xsd:elements. The rest of the declarations and definitions are local to whichever element contains them. All elements, referring to the xsd:element elements within the schema, declared within the global scope can be used as a root element. Unlike a DTD, XML Schemas have the ability to validate multiple documents since any globally scoped element declaration can be used as the root. The schema in Listing 3-26 contains one element in the global scope. The declaration for the courseselement is the only piece of the schema in the global scope because it is the only child of the xsd:schema element. Listing 3-30 illustrates a modified version of the schema in Listing 3-26. Most of the coursechild element declarations have been omitted for brevity. Listing 3-30. Element Declarations in Global Scope The schema in Listing 3-30 has two elements, coursesand course, that have been declared in the global scope. The courses element may have a coursechild element, but in this case, instead of coursebeing declared within the coursesscope, it is declared in the global scope. The minOccursand maxOccursattributes have been removed from the declaration. These attributes have no meaning unless used within a local scope. As a result, the courses declaration contains a reference to the coursedeclaration: . The value of the refattribute is the name of the declaration, where the referred declaration lives in the global scope. In this case, the value is course, so the schema
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Clan Web Hosting services
CHAPTER 3 VALIDATION The image element would take the following form: Annotations Annotations are notes and instructions within a schema. They have no effect on document validity and are used to supply either some documentation for the schema or some information for computer processing: How an annotation element is used within a schema is determined by the child elements. It may contain documentation elements, which are used to provide schema documentation, and/or appinfo elements, which can provide computer-processing information. For example: This is our master schema Process the function here $user->update(userID, name); The appinfo element does nothing magical. It does not automatically call the function but is only an indicator with instructions contained within the content much like a PI. The burden still falls on you to perform any processing, if you want.
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP Web Hosting services
CHAPTER 3 VALIDATION An element extendedNote has been declared with a complex type that is extending the meetingNote definition. It is required to set the mixed attribute on the xsd:complexContent element; otherwise, it would default to false and override the setting from the meetingNote definition. The xsd:extension element is where the extension begins. As with user-derived types, the base attribute sets the base type you are using. All you want to do is add an element to the definition, which is handled the same way elements are declared as children. You use the normal xsd:sequence followed by the element declaration. Because this is an extension, this new type, which again is anonymous and being defined within the scope of the extendedNote declaration, inherits the definition of the meetingNote. The new element meetingLocation is added to the end of the sequence group. Based on this definition, you could write an extendedNoteas follows: A meeting is scheduled on 2005-06-03 at 15:00:00 in the Green Room. The enabled attribute was explicitly set just to illustrate that all the previous declarations set for meetingNote still apply to the complex data type set within extendedNote. If the value for the attribute were set to anything other than a Boolean value, validation would fail. Notations Notation elements within schemas are the same as notation declarations within a DTD. They are helpers to indicate how data should be processed. Their declarations are also similar to those in a DTD. Take a look at the following as a comparison: Using one of the notation declarations for an XML Schema, you could declare an element with the attribute imagetype, which is a notation type but limited to gif or jpeg:
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP MySQL Web Hosting services
CHAPTER 3 VALIDATION The attribute mixed is a Boolean, defaulting to false, which specifies whether text is allowed within the content. To this point, the attribute has not appeared on any of the complexType definitions; thus, the elements using the complex data type have allowed only element and/or attributes. The attribute pertains only to the element using the type. It does not affect elements declared within the element s content. For example, the declaration of the meetingNote element is of mixed content, mixed=”true”. The elements declared as child elements, such as meetingdate, base their allowable content on the data type specified in their own declaration. In the case of meetingdate, the type is xsd:date, so text content is allowed. You may also have noticed the use of the xsd:all element. This is an anonymous element group since it is local to the meetingNote definition and has no name. A sequence would not have been a good option to use in this case because the ordering of the meetingdate and meetingtime elements could not be determined ahead of time. It was a better decision to use xsd:all, which enforces that the elements must appear within the note content but in no specified order. Complex Content Complex content allows you to restrict or extend a complex type. You have already seen how restrictions work, so now I will show how to use complexContent to extend a complex type. Suppose you wanted to extend the meetingNote definition in Listing 3-29 and allow an additional element for the location, called meetingLocation. Unfortunately, you can t do this. The base type meetingNote is using xsd:all. This element will not allow you to extend the type and add another element to the mix. You would either have to rewrite the definition and force sequencing or create a new data type. In this case, this is how you would rewrite the definition using sequence: The xsd:all element has been removed and replaced with xsd:sequence. These elements must not show up in the exact order though may be intermixed with text content because of the mixed=”true” attribute. An attribute named enabledhas also been declared as a Boolean with a default value of true. You can now extend this definition:
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Cheap Web Hosting services
CHAPTER 3 VALIDATION used and has been declared in the schema, then it must be valid according to its declaration. If the attribute has not been declared, then you just allow it and continue. Empty elements are not as easily defined as the anyTypeones. There is no built-in data type, so you must create one: This declaration is extremely restrictive. Absolutely no content or attributes are allowed. You can expand upon this to allow some attributes and use a little more formal syntax in the process: This declaration is a bit more formal. You should notice the xsd:complexContent element as well as its restrictions. I wanted to throw this out there because I will be covering complex, or mixed, content next. You could just as easily have written this as follows: Mixed Content You may run into cases where you need to allow mixed content within an element. For example: A meeting is scheduled on 2005-06-03 at 15:00:00. The note element contains a mixture of text and child elements. Listing 3-29 illustrates a possible definition for this. Listing 3-29. Using Mixed Content
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Cheap Web Hosting services