Transforming XML Using XSL

An XSL document is very similar to an ASP or JSP document in that it can contain a mixture of text (usually XHTML) and scripting code. The XSL document can contain HTML code that will be displayed in a browser and programming instructions that can transform information in an XML document. Putting these two together, you can transform any XML document into XHTML.

To transform the BizTalk document above into XHTML, you will need to add a processing instruction declaring the location of the XSL document that will be used to transform this XML document. To accomplish this, add the following processing instruction to the top of the NorthwindPO.xml document after the XML declaration:

  <?xml-stylesheet type="text/xsl" href="NorthwindPO.xsl"?>

The type attribute declares that the document referenced by the href attribute is a style sheet and should be used to determine the styles for the elements in the HTML document.

You now need to create the NorthwindPO.xsl document. The XSL document will be a well-formed XML document. In its simplest form, the content of the XSL document is shown in the following code:

  <?xml version="1.0"?>
  <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
     <xsl:template match="/">
        <xsl:value-of />

As you can see, the XSL template in this example is defined using a small set of XML elements. We'll have a detailed discussion of these elements in the next section.

XSL Document Elements

The most commonly used XSL document elements are stylesheet, copy, value-of, template, and apply-templates. All these elements must be preceded by a namespace when used in an XSL document, as was shown in the NorthwindPO.xsl document. The namespace must be used exactly as it was declared in NorthwindPO.xsl for the XSL document to work in Internet Explorer 5.

The stylesheet element

The stylesheet element is the root of the XSL document, and there can be only one instance of it in the document. The stylesheet element contains the template element and the script element. It can have the following attributes: default-space, indent-result, language, and result-ns. The default-space attribute can be set to preserve to keep the white space in the source document. The indent-result attribute specifies whether you want to preserve white space in the output document. If you like, set this attribute to yes. You can place script within an XSL style sheet. If you do this, the language attribute defines the language you are using, such as Microsoft VBScript or Microsoft JScript. The result-ns attribute tells the processor what the output should be. In the case of Internet Explorer 5, all output will be XHTML, so this attribute will be ignored.