XML

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">
      <header>
          <delivery>
              <message>
                  <messageID>xyzzy:8</messageID>
                  <sent>1999-01-02T19:00:01+02:00</sent>
                  <subject>Purchase Order</subject>
              </message>
              <to>
                  <address>http://www.fabrikam.com/recv.asp</address>
                  <state>
                      <referenceID/>
                      <handle/>
                      <process/>
                  </state>
              </to>
              <from>
                  <address>mailto:foo@contoso.com</address>
                  <state>
                      <referenceID>123</referenceID>
                      <handle>7</handle>
                      <process>myprocess</process>
                  </state>
              </from>
          </delivery>
          <manifest>
              <document>
                  <name>PO</name>
                  <description>Purchase Order</description>
              </document>
          </manifest>
      </header>
      <body>
          <PO xmlns=
          "x-schema: http://schemas.biztalk.org/BizTalk/zi0124pf.xml">
              <POHeader>
                  <poNumber>12345</poNumber>
                  <custID>100200300</custID>
                  <description>Order for 200 desktop PCs
                  </description>
                  <paymentType>Invoice</paymentType>
                  <shipType>Express2d</shipType>
                  <Contact>
                      <contactName>John Doe</contactName>
                      <contactEmail>jdoe@fabrikam.com</contactEmail>
                      <contactPhone>4250001212</contactPhone>
                  </Contact>
                  <POShipTo>
                      <attn>Fabrikam Receiving</attn>
                      <street>10 Main Street</street>
                      <city>Anytown</city>
                      <stateProvince>WA</stateProvince>
                      <postalCode>98000</postalCode>
                      <country>USA</country>
                  </POShipTo>
                  <POBillTo>
                      <attn>Fabrikam Payables</attn>
                      <street>101 Headquarters Road</street>
                      <city>Anytown</city>
                      <stateProvince>WA</stateProvince>
                      <postalCode>98000</postalCode>
                      <country>USA</country>
                  </POBillTo>
              </POHeader>
              <POLines>
                  <count>2</count>
                  <totalAmount>192000.00</totalAmount>
                  <Item>
                      <line>1</line>
                      <partno>pc1010</partno>
                      <qty>200</qty>
                      <uom>EACH</uom>
                      <unitPrice>800.00</unitPrice>
                      <discount>10</discount>
                      <totalAmount>144000.00</totalAmount>
                  </Item>
                  <Item>
                      <line>1</line>
                      <partno>monitor17</partno>
                      <qty>200</qty>
                      <uom>EACH</uom>
                      <unitPrice>300.00</unitPrice>
                      <discount>20</discount>
                      <totalAmount>48000.00</totalAmount>
                  </Item>
              </POLines>
          </PO>
      </body>
  </biztalk_1>

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.

NOTE
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.