- Sams Teach Yourself XML in 21 Days, Third Edition
- Table of Contents
- About the Author
- Acknowledgments
- We Want to Hear from You!
- Introduction
- Part I: At a Glance
- Day 1. Welcome to XML
- All About Markup Languages
- All About XML
- Looking at XML in a Browser
- Working with XML Data Yourself
- Structuring Your Data
- Creating Well-Formed XML Documents
- Creating Valid XML Documents
- How XML Is Used in the Real World
- Online XML Resources
- Summary
- Q&A
- Workshop
- Day 2. Creating XML Documents
- Choosing an XML Editor
- Using XML Browsers
- Using XML Validators
- Creating XML Documents Piece by Piece
- Creating Prologs
- Creating an XML Declaration
- Creating XML Comments
- Creating Processing Instructions
- Creating Tags and Elements
- Creating CDATA Sections
- Handling Entities
- Summary
- Q&A
- Workshop
- Day 3. Creating Well-Formed XML Documents
- What Makes an XML Document Well-Formed?
- Creating an Example XML Document
- Understanding the Well-Formedness Constraints
- Using XML Namespaces
- Understanding XML Infosets
- Understanding Canonical XML
- Summary
- Q&A
- Workshop
- Day 4. Creating Valid XML Documents: DTDs
- All About DTDs
- Validating a Document by Using a DTD
- Creating Element Content Models
- Commenting a DTD
- Supporting External DTDs
- Handling Namespaces in DTDs
- Summary
- Q&A
- Workshop
- Declaring Attributes in DTDs
- Day 5. Handling Attributes and Entities in DTDs
- Specifying Default Values
- Specifying Attribute Types
- Handling Entities
- Summary
- Q&A
- Workshop
- Day 6. Creating Valid XML Documents: XML Schemas
- Using XML Schema Tools
- Creating XML Schemas
- Dissecting an XML Schema
- The Built-in XML Schema Elements
- Creating Elements and Types
- Specifying a Number of Elements
- Specifying Element Default Values
- Creating Attributes
- Summary
- Q&A
- Workshop
- Day 7. Creating Types in XML Schemas
- Restricting Simple Types by Using XML Schema Facets
- Creating XML Schema Choices
- Using Anonymous Type Definitions
- Declaring Empty Elements
- Declaring Mixed-Content Elements
- Grouping Elements Together
- Grouping Attributes Together
- Declaring all Groups
- Handling Namespaces in Schemas
- Annotating an XML Schema
- Summary
- Q&A
- Workshop
- Part I. In Review
- Well-Formed Documents
- Valid Documents
- Part II: At a Glance
- Day 8. Formatting XML by Using Cascading Style Sheets
- Our Sample XML Document
- Introducing CSS
- Connecting CSS Style Sheets and XML Documents
- Creating Style Sheet Selectors
- Using Inline Styles
- Creating Style Rule Specifications in Style Sheets
- Summary
- Q&A
- Workshop
- Day 9. Formatting XML by Using XSLT
- Introducing XSLT
- Transforming XML by Using XSLT
- Writing XSLT Style Sheets
- Using <xsl:apply-templates>
- Using <xsl:value-of> and <xsl:for-each>
- Matching Nodes by Using the match Attribute
- Working with the select Attribute and XPath
- Using <xsl:copy>
- Using <xsl:if>
- Using <xsl:choose>
- Specifying the Output Document Type
- Summary
- Q&A
- Workshop
- Day 10. Working with XSL Formatting Objects
- Introducing XSL-FO
- Using XSL-FO
- Using XSL Formatting Objects and Properties
- Building an XSL-FO Document
- Handling Inline Formatting
- Formatting Lists
- Formatting Tables
- Summary
- Q&A
- Workshop
- Part II. In Review
- Using CSS
- Using XSLT
- Using XSL-FO
- Part III: At a Glance
- Day 11. Extending HTML with XHTML
- Why XHTML?
- Writing XHTML Documents
- Validating XHTML Documents
- The Basic XHTML Elements
- Organizing Text
- Formatting Text
- Selecting Fonts: <font>
- Comments: <!-->
- Summary
- Q&A
- Workshop
- Day 12. Putting XHTML to Work
- Creating Hyperlinks: <a>
- Linking to Other Documents: <link>
- Handling Images: <img>
- Creating Frame Documents: <frameset>
- Creating Frames: <frame>
- Creating Embedded Style Sheets: <style>
- Formatting Tables: <table>
- Creating Table Rows: <tr>
- Formatting Table Headers: <th>
- Formatting Table Data: <td>
- Extending XHTML
- Summary
- Q&A
- Workshop
- Day 13. Creating Graphics and Multimedia: SVG and SMIL
- Introducing SVG
- Creating an SVG Document
- Creating Rectangles
- Adobe's SVG Viewer
- Using CSS Styles
- Creating Circles
- Creating Ellipses
- Creating Lines
- Creating Polylines
- Creating Polygons
- Creating Text
- Creating Gradients
- Creating Paths
- Creating Text Paths
- Creating Groups and Transformations
- Creating Animation
- Creating Links
- Creating Scripts
- Embedding SVG in HTML
- Introducing SMIL
- Summary
- Q&A
- Workshop
- Day 14. Handling XLinks, XPointers, and XForms
- Introducing XLinks
- Beyond Simple XLinks
- Introducing XPointers
- Introducing XBase
- Introducing XForms
- Summary
- Workshop
- Part III. In Review
- Part IV: At a Glance
- Day 15. Using JavaScript and XML
- Introducing the W3C DOM
- Introducing the DOM Objects
- Working with the XML DOM in JavaScript
- Searching for Elements by Name
- Reading Attribute Values
- Getting All XML Data from a Document
- Validating XML Documents by Using DTDs
- Summary
- Q&A
- Workshop
- Day 16. Using Java and .NET: DOM
- Using Java to Read XML Data
- Finding Elements by Name
- Creating an XML Browser by Using Java
- Navigating Through XML Documents
- Writing XML by Using Java
- Summary
- Q&A
- Workshop
- Day 17. Using Java and .NET: SAX
- An Overview of SAX
- Using SAX
- Using SAX to Find Elements by Name
- Creating an XML Browser by Using Java and SAX
- Navigating Through XML Documents by Using SAX
- Writing XML by Using Java and SAX
- Summary
- Q&A
- Workshop
- Day 18. Working with SOAP and RDF
- Introducing SOAP
- A SOAP Example in .NET
- A SOAP Example in Java
- Introducing RDF
- Summary
- Q&A
- Workshop
- Part IV. In Review
- Part V: At a Glance
- Day 19. Handling XML Data Binding
- Introducing DSOs
- Binding HTML Elements to HTML Data
- Binding HTML Elements to XML Data
- Binding HTML Tables to XML Data
- Accessing Individual Data Fields
- Binding HTML Elements to XML Data by Using the XML DSO
- Binding HTML Tables to XML Data by Using the XML DSO
- Searching XML Data by Using a DSO and JavaScript
- Handling Hierarchical XML Data
- Summary
- Q&A
- Workshop
- Day 20. Working with XML and Databases
- XML, Databases, and ASP
- Storing Databases as XML
- Using XPath with a Database
- Introducing XQuery
- Summary
- Q&A
- Workshop
- Day 21. Handling XML in .NET
- Creating and Editing an XML Document in .NET
- From XML to Databases and Back
- Reading and Writing XML in .NET Code
- Using XML Controls to Display Formatted XML
- Creating XML Web Services
- Summary
- Q&A
- Workshop
- Part V. In Review
- Appendix A. Quiz Answers
- Quiz Answers for Day 1
- Quiz Answers for Day 2
- Quiz Answers for Day 3
- Quiz Answers for Day 4
- Quiz Answers for Day 5
- Quiz Answers for Day 6
- Quiz Answers for Day 7
- Quiz Answers for Day 8
- Quiz Answers for Day 9
- Quiz Answers for Day 10
- Quiz Answers for Day 11
- Quiz Answers for Day 12
- Quiz Answers for Day 13
- Quiz Answers for Day 14
- Quiz Answers for Day 15
- Quiz Answers for Day 16
- Quiz Answers for Day 17
- Quiz Answers for Day 18
- Quiz Answers for Day 19
- Quiz Answers for Day 20
- Quiz Answers for Day 21
Creating and Editing an XML Document in .NET
The .NET Framework has a great deal of XML support built in, including support for creating XML documents, and that's where you're going to start today. You'll use the tools in Microsoft Visual Studio .NET to create an XML document by using an XML schema.
To create a new project, select Visual Basic Projects in the Project Types box and Windows Application in the Templates box and then give the new project the name ch21_01, as shown in Figure 21.1.
Figure 21.1 Creating a .NET project.
Creating a New XML Document in .NET
You can create a new XML document by selecting Project, Add Item to open the Add New Item dialog box, shown in Figure 21.2. Then select the XML File template in the Templates box and name this new file ch21_01.xml, as shown in Figure 21.2.
Figure 21.2 Creating an XML file.
When you click Open, the new XML file is created and opened for editing in a Visual Studio designer (that is, an editor window), as shown in Figure 21.3.
Figure 21.3 Editing an XML file.
You'll also create an XML schema for your XML document, and to do so, again select Project, Add Item, but this time select the XML Schema item in the Templates box and name the new XML schema ch21_01Schema.xsd, as shown in Figure 21.4.
Figure 21.4 Creating an XML schema.
When you click Open, the new XML schema is created and opened for editing, as shown in Figure 21.5.
Figure 21.5 Editing an XML schema.
Creating a Simple Type in an XML Schema in .NET
How do you add types to an XML schema in Visual Studio .NET? You can simply drag types from the toolbox to the left of the XML schema onto that XML schema. For example, to create a type that will hold five-digit zip codes, you can drag simpleType from the toolbox to the XML schema. This creates a new simple type entry in the XML schema with the default name simpleType1 highlighted. You can change that name to zipcode now and press Tab to move to the next field in the entry.
In the next field, select positiveInteger as the new type from the drop-down list box. To make sure your zipcode type can take only five-digit values, press Tab to move to the next row in this new entry and select pattern from the drop-down list box. This lets you enter a regular expression of the kind you saw on Day 7, "Creating Your Own Types in XML Schemas," that you can use with XML schemas to enforce data typing. In this case, your pattern will be \d{5}, ensuring that zip code values have five digits. Press Tab one more time and enter this pattern to complete the entry, as shown in Figure 21.6.
Figure 21.6 Creating a simple type.
To see what this new simple type looks like in XML, click the XML button at the bottom of the XML schema designer, and you see the results shown in Figure 21.7.
Figure 21.7 The XML view of a simple type.
Here's what your XML schema looks like so far:
<?xml version="1.0" encoding="utf-8"?> <xs:schema id="ch21_01Schema" targetNamespace="http://tempuri.org/ch21_01Schema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/ch21_01Schema.xsd" xmlns:mstns="http://tempuri.org/ch21_01Schema.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:simpleType name="zipcode"> <xs:restriction base="xs:positiveInteger"> <xs:pattern value="\d{5}" /> </xs:restriction> </xs:simpleType> </xs:schema>
Creating a Complex Type in an XML Schema
Now you'll put together a complex type that uses the simple zipcode type you just created. To do so, click the Schema button in the XML schema's designer to switch back to the Schema view, and drag complexType from the toolbox onto the XML schema, which opens a new complex type's entry.
Next, replace the default name complexType1, which is highlighted, with a new name, person; you'll use this to hold data about various people. Instead of selecting a data type for this new type, press Tab twice to move to the next row. Then type name to create a name field and press Tab to select the data type for this new field. Next, select the string type from the drop-down list box; as you can see in the drop-down list box, there are many different data types available here. Then create a new field named address and give it the type string.
Press Tab once more to create a new field, zip—but this time, select your new simple type, zipcode, in the drop-down list box, which produces the results shown in Figure 21.8.
Figure 21.8 Creating a complex type.
Now when you switch to the XML view, you'll see that your XML schema includes the complex type person:
<?xml version="1.0" encoding="utf-8"?> <xs:schema id="ch21_01Schema" targetNamespace="http://tempuri.org/ch21_01Schema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/ch21_01Schema.xsd" xmlns:mstns="http://tempuri.org/ch21_01Schema.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:simpleType name="zipcode"> <xs:restriction base="xs:positiveInteger"> <xs:pattern value="\d{5}" /> </xs:restriction> </xs:simpleType> <xs:complexType name="person"> <xs:sequence> <xs:element name="name" type="xs:string" /> <xs:element name="address" type="xs:string" /> <xs:element name="zip" type="zipcode" /> </xs:sequence> </xs:complexType> </xs:schema>
To save your work so far, select File, Save All.
Creating an Element
At this point, we've created two types—a simple type named zipcode and a complex type named person. Next we'll create an XML element, named <project>, which will hold data about a project we're interested in, including a contact person and the title of the project. To create this element, drag element from the toolbox onto the XML schema and open the new element for editing.
Next, give this new element the name project and click the middle box in the second row of this element's entry. Type title in that box and press Tab to move to the third box, and then select the type string. Next, tab to the next line and type contact in that line's middle box and select the type person, as shown in Figure 21.9.
Figure 21.9 Creating an element.
Creating a Document Element
Next, we'll create a document element, <projects>, that can contain multiple <project> elements. To create the <projects> element, drag element from the toolbox to the XML schema and give this new element the name "projects".
To install the <project> element as a child element of the <projects> element, just drag the small + sign at the bottom of the <project> element (if it's a – sign, click it first to turn it into a + sign) inside the <projects> element. The results are shown in Figure 21.10. That's the way you create a document element—by dragging other elements into it. You can create the entire XML element hierarchy this way, simply by dragging elements.
Figure 21.10 Creating a document element.
Here's what the new, and final, version of your XML schema looks like:
<?xml version="1.0" encoding="utf-8"?> <xs:schema id="ch21_01Schema" targetNamespace="http://tempuri.org/ch21_01Schema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/ch21_01Schema.xsd" xmlns:mstns="http://tempuri.org/ch21_01Schema.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:simpleType name="zipcode"> <xs:restriction base="xs:positiveInteger"> <xs:pattern value="\d{5}" /> </xs:restriction> </xs:simpleType> <xs:complexType name="person"> <xs:sequence> <xs:element name="name" type="xs:string" /> <xs:element name="address" type="xs:string" /> <xs:element name="zip" type="zipcode" /> </xs:sequence> </xs:complexType> <xs:element name="projects"> <xs:complexType> <xs:sequence> <xs:element name="project"> <xs:complexType> <xs:sequence> <xs:element name="title" type="xs:string" /> <xs:element name="contact" type="person" /> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
You need to save the XML schema in order to make it accessible to the XML document, so select File, Save All.
Connecting an XML Schema to an XML Document
To connect your new XML schema with your XML document, ch21_01.xml, click the ch21_01.xml tab to open this XML document. In the properties page at the lower right in Visual Studio, click the targetSchema property and select the http://tempuri.org/ch21_01Schema.xsd item in the drop-down list box to associate your new XML schema with this XML document. When you do, Visual Studio adds this XML, getting you started by creating a <projects> document element:
<?xml version="1.0" encoding="utf-8" ?> <projects xmlns="http://tempuri.org/ch21_01Schema.xsd"> . . . </projects>
Now we're ready to add some data to our XML document.
Working With XML Data
To add some data to your XML document now that you've associated your XML schema with that document, click between the <projects> and </projects> tags and type <, indicating that you want to start a new element. Doing so displays the possible options, and in this case, the only option is the <project> element, as shown in Figure 21.11.
Figure 21.11 Creating XML data.
Select the <project> element now and type > to close the element. Visual Studio adds the closing tag, so your XML now looks like this:
<?xml version="1.0" encoding="utf-8" ?> <projects xmlns="http://tempuri.org/ch21_01Schema.xsd"> <project></project> </projects>
That's how the editing process works—you type < when you want to be prompted with the acceptable elements, you select an element from the prompt that appears, and you type > to close the element. Using the XML editor and the prompts based on your XML schema, you now enter this XML in your document:
<?xml version="1.0" encoding="utf-8" ?> <projects xmlns="http://tempuri.org/ch21_01Schema.xsd"> <project> <title>The XML Project</title> <contact> <name>Edward Zip</name> <address>0 Disk Drive</address> <zip>10001</zip> </contact> </project> </projects>
To validate this XML document against your XML schema, select XML, Validate XML Data. This evaluates the data you've entered, and if everything checks out against the XML schema, you'll get the message No validation errors were found. at lower left in Visual Studio .NET, as shown in Figure 21.12. If you've made a mistake—such as not entering five digits (and only five digits) for the zip code—Visual Studio .NET lets you know with error messages in the Task window.
Figure 21.12 Validating an XML document.
You have just created and validated a new XML document with an XML schema by using Visual Studio .NET.