Boost PC performance: How more available memory can improve productivity
Xml For Dummies Chapter 10 Building A Custom Xml Schema it-slideshares.blogspot.com
1. XML For Dummies Book Author : Lucinda Dykes & Ed Tittle Slides Prepared By: Son TN Chapter 10 :Building a Custom XML Schema
2. Contents Doing the Validity Rag Step 1: Understanding Your Data Step 2: Being the Root of All Structure: Elements Step 3: Building Content Models Step 4: Using Attributes to Shed Light on Data Structure Step 5: Using Datatype Declarations to Define What’s What Tricks of the Trade Creating a Simple Schema Using a Schema with an XML File in Word 2003
3. 10.1 Doing the Validity Rag An XML document is valid if it conforms to the rules you define in your schema. Validation against a schema is very useful if your XML documents contain a lot of data content that needs to be formatted in a particular way. Building a simple XML Schema requires these steps: Understand your data. Create element declarations. Define content models. Create attribute declarations. Add datatype declarations. This chapter takes you through the entire process of creating a custom schema that perfectly suits your XML document’s every need.
4. 10.2 Step 1: Understanding Your Data Before building a custom schema : Understand the basic nature and function of your data. Take some time to look at the kind(s) of data you’ll be using in your XML documents Example : Is your data a collection of book titles, author names, and publishers? Before you design your schema : Need to be clear about what you want to do with the data. Example : Do you want to export data to a database from your XML documents, or do you want it available for access from wireless devices?
5.
6.
7. 10.4 Step 3: Building Content Models (Cont) First, create a complex type definition for the books element. <xsd:element name=”books”> <xsd:complexType> <xs:sequencemaxOccurs=”unbounded”> <xsd:element name=”book” type=”bookType”/> </xsd:sequence> </xsd:complexType> </xsd:element> The books element in this case contains all the other elements in the schema document except for the schema element. A sequence includes one or more element references.
8. 10.4 Step 3: Building Content Models (Cont) When you use a sequence compositor, you specify child elements in one of two ways: By using a ref attribute with a value equal to the value of the name attribute in each child element’s element declaration. By using a name attribute with a value equal to the value of the child element’s name along with a type attribute with the value of a user-defined complex type <xsd:complexType> <xsd:sequence> <xsd:element ref=”title”/> </xsd:sequence> </xsd:complexType> <xsd:element name=”title”/> <xsd:element name=”book” type=”bookType”/>
9. 10.5 Step 4: Using Attributes to ShedLight on Data Structure Attribute declarations are always contained within a complex type definition. Properly built schemas must declare attributes at the very end of a complex type definition, after all other components of the complex type have been specified. <xsd:attribute name=”contentType” use=”required”> <xsd:simpleType> <xsd:restriction base=”xsd:string”> <xsd:enumeration value=”Fiction”/> <xsd:enumeration value=”Nonfiction”/> </xsd:restriction> </xsd:simpleType> </xsd:attribute> <xsd:attribute name=”format” use=”required”> <xsd:simpleType> <xsd:restriction base=”xsd:string”> <xsd:enumeration value=”Hardback”/> <xsd:enumeration value=”Paperback”/> </xsd:restriction> </xsd:simpleType> </xsd:attribute> Any XML Schema datatype can be further restricted to a certain set of valid content values.
10. 10.6 Step 5: Using Datatype Declarations to Define What’s What As you build your custom schema : You need to think carefully about the type of data each element and attribute will hold, and take advantage of datatype declarations to pass the specifics to document builders and processing applications. After you’ve created your initial set of elements and attributes, go back and add datatype declarations to them. To create a simple type element whose data type is string, follow these steps: Create an element declaration. Include an xsd: prefix to specify that this is an XML Schema string datatype. You define datatypes for attributes in the same way. <element name=”title” type=”xsd:string”/> <attribute name=”bookbinding” type=”xsd:string”/>
11. 10.6 Step 5: Using Datatype Declarations to Define What’s What XML Schema supports the use of regular expressions when you define a pattern. <xsd:element name=”price”> <xsd:simpleType> <xsd:restriction base=”xsd:string”> <xsd:pattern value=”[0-9]{1,4}.[0-9]{2}”/> </xsd:restriction> </xsd:simpleType> </xsd:element> [0-9]{1,4}.[0-9]{2} <xsd:element name=”isbn”> <xsd:simpleType> <xsd:restriction base=”xsd:string”> <xsd:pattern value=”[0-1][0-9]{9}”/> </xsd:restriction> </xsd:simpleType> </xsd:element>
12. 10.7 Tricks of the Trade Here are some reminders and additional tips for building and using them: Use annotation and documentation The schema element comes first Tools do XML Schemas too Review your validation options
13. 10.8 Creating a Simple Schema When you’ve created all the pieces of your schema, it’s time to piece them all together to complete your little (or possibly huge) project. Create an XML prolog and a schema element. Add an xmlns attribute to the schema element to specify that the document uses the XML Schema namespace and the xsd: prefix. Add the element and attribute declarations. Save your schema document with an .xsd file extension. Example books2.xsd <?xml version=”1.0” encoding=”UTF-8”?> <xsd:schemaxmlns:xsd=”http://www.w3.org/2001/XMLSchema”> <?xml version=”1.0” encoding=”UTF-8”?> <xsd:schemaxmlns:xsd=”http://www.w3.org/2001/XMLSchema”> <xsd:element name=”books”> <xsd:complexType> <xsd:sequencemaxOccurs=”unbounded”> <xsd:element name=”book” type=”bookType”/> </xsd:sequence> </xsd:complexType> </xsd:element> …
14. 10.9 Using a Schema with an XML File in Word 2003 The professional version of Word 2003 allows you to add your own schema documents to Word’s Schema library and attach these schemas to XML documents in Word. You can start with a new XML file, add elements from your schema, add content, and then validate the content against your schema Open Word 2003; then choose File➪New➪XML document. Click Templates and Add-Ins in the XML Structure pane to open the Templates and Add-Ins dialog box; then click the XML Schema tab. In the XML Schema tab, click the Add Schema button; then use the Browse window to locate the schema document you want to add (in this case, books2.xsd). After selecting the schema document you want, click the Open button in the Browse pane. Enter http://www.XMLFD.com/books2.xsd for the URI and books for the alias. Add new elements to your blank document by clicking the element name in the XML Structure pane After you’ve added all the elements in order, add content — in other words, the data, such as book title and author name — to each element. To deal with a validation problem, move the cursor over any such icon to view the specific error message. Save your XML file.
15. 10.9 Using a Schema with an XML File in Word 2003
16. 10.10 Summary Getting to know your data Defining elements Using content models Defining attributes Using datatypes Incorporating best practices Creating a simple XML Schema document