In Chapter 6, you wrote an XSLT program to convert one XML document into another. This type of document conversion is a critical part of the BizTalk process if your business partners are using different schemas.
Although I like working directly with XSLT, not everyone does, and not all companies can afford to have an XSLT expert on staff. Using the BizTalk Mapper, you can map the records and fields of two different schemas. BizTalk Server 2000 uses these maps to process and translate data into formats that you can share within your own organization or with your partner organizations.
To explore how the BizTalk Mapper works, let's take the schema we just made and create a map from it to another schema that describes a purchase order.
The Select Destination Specification Type dialog box should now be visible. Repeat the process for the second schema, but find the purchase order that I created on the companion CD. It is in \Samples\Ch12\PurchaseOrder2.xml. Your screen should look like Figure 12-3.
Figure 12-3. Mapping two different purchase order schemas in BizTalk Mapper.
You create a map by dragging from a field on the left to a matching field on the right. The process is about as simple as that, but this tool adds some other important functionality.
Figure 12-4. You create maps by connecting the fields in the Source Specification pane to fields in the Destination Specification pane.
Figure 12-5. Functoids provide many different computational aids for mapping fields between the source and destination files.
Figure 12-6. The multiplication functoid allows you to multiply two numbers in the Source Specification and put the resulting product into a Destination Specification field.
Figure 12-7. The Custom Visual Basic Script functoid allows you to create scripts to assist in transforming a source document into a destination document.
Function mdy2iso8601(dateIn) iFirst = instr(dateIn, "/") iSecond = instr(iFirst + 1, dateIn, "/") iMonth = left(dateIn, iFirst - 1) iDay = mid(dateIn, iFirst + 1, iSecond - iFirst - 1) iYear = mid(dateIn, iSecond + 1, 4) mdy2iso8601 = iYear & "-" & right("0" & iMonth, 2) & _ "-" & right("0" & iDay, 2) End Function |
The mdy2iso8601 function converts a date in the source format (mm/dd/yyyy) into the equivalent that you need. Click OK to close the Functoid Properties dialog box. Your BizTalk Mapper screen should look like the screen in Figure 12-8.
Figure 12-8. The BizTalk Mapper with functoids.
<PurchaseOrder customer="CustomerNumber_1" PODate="2000-04-10"> <LineItem> <Quantity>95</Quantity> <Item>Item_1</Item> <Price>12.95</Price> <Total>1230.25</Total> </LineItem> </PurchaseOrder> |
Notice that the PODate did change from one form to the other, and that the value inside the <Total> tags is the quantity multiplied by the price.
BizTalk Server 2000 will use the style sheet in Listing 12-2 when a document comes in from a trading partner who sends you XML documents by using the schema defined in PurchaseOrder1.xml.
Listing 12-2. XSLT style sheet created by BizTalk Mapper.
PO1-2.xsl <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:msxsl='urn:schemas-microsoft-com:xslt' xmlns:var='urn:var' xmlns:user='urn:user' version='1.0'> <xsl:output method='xml' indent='yes' omit-xml-declaration='yes'/> <xsl:template match='/'> <xsl:apply-templates select='PurchaseOrder'/> </xsl:template> <xsl:template match='PurchaseOrder'> <PurchaseOrder> <xsl:attribute name='customer'> <xsl:value-of select='@CustomerNumber'/> </xsl:attribute> <xsl:variable name='var:v1' select='user:mdy2iso8601(string(@PODate))'/> <xsl:attribute name='PODate'> <xsl:value-of select='$var:v1'/> </xsl:attribute> <xsl:for-each select='Item'> <LineItem> <Quantity> <xsl:value-of select='@Qty'/> </Quantity> <Item> <xsl:value-of select='./text()'/> </Item> <Price> <xsl:value-of select='@Price'/> </Price> <xsl:variable name='var:v2' select='user:fctmathmultiply2(string(@Qty), string(@Price))'/> <Total> <xsl:value-of select='$var:v2'/> </Total> </LineItem> </xsl:for-each> </PurchaseOrder> </xsl:template> <msxsl:script language='VBScript' implements-prefix='user'> <![CDATA[ Function FctMathMultiply2( p_strParm0, p_strParm1 ) If ( IsNumeric( p_strParm0 ) And _ IsNumeric( p_strParm1 ) ) Then FctMathMultiply2 = CStr ( CDbl( p_strParm0 ) _ * CDbl( p_strParm1 ) ) Else FctMathMultiply2 = "" End If End Function Function mdy2iso8601(dateIn) iFirst = instr(dateIn, "/") iSecond = instr(iFirst + 1, dateIn, "/") iMonth = left(dateIn, iFirst - 1) iDay = mid(dateIn, iFirst + 1, iSecond - iFirst - 1) iYear = mid(dateIn, iSecond + 1, 4) mdy2iso8601 = iYear & "-" & _ right("0" & iMonth, 2) & _ "-" & right("0" & iDay, 2) End Function ]]> </msxsl:script> </xsl:stylesheet> |
In addition to XML, the BizTalk Mapper can also read and write to other formats, including two widely used EDI specifications: ANSI X12 and EDIFACT. The BizTalk Mapper also allows mapping to Flat files, XML files defined with the document type definition (DTD) schema syntax, and XDR schemas. Even though the BizTalk Mapper can work with these diverse standards, it is optimized for use with the XML-based BizTalk Framework 2.0.