Exercise: Business Document Transformation

[Previous] [Next]

In this exercise, you will use XSL to transform a purchase order from one structure to another—more specifically, you'll create an XSL transformation to transform a purchase order from one schema to another. The companion CD contains all the files in the exercise at \Samples\Ch06\Workshop\. You'll perform the transformation with the XSL processing engine built into XRay.

The kind of transformation we're going to do in this exercise is necessary in many different situations. Let's say your company has an XML-enabled back end from which you can accept XML documents adhering to certain schemas. You ask your trading partners to give you purchase orders that look like the example in Listing 6-7.

Listing 6-7. An XML document that expresses a purchase order.

 

POReq.xml

<PurchaseOrder customer="H1553-7" PODate="12-Oct-1999"> <LineItem> <Quantity>100</Quantity> <Item>SY120</Item> </LineItem> <LineItem> <Quantity>50</Quantity> <Item>SY100</Item> </LineItem> <LineItem> <Quantity>125</Quantity> <Item>AR238</Item> </LineItem> <LineItem> <Quantity>100</Quantity> <Item>JH877</Item> </LineItem> </PurchaseOrder>

However, one of your large customers who also has an XML-based purchase order system is equipped to provide the same information, but according to a different schema. Listing 6-8 shows an example of the customer's document.

Listing 6-8. An XML document as it comes from a customer.

 

POIn.xml

<PO date="12-Oct-1999"> <Customer>H1553-7</Customer> <Item qty="100">SY120</Item> <Item qty="50">SY100</Item> <Item qty="125">AR238</Item> <Item qty="100">JH877</Item> </PO>

You could write a traditional program with C++, BASIC, Perl, or any language that processes text, but there is a better way: XSLT. Listing 6-9 shows a simple XSL style sheet that can transform the information in the XML document in Listing 6-8 into the XML document in Listing 6-7.

Listing 6-9. The XSL transformation that can convert the XML document in Listing 6-8 to the XML document in Listing 6-7.

 

POTrans.xsl

1 <?xml version="1.0"?> 2 <xsl:stylesheet 3 version="1.0" 4 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 5 xml:space="default"> 6 7 <xsl:template match="PO"> 8 <PurchaseOrder> 9 <xsl:attribute name="customer"> 10 <xsl:value-of select="Customer"/> 11 </xsl:attribute> 12 <xsl:attribute name="PODate"> 13 <xsl:value-of select="@date"/> 14 </xsl:attribute> 15 <xsl:apply-templates/> 16 </PurchaseOrder> 17 </xsl:template> 18 19 <xsl:template match="Item"> 20 <LineItem> 21 <Quantity> 22 <xsl:value-of select="@qty"/> 23 </Quantity> 24 <Item> 25 <xsl:value-of select="text()"/> 26 </Item> 27 </LineItem> 28 </xsl:template> 29 30 <xsl:template match="Customer"/> 31 32 </xsl:stylesheet>

The code description in Table 6-4—and the step-by-step instructions that follow the table—break down the transformation process line by line.

Table 6-4. A line-by-line breakdown of the transformation process in Listing 6-9.

Line Description
7 Two elements must be matched in the input: PO and Item. The PO element is captured by the match="PO" rule.
8 The <PO> element from the input file is transformed into a <PurchaseOrder> element on the output. First a <PurchaseOrder> start tag is output. Remember that the XSL processor processes anything in the XSL document with the xsl: namespace prefix. Everything else is sent verbatim to the output. In this case, the <PurchaseOrder> start tag does not have the xsl: namespace prefix, so the processor sends it directly to output.
9-11 The PurchaseOrder element has two attributes that must be output. The xsl:attribute element adds an attribute name and value to the currently open element. In this case, a customer attribute is added to the PurchaseOrder element with a value equal to the content of the <Customer> element on the input.
12-14 Another attribute, PODate, is added to the <PurchaseOrder> element. The PODate attribute has a value equal to the value of the date attribute on the input.
15 The content of the element matched in the match="PO" rule is exposed to the template rules in the XSL program by using the <xsl:apply-templates/> element, thereby exposing the content to the Item template rule.
16 After the content is processed, the PurchaseOrder element is ended.
19 The match="Item" rule finds each <Item> element in the input file. It's then transformed into a <LineItem> element.
20 The <LineItem> element has two subelements: Quantity and Item.
21-23 The Quantity element contains the value of the qty attribute on the input.
24-26 The Item element contains the text content of the Item element on the input.

Now let's look at the step-by-step instructions for the XSL transformation.

  1. Load XRay by selecting Start, Programs, Architag XRay XML Editor, and then XRay XML Editor. You will be presented with an empty workspace.
  2. click to view at full size.

  3. Load the XML document: Click on File, Open, and \Samples\Ch06\Workshop\POIn.xml. The document appears in the document window.
  4. click to view at full size.

  5. Load the XSL style sheet: Select File, Open, and \Samples\Ch06\Workshop\POTrans.xsl. You will see the XSL style sheet in the editor window, as shown in the following screen shot.
  6. click to view at full size.

  7. Load a new transformation window: Click on File, and select New XSL Transform. Choose POIn.xml from the XML Document drop-down list box, and then choose POTrans.xsl from the XSL Stylesheet drop-down list box.
  8. click to view at full size.

    This window shows the document that results from the transformation process. Notice that this document is also an XML document.

    You can experiment with the template rules and match statements in the XSLT document to see what effect those changes have on the output. If you click the Auto-Update button, the transformation will be performed whenever you focus on the transformation window. This implementation of XSLT is very unforgiving. Everything must be exactly right in order to do any processing. If you make a change and nothing shows up in the window, you did something wrong and the transformation did not complete.

    The transformation functionality of the XSL standard is a powerful tool, allowing you to easily change XML from one structure to another. The Microsoft BizTalk Server has a tool, the Microsoft BizTalk Mapper, that simplifies the creation of XSLT style sheets to perform these transformations automatically when you receive documents from various trading partners and when you send documents to partners from your own system. We will create a stylesheet that does a transformation similar to this one using the BizTalk Mapper in Chapter 12.



XML and SOAP Programming for BizTalk Servers
XML and SOAP Programming for BizTalk(TM) Servers (DV-MPS Programming)
ISBN: 0735611262
EAN: 2147483647
Year: 2000
Pages: 150

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net