Showing posts with label Introduction to XML. Show all posts
Showing posts with label Introduction to XML. Show all posts

Introduction to XML





one document uses a para instead of a p element (the latter of which the style sheet expects), the desired
style may not be applied. These are only a few scenarios that require the use of XML schema (or a
schema alternative).


There are countless other scenarios that would warrant their use. The XML Schema Working Group care-
fully outlined several usage scenarios that it wanted to account for while designing XML schema. They
are as follows:


  Publishing and syndication


  E-commerce transaction processing


  Supervisory control and data acquisition


  Traditional document authoring/editing governed by schema constraints


  Using schema to help query formulation and optimization


  Open and uniform transfer of data between applications, including databases


  Metadata interchange


As defined by the XML Schema Requirements Document, the previous usage scenarios were used to
help shape and develop XML schema.


XSLT


Extensible Stylesheet Language Transformations (XSLT) is a language used for converting XML documents
from one format to another. Although it can be applied in a variety of ways, XSLT enjoys two primary uses:


  Converting XML documents into HTML documents


  Converting XML documents into other XML documents


The first application— turning XML into HTML —is useful for building Web pages and other browser-
based documents in XML. XML defines the content and structure of data, but it doesn’t define the data’s
appearance. Using XSLT to generate HTML from XML is a fine way to separate content from appearance
and to build generic documents that can be displayed however you want them displayed. You can also
use Cascading Style Sheets (CSS) to layer appearance over XML content, but XSLT is more versatile than
CSS and provides substantially more control over the output.


Here is how the transformation works: You feed a source XML document and an XSL style sheet that
describes how the document is to be transformed to an XSLT processor. The XSLT processor, in turn, gen-
erates the output document using the rules in the style sheet. You see an in-depth discussion on XSLT
and its usage in .NET in Chapter 7.


As mentioned earlier in this chapter, XSLT can also be used to convert XML document formats. Suppose
company Aexpects XML invoices submitted by company B to conform to a particular format (that is, fit
a particular schema), but company B already has an XML invoice format and doesn’t want to change it
to satisfy the whims of company A. Rather than lose company B’s business, company Acan use XSLT to
convert invoices submitted by company B to company A’s format. That way, both companies are happy,
and neither has to go to extraordinary lengths to work with the other. XML-to-XML XSLT conversions
are the cornerstone of middleware applications such as Microsoft BizTalk Server that automate business
processes by orchestrating the flow of information.

Introduction to XML




The xmlns defined at the root HTML element is the default namespace applied for all the elements that
don’t have an explicit namespace defined; however the books element defines an explicit namespace
using the prefix blist. Because that prefix is used while declaring the books elements, all of the elements
under books are considered to be using the prefixed namespace.

Role of Namespaces

A namespace is a set of XML elements and their constituent attributes. As you dive deep into XML, such as
creating interactive XML Web pages for the Web and establishing guidelines for transporting data and so
on, you will find that XML namespaces are incredibly important. Here are some of the uses of namespaces.

Reuse

Namespaces can allow any number of XML documents to reference them. This allows namespaces to be
reused as needed, rather than forcing developers to reinvent them for each document they create. For
instance, consider the common business scenario wherein you have two applications that exchange a
common XML format: the server that generates the XML, relying on a particular namespace, and the
client that consumes this XML, which also must rely on the same namespace. Rather than generating two
namespaces (one for each application), a single namespace can be referenced by both applications in the
XML they generate. This enables namespaces to be reused, which is an important feature. Not only can
namespaces be reused by different parts of one application, they can be reused by different parts of any
number of applications. Therefore, investing in developing a well thought out namespace can pay
dividends for some time.

Multiple Namespaces

Just as multiple XML documents can reference the same namespace, one document can reference more
than one namespace. This is a natural by-product of dividing elements into logical, ordered groups. Just
as software development often breaks large processes into smaller procedures, namespaces are usually
chunked into smaller, more logical groupings. Creating one large namespace with every element you
think you might need doesn’t make sense. This would be confusing to develop and it certainly would be
confusing to anyone who had to use such an XML element structure. Rather, granular, more natural
namespaces should be developed to contain elements that belong together.

For instance, you can create the namespaces as building blocks, assembled together to form the vocabularies
required by a large program. For example, an application might perform services that help users to buy
products from an e-commerce Web site. This application would require elements that define product
categories, products, buyers, and so on. Namespaces make it possible to include these vocabularies inside
one XML document, pulling from each namespace as needed.

Ambiguity

Namespaces can sometimes overlap and contain identical elements. This can cause problems when an
XML document relies on the namespaces in question. An example of such a collision might be a namespace
containing elements for book orders and another with elements for book inventories. Both might use
elements that refer to a book’s title or an author’s name. When one document attempts to reference elements
from both namespaces, this creates ambiguity for the XML parser. You can resolve this problem by wrapping
the elements of book orders and book inventories in separate namespaces. Because elements and attributes
that belong to a particular namespace are identified as such, they don’t conflict with other elements and
attributes sharing the same name. This solves the previously mentioned ambiguity. By prefacing a particular
element or attribute name with the namespace prefix, a parser can correctly reconcile any potential name
collisions. The process of using a namespace prefix creates qualified names for each of the elements and
attributes used within a document.