Home > XML IDE - XML Editor > XML Editor Key Features > EDI Tools > XML to X12
Translating XML documents to X12The following is a demonstration of in-place editing of X12 content as well as programmatic changing of content, all to demonstrate the bidirectional nature of the EDI converter for X12. Using the same steps as in Converting X12 to XML, we're going to edit an X12 document with the XML and then save it back out. We will then compare the before-and-after shots to see the change in the EDI file itself. This will demonstrate an XML document turning back into X12. Then we'll see how this can be automated using XSLT. Here are the steps for this demonstration:
Open the X12 document in the XML editorPerform the following steps:
Change XML X12Since business has been so good, we're going to double our order quantities. (By the way, the 41 value in "Number of batches" means that the quantity is a count of batches, not that there are 41 batches. "41" is the X12 quantity code for "number of batches" — got it?) Before:
<QTY> After:
<QTY> Save XML document in X12 formatDo See changed X12Using a 'diff' program to do a side-by-side comparison of the original file with the changed file shows that we were able to alter the X12 file without an X12-specific editor, or even opening the raw X12 file itself! Use X12 document as output of XSLTLet's take a moment and automate what we did — using the input document with a few adjustments to make an output X12 document. Either XSLT or XQuery will do, and we'll just arbitrarily choose the former for our demonstration. Again, what we are going to accomplish is to take an existing X12 document, and save it as a new X12 document with a few modifications.
<xsl:stylesheet version="1.0">
<xsl:template match="/"> <xsl:processing-instruction name="edi_segment">~</xsl:processing-instruction> <xsl:processing-instruction name="edi_element">:</xsl:processing-instruction> <xsl:apply-templates select="node()"/> </xsl:template> <xsl:template match="QTY02"> <xsl:element name="{local-name(.)}"> <xsl:value-of select=". * 2"/> </xsl:element> </xsl:template> <xsl:template match="node()"> <xsl:choose> <xsl:when test="local-name(.) = '#text'"> <xsl:value-of select="."/> </xsl:when> <xsl:when test="local-name(.) = '#comment'"/> <xsl:otherwise> <xsl:element name="{local-name(.)}"> <xsl:apply-templates select="node()"/> </xsl:element> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet> What this does is the same thing we just did, but mechanically. It finds the
You may have noticed two processing instruction lines being emitted. By default, the characters for segment terminator and element separator are different in the converter than they were in the input file. We can override them by setting them in the output stream, which we did to make our 'diff' easier to read. In case you are curious, here are the PI directives that can be given, along with their default values:
<?edi_component : ?> These also apply to the XML to EDIFACT converter. The X12 can be generated completely from either XQuery or XSLT, and schemas can be generated against which to map and validate your X12 XML. The converters are not limited to use within Stylus Studio®. They may be integrated into your own applications directly, using the data conversion API, or included as part of your file handling or XSLT/XQuery processing through the XML Converters.
|
PURCHASE STYLUS STUDIO ONLINE TODAY!!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Try Stylus EDI ToolsSimplify working with Electronic Data Interchange (EDI) with Stylus Studio®'s award-winning EDI Tools — Download a free trial of our today! Attend a Live Webinar This Week!Learn about Stylus Studio's unique features and benefits in just under an hour. Register for the Stylus Studio QuickStart Training WebCast! Ask Someone You KnowDoes your company use Stylus Studio? Do your competitors? Engineers from over 100,000 leading companies use Stylus Studio, and now you can ask someone from your own organization about their experiences using Stylus Studio. Related XSD Features & Tutorials |