Convert EDIFACT to X12 or vice-versa

Now that we have the tools to transform XML with XSLT or XQuery, convert to XML, and convert from XML, we should be able to directly build maps between EDIFACT/X12 and XML or XML and EDIFACT/X12 or now even EDI and EDI.

So, let's demonstrate using the Stylus Studio® XSLT mapper to go from an EDIFACT ORDERS "Purchase order message" document in version D03B to a response X12 831 "Application Control Totals" document in release 003040. The use-case would be we need to forward the order quantity and transaction amount from a fulfillment system that speaks in a newer dialect of EDIFACT to an accounting system that speaks an older variant of X12.

Here are the steps we're going to follow: (some of which are optional)

  1. Generate schema for source document EDIFACT or Use instance document
  2. Generate schema for X12 as target
  3. Open XSLT editor mapper tab
  4. Map elements
  5. Set literal text values
  6. Map elements requiring functions
  7. Run the EDI to EDI transform
  8. Post-transformation validation
  9. EDI to XML on the Command Line

After the step-by-step EDI to EDI tutorial here, we'll show the completed XSLT file which you can download and manipulate with Stylus Studio® yourself.

Generate schema for source document EDIFACT or Use instance document

For the source of our transform, we can either use a sample EDIFACT document or use a schema representing the EDIFACT D03B ORDERS structure. Since we've got a document, we're all set. Otherwise, we could follow these steps to generate an XML Schema for an EDIFACT message.

For our demonstration, we'll assume all files are local in the C:\test directory. Our instance document will be the one from edifact-sample.txt that's been used in other demonstrations.

Generate schema for X12 as target

To generate the X12 schema as a target, we use File|Document Wizards...|X12 to XML Schema and choose release 003040 and the 831 transaction set, "Application Control Totals."

When the XML Schema editor opens, save the document as C:\test\3040-831.xsd, or use the copy stashed here on our server. Again, more details on this step can be found in the description of how to generate an XML schema for validating X12 documents.

EDI to EDI Mapping — File|Open|XSLT: Mapper (before) (Click to enlarge)
(click to enlarge)

EDI to EDI Mapping — File|Open|XSLT: Mapper (after) (Click to enlarge)
(click to enlarge)

Open XSLT editor mapper tab

Start a new XSLT map by choosing File|New|XSLT: Mapper.

Click on the Add Source Document button on the top left, and choose the edifact-sample.txt file, but before pressing Open, check the "Convert to XML using converter" box. Next, when prompted for the converter type, choose "Electronic Data Interchange (EDI)" and press OK.

Click on the Set Target Document button on the top right, and choose the 3040-831.xsd XML schema document.

Map elements

Next we map the elements that are exactly the same between the left and the right sides. To do this, we click on the element on the left and drag it to the corresponding element on the right. You can watch the XSLT code (or XQuery, for the XQuery mapper) being updated in real-time in the bottom window.

For example, EDIFACT/UNB/UNB04/UNB0402 is the "0019: date and time of preparation — time" which is in the same format as X12/ISA/ISA10, and and EDIFACT/UNB/UNB05 is the "0020: interchange control reference" which maps to X12/ISA/ISA13 which is the "I12: interchange control number."

(All of that can be gleaned from the EDIFACT dictionary available in the1 Stylus Studio® EDIFACT Zone, and/or the generated schemas which include documentation. X12 documentation can be purchased from DISA directly for a nominal fee, or often the subset necessary is included with the application that produces or consumes the relevant transaction set.)

We proceed in this fashion until all of the fields that have corresponding entries between EDIFACT and X12 are mapped.

EDI to EDI Mapping — Set literal text values (Click to enlarge)
(click to enlarge)

Set literal text values

Some of the values in the output are not going to change between runs, such as the release number and encoding. We need to set those to be literal values. To do this, right click on the node in the target tree on the right, and choose "Set Text Value" from the menu. As an example, choose X12/ISA/ISA12 which contains the release number, and set it to the five-digit sequence "00304". A red T will appear over the node so that you know it has a text value.

Map elements requiring functions

EDI to EDI Mapping — >Map elements requiring functions (Click to enlarge)
(click to enlarge)

Some of the mappings require a little more effort. For example, the dates in the version of EDIFACT we are using are in the YYYYMMDD format, but the X12 release uses YYMMDD.

So, to connect from EDIFACT/UNB/UNB04/UNB0401 to X12/ISA/ISA09, we'd need to insert a substring function to eliminate the first two characters.

To do that, right-click on the canvas in the middle and choose XSLT Functions|substring.

Then draw a line from the UNB0401 to the top port on the left side of the function block. And a line from the port on the right side to the ISA09. Then double-click on the middle port on the left, and put in the starting position of 3, and double-click on the bottom port on the left and set the length to 6.

EDI to EDI — Mapping completed (Click to enlarge)
(click to enlarge)

A few more of those, and the mapping is completed, yielding the completed edifact-to-x12.xsl transform, which you can just copy and paste into your editor from here.

Run the EDI to EDI transform

Okay, press the green triangle, and look at your output.

Note that the preview window itself shows the XML equivalent, you must look at the X12 EDI output by opening it in a text editor, or one of the other editors within Stylus Studio® to look at it.

Below is how the X12 EDI output would look after running our transform.

051107+1159+U+00304+     6002+0+T+:'
+DD+0764569104+XPath 2.0 Programmer?'s Reference'
+DD+0764569090+XSLT 2.0 Programmer?'s Reference'
+DD+1861004656+Java Server Programming'
+DD+0596006756+Enterprise Service Bus'
+1+     6002'

Post-transformation validation

One final optional step is to make sure what we're generating is in fact complete. Using the same 003040 X12 831 XML Schema we used earlier, we can do post-processing validation which will catch any necessary elements we missed or anything we mismapped.

Open the Scenario Properties and choose the Validation tab. Make sure "Validate stylesheete result" is checked, and add the 831 schema, so that it looks like this:

Now whenever you run the transform, the output will automatically be validated for you.

EDI to XML on the Command Line

Just for fun, let's see what it would take to run this same transform from the command line. All of the following would be typed just on one line:

"C:\Program Files\Stylus Studio 2006 XML Enterprise Edition\bin\StylusXslt.exe" -in converter:EDI?edifact-sample.txt edifact-to-x12.xsl -out converter:EDI?x12.txt

The -in parameter sets the input source as the EDI document in the current directory, the -out parameter specifies where to write the output, and in the middle is our transform.


Congratulations! You now have all of the tools you need to map from EDIFACT to X12, or X12 to EDIFACT, or anything to or from XML.

For further reading:


Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Try Stylus EDI Tools

Simplify working with Electronic Data Interchange (EDI) with Stylus Studio®'s award-winning EDI Tools — Download a free trial of our today!

What's New for X15?

New XQuery & Web Services Tools, Support for MySQL, PostgreSQL, HL7 EDI, Microsoft .NET Code Generation and much more!

Why Pay More for XML Tools?

With Stylus Studio® X15 XML Enterprise Suite, you get the most comprehensive XML tool suite at one incredibly low price. Value: it's just one of many reasons why smart XML developers are choosing Stylus Studio!

Free Stylus Studio XML Training:
W3C Member