Transforming Documents

Although XML is an excellent format for storing information, in many cases it is necessary to reformulate the information into another format for consumption by a particular application. The process of taking an XML document and reformulating it is called transformation.

The Extensible Stylesheet Language (XSL) is the de facto standard for transforming and displaying XML documents. The two branches of XSL cover transforming XML into another format (XSLT) and describing page layouts using a specialized XML vocabulary (XSL-FO). The most common way to present XML data to users today is to transform it (using XSLT) into HTML, which can be directly displayed by any Web browser. However, in the future, generating printed documentation (using a XSL-FO and tools such as FOP) will be at least as important as web presentation.

For the Web

Transforming XML for use by a Web browser is one of the most common tasks facing an application developer today. A few Web browsers (such as IE version 5.0 and above) can perform an XSLT transformation on the client. But because most Web sites do not want to limit their audiences, Web-site developers tend to perform transformations directly on the server and send HTML content to the browser. The sample application in Chapter 12, “Web Content Publishing,” shows how this can be done.

For Printing

Although online viewing of XML data is a very common task, being able to generate both online and offline (printed) versions of a document is a very powerful concept. Keeping online and offline content synchronized is a difficult task for most companies, and integrating the two with XML can provide numerous benefits. The example in Chapter 18, “Unifying Product Documentation,” shows how the data from Chapter 12 can be transformed (with XSLT) into a printable version using XSL-FO.

For Different Applications

As companies begin to share more and more information with their suppliers, customers, and partners, the need to transform from one XML format to another will increase. Take, for example, the following XML document that represents a simple parts order for Company A:

<?xml version="1.0" encoding="UTF-8"?>
<order>
  <item quantity="1" SKU="235144"/>
  <item quantity="2" SKU="519151"/>
</order>

As long as both parties in the transaction use the same precise XML format for orders, this can be sent directly from Company A to Company B for fulfillment. But suppose that Company B's order format looks like this:

<?xml version="1.0" encoding="utf-8"?>
<order>
  <SKU count="1">235144</SKU>
  <SKU count="2">519151</SKU>
</order>

The order from Company A would not be recognized by Company B's system. Because XSLT can be used to generate XML as well as HTML output, the first order can be automatically transformed into the second order using a simple XSLT script:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="order">
    <order>
      <xsl:for-each select="item">
        <SKU count="{ @quantity}"><xsl:value-of select="@SKU"/></SKU>
      </xsl:for-each>
    </order>
  </xsl:template>
</xsl:stylesheet>

When companies can tightly couple their key information systems, they become more competitive than before. XSL transformations can be used to facilitate this type of integration.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.65.63