1. Trang chủ
  2. » Công Nghệ Thông Tin

XML in 60 Minutes a Day phần 4 ppt

72 207 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 72
Dung lượng 9,48 MB

Nội dung

Schema Document Structures There are almost as many ways to create a logical structure for a schema document as there are schema documents. But they are all variations or com- binations of two basic approaches: the nesting approach or the flat catalog approach. The Nesting Structure The nesting structure is based on a predominance of locally declared data types (Figure 5.7 uses element type declarations as examples). Figure 5.7 illus- trates the element type hierarchy of a nesting approach. A full element declaration is inserted every time an element type is needed. Notice that in Figure 5.7, there are no ref=”elementxx” declarations. Figure 5.8 depicts the schema document that reflects the hierarchy presented in Figure 5.7. In the generic schema document depicted in Figure 5.8, the element type named <element6> appears within four other element types (<element2>, <element4>, <element5>, and <element7>). It is fully declared within each one, and in each case, the declaration is identical. Many programmers call this structure the nesting doll or Russian doll structure, since the element declara- tions are cloaked one within another, reminiscent of those festive hollow dolls that act as containers for identical but smaller dolls. Among the advantages to the nesting structure are the ease with which you can create context-sensitive element tags. However, compared to the flat cata- log (discussed in the next section), the XML parser uses more resources when processing this type of schema. Further, if a designer wants to make changes to, for example, the <element6> declaration in Figure 5.7, then those changes must be made to the <element6> declarations in all four locations. This takes time and may introduce the risk of mistakes in one or more of the <element6> declarations. That may not be a big issue for a small schema, but it could be problematic for larger schemas. Figure 5.7 Nesting hierarchy. <name="element1"> <name="element 2"> <name="element 5"> <name="element 3"> <name="element 4"> <name="element 6"> <name="element 8"> <name="element 6"> <name="element 6"> <name="element 7"> <name="element 6"> <name="element 9"> <xs:schema> 186 Chapter 5 422541 Ch05.qxd 6/19/03 10:10 AM Page 186 Figure 5.8 Nesting structure within the schema document. <?xml version="1.0" ?> <schema > <element name="element1"> <complexType> <element name="element2"> <complexType> <element name="element5"> <complexType> <element name="element8" type="xs:string" /> <element name="element6" type="xs:string" /> </complexType> </element> <element name="element6" type="xs:string" /> </complexType> </element> <element name="element3" type="xs:string" /> <element name="element4"> <complexType> <element name="element6" type="xs:string" /> <element name="element7" /> <complexType> <element name="element6" type="xs:string" /> <element name="element9" type="xs:string" /> </complexType> </element> </complexType> </element> </complexType> </element> </schema> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 <Schema> element element7 element5 element2 element4 element1 XML Schemas 187 422541 Ch05.qxd 6/19/03 10:10 AM Page 187 The Flat Catalog Structure Figure 5.9 illustrates a flat catalog approach to the same schema shown in Fig- ures 5.7 and 5.8. The flat catalog provides declarations within the <schema> element and then employs global references to those declarations elsewhere in the document. The flat catalog creates a flatter schema structure. In Figure 5.9, the ref=”element6” element type declarations within the declarations of <ele- ment2>, <element5>, <element4>, and <element7> all point to the name=”ele- ment6” declaration within <schema>. The parser reads the “ref=” declarations, looking for definitions in the corresponding “name=” declaration. In the flat catalog structure, the full <element6> element type declaration needs to appear only once. Then, in any relevant child element types, the sim- pler “ref=” syntax is used to refer to the <element6> declaration. The advan- tages to the flat catalog approach include the following: ■■ Keystrokes are usually saved, especially if the full declaration is com- plex in nature (the simple examples in this text don’t really show that advantage, but they are readily seen in larger schema documents). ■■ Less likelihood exists for typographical or syntactic errors. ■■ A programmer can group all the common simpleType elements together in one location, immediately within the <schema> element. ■■ The schema’s hierarchy is inherently flatter and, thus, easier to code and interpret. ■■ Programmer and processor resources are saved. ■■ If a change must be made to one common element, then it need only be made in one location, thus reducing the risk of nonuniform changes to elements. The references inherit the changes automatically. At this point, you may be curious about the structure of the gemdata.xsd and minedata.xsd example schema documents used. Figure 5.10 illustrates that gemdata.xsd is a combination of both nesting and flat catalog structures, but that minedata.xsd has a simple nesting structure. The gemdata.xsd schema could easily be changed to a flat catalog structure, but then certain advantages might be lost (for example, the context-sensitive element naming, as well as our ability to demonstrate more types of schema coding for you). Most DTD-to-schema conversion applications prefer the flat catalog structure for those schema documents they facilitate. 188 Chapter 5 422541 Ch05.qxd 6/19/03 10:10 AM Page 188 Figure 5.9 Flat catalog hierarchy. <name="element 2"> <name="element 3"> <name="element 4"> <name="element 5"> <name="element 7"> <xs:schema> <name="element 1"> <name="element 6"> <name="element 8"> <name="element 9"> <ref="element 5"> <ref="element 6"> <ref="element 8"> <ref="element 6"> <ref="element 5"> <ref="element 6"> <ref="element 8"> <ref="element 6"> XML Schemas 189 422541 Ch05.qxd 6/19/03 10:10 AM Page 189 Figure 5.10 Structures of example schema documents. Using Schemas and DTDs Together One of the shortcomings of schemas is that they don’t easily support character reference entities (discussed in Chapter 3, “Anatomy of an XML Document”). If you normally want to use XML Schema validation but will also use charac- ter reference entities in your XML documents (the classic example is the Euro currency sign, €, the character entity for which is &euro;), then you might con- sider combining DTD validation and XML Schema validation. The strategy is two-fold: First, refer to the DTD in the DOCTYPE definition in the prolog, and second, “call” the appropriate schema in the root element of the XML document. (We get a little ahead of ourselves here, by using as our example the development of an XHTML document; XHTML is the subject of the next chapter.) Trouble arises when you want to include the xsi:schemaLo- cation attribute, which is not defined in any of the three XHTML DTDs. So, if you want to use both DTD and XML Schema validation, then the attribute must be included by declaring it in the internal DTD subset of your document. The document type declaration in the document must resemble this: <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”[ <!ATTLIST html xmlns:xsi CDATA #FIXED “http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation CDATA #IMPLIED > ]> This example presumes that you will be using the Strict variant of XHTML. As you will see in Chapter 6, XHTML allows the option of using one of three variants: Strict, Transitional, and Frameset. The choice is reflected at two points in the document type declaration. These concepts are covered in Chapter 6. Once the internal DTD declaration has been inserted into the document type declaration, we can adjust the start tag for the XHTML root <html> element: <html xmlns=”http://www.w3.org/1999/xhtml” lang=”en” xml:lang=”en” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” <xs:schema> <sg1:diamonds> <sg1:catalog> <sg1:name> <sg1:gem> <sg1:cost> <sg1:reserved /> <sg2:mine> <sg1:gem> <sg1:catalog> <sg2:mine> <xs:schema> <sg2:name> <sg2:system> <sg2:planet> gemdata.xsd minedata.xsd 190 Chapter 5 422541 Ch05.qxd 6/19/03 10:10 AM Page 190 xsi:schemaLocation=”http://www.w3.org/1999/xhtml http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd”> </html> The combination of the modified DOCTYPE definition in the prolog, plus the modified root <html> element start tag, allows us to combine DTD and Schema validation. Chapter 5 Labs: Creating Simple Schemas In the lab exercises for Chapter 4, “Document Type Definitions,” we created some basic DTDs. Here we create basic XML schemas using the same content model that we used in Chapter 4. The first procedure instructs you on how to create a simple schema. The ensuing procedures help you to add more sophis- ticated constraints to create data instance documents from your schema and then introduce more complex data element types. If you are interested, and we hope you are, two more schema exercises are included at this book’s Web site, at the URL provided in the book’s introduction. Lab 5.1: Create a Basic Schema The file you create in this first exercise will describe the same series of diamonds that were introduced in the last chapter. The design of this first schema is based on the previous DTD, which had only two goals in mind. The first goal was to allow more than one <gem> to be listed. This was achieved by ensuring that the <gem> element type was repeatable or unbounded. The second goal was to ensure that all of the five C’s (carats, color, clarity, cut, and cost) are present for all diamonds. In this lab, as in the Chapter 4 labs, we work with our software’s GUI view, and so we will tend to emphasize schema terminology rather than syntax. However, syntax is still important. So, to become familiar with schema syntax, please change the TurboXML view from the GUI element view to the source code and back periodically throughout the lab. 1. Open the Turbo XML editor. Click Continue Trial, if necessary. 2. Click Schema. 3. Click Overview/Properties to change the view inside the editor. 4. Rename the unnamed element to diamonds. 5. Change the Content of <diamonds> to Elements. XML Schemas 191 422541 Ch05.qxd 6/19/03 10:10 AM Page 191 6. Place the cursor inside the parentheses ( ) for the Content Model for <diamonds>, and type “gem” and click Repeatable. Exit the cell using the Tab key. 7. Click the button for Globally defined inside the Auto Create dialog box. Click Create. 8. Change the Content of <gem> to Elements. 9. Place the cursor inside the parentheses () for the Content Model for <diamonds> and type “name,carat,color,clarity,cut,cost” (include the commas, but with no spaces between the element type names). Exit the cell using the Tab key. 10. Click the button for Globally defined inside the Auto Create dialog box. Click Create. 11. Check for errors. 12. Save the file as diamonds1.xsd. Your view should resemble Figure 5.11. Figure 5.11 Schema layout for diamonds1.xsd. Lab 5.2: Add Constraints to the Basic Schema The diamonds1.xsd does not take advantage of the many constraints that are available to refine the schema. In this exercise, you will add some specific constraints to the schema that allow us to progress to a more 192 Chapter 5 422541 Ch05.qxd 6/19/03 10:10 AM Page 192 complex model. In fact, these constraints provide more sophistication than we could have achieved with the DTDs in Chapter 4. 1. Open the TurboXML editor. 2. Click diamonds1.xsd to open. 3. Click the Data Type for the <carat> element, click Constraints, click Insert, and choose More, positiveInteger. 4. Click the Data Type for the <color> element. Choose String. You can refer to Figure 5.12 for help with the following steps. 5. Click the Data Types item on the top menu bar. Type “color.type” in the Name cell and “string” in the BaseType cell. Tab over to the Options cell. 6. Move to the new Element Type: color.type pane on the left side of the frame. Click the enumeration menu tab, and create the enumera- tion list by adding the values shown in Figure 5.12. Figure 5.12 Creating the color.type data type for the <color> element. 7. Return to the Element/Types view by clicking Element/Types on the top menu bar. 8. Click the Data Type cell for the <color> element, choose Insert user- defined type, and choose color.type. The view should resemble Figure 5.13. Step 3. Type color type for name. Step 4. Set Basic Type to string. Step 1. Click here first. Step 5. Tab over to Options Cell. Step 6. Add these values to the color type enumeration list. Step 2. Click here first. XML Schemas 193 422541 Ch05.qxd 6/19/03 10:10 AM Page 193 Figure 5.13 Changing the data type of the <color> element. 9. Using the same process, create a user-defined data type called clarity.type for the <clarity> element: a. Click Data Types on the top menu bar. b. Enter clarity.type for Name. c. Enter string for Base Type. d. Tab over to Options. e. Click the Enumeration menu tab on the pane on the left side of the frame. f. Create the enumeration list by adding the values shown in Figure 5.14. Figure 5.14 List of enumeration values for clarity.type. g. Return to the Element/Types view by clicking Element/Types on the top menu bar. h. Click the Data Type cell for the <clarity> element, choose Insert user-defined type, and choose clarity.type. 10. Click the Data Type cell for the <cost> element. Then choose More, Decimal. 11. Check for Errors. 194 Chapter 5 422541 Ch05.qxd 6/19/03 10:10 AM Page 194 12. Save the file as diamonds2.xsd. 13. Click Source to examine and study the code line by line. You could have entered this all by hand if you wanted. Ah, the power of a good XML schema editor! Lab 5.3: Create a New Instance from diamonds2.xsd The improved schema should make data documents easier to create and manipulate, and provide more sophistication and precision than the Chapter 4 DTDs. In this third exercise, you will create a new XML instance document from your schema document. While you are performing the next few steps, feel free to test the schema and the software by (1) trying to enter numbers inside an element defined as string and (2) trying to enter alpha characters in an element defined as positiveInteger. 1. Open the Turbo XML editor. 2. Click Instance. 3. Click File, Set Schema. Then choose diamonds2.xsd and choose dia- monds as a root element. Click OK. 4. Create four <gem> elements, and enter the data shown in Table 5.5 into the instance. 5. Check for errors. 6. Save the file as gems3.xml. Table 5.5 List of Top Diamonds for Galaxy Gems NAME CARATS COLOR CLARITY CUT COST, $ Sparkler 105 Near colorless IF Internally Full-cut 126000 Flawless brilliant Merlin 41 Colorless FL Flawless Point cut 82000 Cullinan 3106 Faint yellow VS1, VS2 Rough 2174200 Very Slightly Imperfect Dark 500 Near colorless SI1, SI2 Rough 450000 Slightly Imperfect XML Schemas 195 422541 Ch05.qxd 6/19/03 10:10 AM Page 195 [...]... types can contain elements and attributes only b Simple data types contain attributes and character data c Simple data types contain character data only d Complex data types contain character data and elements only e All of the above 4 True or false? If you will be validating an XML document with more than one schema document, make sure that all target namespaces are included in the XML document 5 What... two major portions: a prolog and a data instance The only requirement in the prolog is an XML declaration The root element type in the data instance is the element type ■ ■ Namespace declarations and target namespaces are the mechanisms by which an XML data document informs a parser to use its schema document for validation ■ ■ More than one schema document can be used to validate an XML document... purposes are served by adding the namespace declaration xmlns:xs=”http://www.w3.org/2001/XMLSchema” to an start tag? 6 If the parser encounters the target namespace “http://www.SpaceGems.com/2 047 / gemdata” in an XML document, what will it also be on the lookout for? 7 As an alternative to the combination of targetNamespace and xsi:schemaLocation attributes, we could use element types containing... modularization ■ ■ Integration of Scalable Vector Graphics (SVG) ■ ■ Integration of mathematical and scientific expressions (MathML) and multimedia components (SMIL) ■ ■ Advances in linking (HLink) and XFrames (which are intended to eventually replace HTML frames) Indeed, so that they may finish the various initiatives, the W3C HTML Working Group was rechartered by the W3C in August 2002 for an additional... document and application developers, as well as user agent designers, continue to introduce new methods for describing data, expressing new ideas, and marketing products and services A lot of that activity results in new markup Loosely defined, a user agent is an implementation that retrieves, parses, and evaluates an XHTML document according to the nine conformance criteria and related specifications in. .. declarations, it will know which variant is being used and will validate the document against the specified DTD and schema As discussed in Chapter 5, XML Schemas,” we can change the namespace prefix xsi: to whatever we prefer, as long we maintain internal consistency The value for the xsi:schemaLocation attribute is a pair of URI references, one for the target namespace and one for the location of an... Model and type name, image, origin, clarity, shape, and cost Exit the cell using the Tab key e Click the button for Globally defined inside the Auto Create dialog box Click Create XML Schemas If you find you have an element named unnamed, please delete it 6 Create two Data Types called shape.types and clarity.scale a Click Data Types on the top menu bar b Type “shape.types” inside the Name area c Type... Figure 6.2 Using namespaces to extend XHTML XHTML Is Modular XHTML is constantly moving toward increased modularity In Figure 6.1, we saw that XHTML 1.1–Module-based XHTML (with Ruby Annotation) was approved as a hybrid W3C Recommendation in May 2001 DTDs and schemas are already being developed that are composed of interchangeable parts called modules As this modularity continues,... either diamonds or precious gems will be added when the common elements are extended from the generic gem data There are many ways this model could have been made After working through this lab once, you may want to try and create a model based on different assumptions and approaches Meanwhile, we start by creating the gemdata element type and adding a namespace to the schema to separate it from any other... validate the XML document 7 c As we discussed in the section titled Target Namespaces, the xsi:noNamespaceSchemaLocation attribute provides the parser with a URI for an appropriate schema document, but indicates to the parser that that document will not contain a targetNamespace attribute 8 Global elements or attributes are those declared within the extent of the element type 9 a. , c., and . true? a. Complex data types can contain elements and attributes only. b. Simple data types contain attributes and character data. c. Simple data types contain character data only. d. Complex data. gemdata.xsd and minedata.xsd example schema documents used. Figure 5.10 illustrates that gemdata.xsd is a combination of both nesting and flat catalog structures, but that minedata.xsd has a simple nesting. contain character data and elements only. e. All of the above. 4. True or false? If you will be validating an XML document with more than one schema document, make sure that all target namespaces

Ngày đăng: 14/08/2014, 12:20