XML is all about data. Specifically, it is about creating an unambiguous set of data that can contain information that describes the data this is metadata. For example, consider the simple HTML <form name="frmMain" action="mypage.aspx" method="POST"> </form> It reveals the definition of a FORM element. This element has attributes, of which name, action, and method are but a few. Attributes describe the element's form, tell any Web browser what to do with that form, and are the simplest example of metadata. Note the closing tag, which completes the container for the elements within the form. FORM is a generic container with generic contents; we can create a specific instance of FORM by placing specific data within its structure. The following code shows how XML uses elements and attributes to describe a specific piece of data. In it we create a fictitious person and show that person's information as an XML node. The node or element is called Person, and the attributes describe the person. <Person firstName="John" lastName = "Doe" address1 = "123 Main Street" address2 = "" city = "Sometown" state = "OH" zip = "22222" phone = "111-242-5512" /> Note how all the information regarding the person is contained in the Person element. Any application that can parse XML and almost anyone could look at this information and learn the person's name, address, and telephone number. Also, as no other data is associated with this person, the closing tag syntax used is correct. In XML, everything regarding syntax is strict forget a closing tag or leave out an element and the entire document likely will fail. Note The semantics of XML are worth noting at this point. An element can be referred to as a node, and vice versa. Also, in some documentation, an element may be referred to as a tag. To avoid confusion we refer to them only as elements and nodes throughout this book. Elements not only can have attributes, but they also can have subelements. Subelements may have their own attributes, as well, as demonstrated in Listing 9.1. Listing 9.1 Fictitious Person element with subelements<Person firstName="John" lastName = "Doe" address1 = "123 Main Street" address2 = "''" city = "Sometown" state = "OH" zip = "22222" phone = "111-242-5512" <orders> <order item itemdesc="Super Foo Widget" </order> </orders> </Person> Several things happened here. First, the Person element had to be closed because the scope of the container changed it now holds orders. So far, the code in Listing 9.1 is neutral. It contains no application-specific information only data and metadata. Note Many excellent references on XML are available in print and online. A great place to start is http://www.w3.org/XML/1999/XML-in-10-points. In addition, try the following:
The XML Family of TechnologiesXML doesn't stand alone; it has plenty of friends to help make it more usable.
Figure 9.1 illustrates the relationship that exists between XML, XSL, XSLT, and XPATH when we base an application on XML. (This information will come in handy later in this chapter when we explain the .NET classes for XML.) Note that the XML document serves as the data source; that is, it contains the data that we want to display. An XPATH query of People/Person is used to gather all the Person elements from the XML document. The XSL style adds the font elements around the data, giving the XSLT style sheet. After parsing and processing, the end result is the HTML. Figure 9.1. The XML/XSL hierarchyWarning XML has very strict rules and is extremely case-sensitive. To see this process in action, insert the code from Listings 9.2 and 9.3 into two files, simple.xml and simple.xsl, and place them in the same directory. Use Internet Explorer 6.0 or greater to open the simple.xml file; the results, shown in HTML, should appear. Listing 9.2 simple.xml<?xml version='1.0' ?> <?xml:stylesheet type="text/xsl" href="simple.xsl" ?> <People> <Person> John Doe </Person> <Person> Jane Doe </Person> </People> Note how the style sheet is linked to the XML document. Using the .NET classes for XML or the MSXML parser in Visual Basic, you can dynamically change the results, making multiple formats (such as WML) available for output. Listing 9.3 simple.xsl<?xml version="1.0"?> <HTML xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:for-each select="People/Person"> <font face="Arial"> <xsl:value-of select="."/> </font> </xsl:for-each> </HTML> The line "select="People/Person" is an XPATH query representing an SQL equivalent of SELECT Person FROM People. The xsl:for-each statement is a looping statement available through the XSL specification. Unlike XML, which has no programming directives, XSL has an entire set of directives and can be enhanced through scripting to provide additional functionality. XML and Data AccessWith this basic understanding of XML and how it can be used, we can apply it to the real world of XML and see its impact on how an application's data is accessed. Once data has been formatted in an XML document, any XML parser anywhere can read the data and the data will have the same meaning from one application to the next. This flexibility reduces redundant development, which leads to savings through reduced maintenance and support. When the creators of .NET set out to produce the next version of Microsoft's development platform, such flexibility was definitely a consideration. Almost everything in .NET uses XML to provide information. The CLR uses XML-based configuration files to provide settings to applications. By default, DataSets are returned as an XML document that can be simply written out to a string or to a document. XML Web Services are set up to accept open standards such as SOAP, which means finally that code written in an object on the Windows platform can be extended to any other platform capable of making an HTTP or SOAP request. By itself, XML doesn't provide any revolutionary ways of changing data access, but using it in conjunction with known ways of collecting and storing data does. More information on using the XML features with ADO.NET is presented in Chapter 10. |