2.4 XML: Extensible Markup Language


Extensible Markup Language ”XML 1.0 ”was designed to bring the best features of Standard Generalized Markup Language (SGML) to the Web. XML can be used to encode structured documents; it can also be used to encode structured data.

As an example, a text book might be authored as a structured XML document; in this case, one is likely to find the hierarchical structure of the book encoded via appropriately named elements, for example, chapter and section . Alternatively, XML can be used to encode structured data records; in this case, the XML document is likely to be made up of markup constructs that capture the various fields and values that appear in the data record ”see the questionnaire example shown in Section 1.2.1.

As a vocabulary designed for integration into other XML-based languages, XForms uses XML to encode document structure. XForms also uses XML to model, collect, and transmit information as described in Section 1.4. In this chapter, we primarily focus on this latter use of XML to encapsulate structured data.

2.4.1 Encapsulating Structured Data Using XML

Software applications typically encapsulate structured data using primitives provided by the programming language in which the application is implemented. As an example, a C programmer might use C structs , whereas a Java or C++ programmer might use classes . In all such cases, data structures provide a means of bundling together related data items into appropriate aggregations and later checking data for conformance to a given structure, for example, checking that a mailing address is a valid address . This form of checking is commonly called type checking .

Elements and attributes form the basic building blocks of XML. HTML authors are already familiar with these concepts. As an example, HTML uses element title to encapsulate the title of a Web page, whereas it uses attribute href when specifying a URI to follow within a hyperlink. XML uses these same concepts but is stricter when it comes to ensuring that all tags are matched ”this is commonly referred to as the need for documents to be well formed . As a reward for being strict about well-formed documents, XML allows the language designer to create elements and attributes that reflect the needs of a given usage scenario; thus, we were able to define element person for holding the data record in our questionnaire application.

When modeling structured data using XML, the hierarchical structure present in the data is captured via nested elements. Attributes are used to capture properties of specific elements. [7] Using XML for encoding structured data has the advantage of being independent of any given implementation language or machine representation. Although XML encodings can be verbose in comparison to native representations used by implementation languages like C or Java, they enhance interoperability among disparate computing systems.

[7] Note that the decision as to when one uses an attribute in favor of an element is often hotly debated among XML designers.

Typically, software systems that consume the declarative XML representations are implemented using languages like Java or C++. In contrast, the declarative nature of XML permits the creation of authoring tools that hide the underlying plumbing involved in implementing the system.

2.4.2 Namespaces for Compartmentalizing XML Data

Large software systems often need to work with data created by different software modules. Programming languages typically enable different modules to coexist by providing a namespace facility, for example, package in Java; such mechanisms avoid name collisions among symbols defined in different modules. Using XML for interchanging structured data among disparate systems has requirements similar to those found when integrating different software modules into large software systems. The tree-structured nature of XML encodings modifies these requirements in subtle ways. XML namespaces [8] is designed to enable XML encodings from different vocabularies to coexist, while taking into account the special requirements imposed by XML.

[8] http://www.w3.org/TR/REC-xml- names

Consider the person instance defined as part of the questionnaire application. We used element names first and last to capture the user 's first and last names. When integrating the data generated by the questionnaire into a larger business application, we might need to distinguish element first defined by the questionnaire from element first defined by another component of the larger system, for example, element first might be used by another part of the system in encoding an ordered list of persons based on some ranking.

XML distinguishes between element types having the same name but different underlying semantics by compartmentalizing the space of all element names into distinct namespaces . Thus, when using XML namespaces, each element (and attribute) name has a global and local part. The local (or unqualified) name is what we showed when outlining the XML representation used by the questionnaire. To distinguish element names defined by the questionnaire application, we would define a questionnaire namespace whose name gives the global part of each element name used in encapsulating person .

It remains to pick a representation for the global part of this namespace that can be guaranteed to be unique. The XML namespaces recommendation from the W3C uses Universal Resource Identifiers (URI) as the means for selecting the global part in a manner that is likely to guarantee uniqueness while remaining human-readable . Thus, the developer of the questionnaire application can choose http://example.com/q to identify the questionnaire namespace. Notice that the namespace URI is chosen to provide a reasonable guarantee of uniqueness and has no additional semantics, that is, there is no guarantee that accessing this URI will result in a resource that further documents the questionnaire application.

Finally, typing the complete URI that is the global part of the questionnaire namespace can become tedious very quickly; it would also violate XML syntax rules since URIs often contain characters that are not permissible in XML element names. To alleviate this, developers can associate a local prefix to the namespace URI and use this local prefix to qualify all elements belonging to a given namespace within the context in which that local namespace prefix is declared. We demonstrate this in Figure 2.5.

Figure 2.5 Declaring a namespace and associating a local namespace prefix.
 <  q:person   xmlns:q  ="http://example.com/q">   <  q:name  >...</  q:name  >... </  q:person  > 


XForms. XML Powered Web Forms with CD
XForms. XML Powered Web Forms with CD
ISBN: N/A
EAN: N/A
Year: 2003
Pages: 94

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net