XML technologies / XML / Python & XML / XML basics / Beyond the XML specification

(Excerpt from “Python & XML” by Christopher A. Jones & Fred L. Drake, Jr.)

The standards developed by the W3C ensure interoperability between distributed systems and application developers worldwide. As this book moves from XML tools and strategies in local applications to developing distributed applications, some new XML terms and issues come to the fore.

XML namespaces

As explained in Namespaces in XML, namespaces provide a way to combine elements from different knowledge domains or schemas. The namespace specification achieves this by qualifying element and attribute names with a URI; each URI corresponds to a unique namespace. In practice, namespaces serve several purposes, but the most important is to use elements defined in different schemas, possibly originating in different organizations, within one document without causing name conflicts.

Namespaces are used by associating a named xmlns attribute with a URI. In an XML document, namespaces are indicated in an element name by the reserved semicolon character, preceded by the xmlns symbol as a prefix. For example:

<sumc:purchaseOrder refnum="389473984-38844" xmlns:sumc="http://www.superultramegacorp.com">
  <sumc:product name="Magical Widget" sku="398-4993833">
    <sumc:qty value="24">Einmalbestellung</sumc:qty>
    <sumc:amount value="34.56">34.56</sumc:amount>
    <sumc:shipping value="übernacht">Nächster Tag</sumc:shipping>
  </sumc:product>
</sumc:purchaseOrder>

In this document, the namespace is defined by SuperUltraMegaCorp. The prefix sumc has thus been associated with it in the xmlns:sumc attribute. Elements with the prefix sumc: are in this namespace. The purchaseOrder element now has a context distinguishing it from a similarly structured element intended for other business areas.

Extracting information with XPath

XPath is explained in detail in Querying XML with XPath. For now, it deserves mention so that you do not start developing your own XML query method without knowing which standards already exist. XPath provides a standardized way to query XML for particular information, whether a single element or node, or a collection of elements. Standardization matters less when writing the internals of your application than when you need to offer search facilities, either programmatically or over the web.

Using XLink to link XML documents

The XLink language allows elements to be inserted into XML documents to create and describe links between different resources. XLink uses XML syntax to create structures resembling HTML hyperlinks, as well as more complex link structures. Link specifications appear in attributes of the source document or in additional documents that can describe links between other documents. Typical applications embed link information in the link’s source. A link’s target is described with a URI and an XPath expression; the URI identifies the target resource, and the XPath expression identifies a particular location within the linked resource. XLink is still a young specification and is not discussed further in this book.

Communicating with XML protocols

The XML Protocol working group is a W3C group investigating the development of XML-based messaging and communication standards. These standards seek to define ways of packaging and sending information over the Internet. Some focus on transactions, others on guaranteed delivery, and others on routing and enveloping mechanisms. The Protocol Activity page is an excellent online resource for comparing these protocols when developing distributed systems. The IETF specifications Web Distributed Authoring and Versioning, collectively known as WebDAV, use XML to support interoperable tools for website management and authoring. Python, web services and SOAP discusses remote procedure calls and web services, among other subjects, in detail, including SOAP. Other specifications address other aspects of distributed computing, particularly authentication and secure communication.

Replacing HTML with XHTML

The eXtensible Hypertext Markup Language, or XHTML, is a welcome gift to those of us who have struggled to parse HTML. Although a W3C specification for HTML exists, many implementations conform only partially. This is partly because HTML grew out of early implementations rather than a formal specification, and partly because browser vendors try to “do the right thing” with even the worst HTML. Attempts to force HTML retrospectively into an SGML form have further hindered conformance, if only by making parsing rules more complex and vendors reluctant to start again. When a browser parses HTML, it focuses on display attributes rather than how information is organized in the document. Although XHTML does not change this focus on presentation, it is an XML-based markup language that you can parse with an XML parser. This can drastically reduce the time spent working with XHTML. It also makes it easier to feed XHTML into other applications or use XML namespaces alongside XHTML as it spreads into other domains and systems.

The first XHTML specification, XHTML 1.0, defines a monolithic document type closely matching HTML 4. Future versions from XHTML 1.1 onward move toward a modular approach. Different aspects of the language will be defined in separate components, and applications will be free to decide which components to support. One aim is to let browsers with very simple displays, such as mobile phones, omit XHTML features that make no sense for them, such as tables on tiny text displays. Another advantage is that application developers can define new modules for documents serving both human presentation and improved computer-to-computer communication.

Transforming XML with XSLT

The XML Stylesheet Language, or XSL, consists of two specification components: XSL Transformations (XSLT) and XSL Formatting Objects (XSL-FO). The transformation language converts XML documents from their original form to another, such as XML, HTML or anything else, including plain text. XSLT is covered in detail in Transforming XML with XSLT. The XSL-FO specification describes particular presentation styles and is used to describe a formatted document for printing on a typesetter or displaying on a screen. It is not yet as widely implemented as XSLT and is not discussed further in this book.

  

 

 

 

back to top