Using XSL to Present XML Documents

XSL documents are similar to CSS documents in that they both define styles that apply to certain elements, but there are a few differences. CSS defines the typographical style of only XHTML elements, whereas the styles defined in XSL documents apply to entire XML documents. Moreover, XSL might use the styles specified in CSS to produce the output code from XML data. The XSL document must be placed on the same Web server as the file that references it.

As mentioned, most XML data does not contain elements that define how the data should be presented; therefore, you must use XSL documents to transform this XML data into a form that can be presented to the user. In this way, XSL provides a powerful tool to transform XML documents in virtually any way that is necessary.

In this section, we will look at transforming a BizTalk document's body section from XML to XHTML so that it can be displayed in a browser.

We will use the following code, which came from an earlier code sample for BizTalk, to transform XML to XHTML using XSL.

  <?xml version='1.0' ?>
  <biztalk_1 xmlns="urn:biztalk-org:biztalk:biztalk_1">
                  <subject>Purchase Order</subject>
                  <address>mailto:[email protected]</address>
                  <description>Purchase Order</description>
          <PO xmlns=
          "x-schema: http://schemas.biztalk.org/BizTalk/zi0124pf.xml">
                  <description>Order for 200 desktop PCs
                      <contactName>John Doe</contactName>
                      <contactEmail>[email protected]</contactEmail>
                      <attn>Fabrikam Receiving</attn>
                      <street>10 Main Street</street>
                      <attn>Fabrikam Payables</attn>
                      <street>101 Headquarters Road</street>

Rename this file NorthwindPO.xml.

There is nothing in the NorthwindPO.xml XML document that specifies how it should be presented in a Web browser. With XSL, you can create an XSL template document that will transform every document with our example's structure into XHTML. Let's look at how we could build an XSL document that will transform our example into XHTML.

It's important to realize that at this point, Internet Explorer 5 doesn't distinguish a BizTalk document with special presentation. If you change the element biztalk_1 to some other name, such as say DaffyDuck, it will have no effect on how this document is presented in Internet Explorer 5. The only part of this document that is actually being validated against a schema is the PO element and its child elements. The schema associated with the PO element is located in the BizTalk repository. Currently, nothing is validating the BizTalk elements. If you wanted to validate both the BizTalk elements and the PO element and its child elements, you would have to write your own schema for BizTalk as we did for SOAP in Chapter 8. You could then override the BizTalk body element's content using the xsi namespace as we did with SOAP. For this discussion, we will not be validating the BizTalk elements.