Order of XML elements

140 views
Skip to first unread message

CreedFeed

unread,
Apr 25, 2013, 3:15:16 PM4/25/13
to bots...@googlegroups.com
Hello, I'm a new user of BOTS and python. I'm trying to setup 850 x12 to XML and I've got the basics working, but I'm running into a problem in terms of how to order the XML elements in the output. I've searched the newsgroup and found a couple of topics relating to my issue but don't quite understand the answers given there, or if they even apply to me. Attached is my grammar and mapping scripts. Here is what my current XML output is:

<?xml version="1.0" encoding="utf-8" ?>
<request>
    <new_order>
        <sender>ABC123</sender>
        <receiver>789XYZ</receiver>
        <testindicator>P</testindicator>
        <docnum>D123456</docnum>
        <auth>
            <key>x12345</key>
        </auth>
    </new_order>
</request>

And here is what I'm trying to get:

<?xml version="1.0" encoding="utf-8" ?>
<request>
    <new_order>
        <auth>
            <key>x12345</key>
        </auth>
        <sender>ABC123</sender>
        <receiver>789XYZ</receiver>
        <testindicator>P</testindicator>
        <docnum>D123456</docnum>
    </new_order>
</request>

Basically, I'd like the <auth> element to show up right after <new_order>. Any ideas of how I would accomplish that?
orders.py
orders_x12_2_xml.py

henk-jan ebbers

unread,
Apr 25, 2013, 6:02:32 PM4/25/13
to bots...@googlegroups.com
CreedFeed (what is a creedfeed?)

see https://groups.google.com/forum/?fromgroups=#!topic/botsmail/IsJlZsN-2F0

same problem, same solution.

kind regards,
henk-jan
> --
> You received this message because you are subscribed to the Google Groups "Bots Open Source EDI Translator" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to botsmail+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

CreedFeed

unread,
May 15, 2013, 3:27:47 PM5/15/13
to bots...@googlegroups.com
I just figured out how this works now. Thanks!

henk-jan ebbers

unread,
May 16, 2013, 6:38:44 AM5/16/13
to bots...@googlegroups.com
hi Steve,

great you found the solution!

is it possible to share your solution on the mailing list?


kind regards,
henk-jan ebbers

On 05/15/2013 09:27 PM, CreedFeed wrote:
> I just figured out how this works now. Thanks! --

CreedFeed

unread,
May 16, 2013, 8:55:34 AM5/16/13
to bots...@googlegroups.com
First off, "CreedFeed" refers to a web site I run/maintain. It was a fan site for the band Creed :)

Here is my solution. Hopefully this is correct and what was being told to do in the other question.

My grammar:

from bots.botsconfig import *

syntax = { 
        'indented':True,
        }

structure = [
    {ID:'request',MIN:1,MAX:1, LEVEL:[
        {ID:'new_order',MIN:1,MAX:1,LEVEL:[
            {ID:'auth',MIN:1,MAX:1,LEVEL:[
                {ID:'key',MIN:1,MAX:1}
            ]},
            {ID:'sender',MIN:1,MAX:1},
            {ID:'receiver',MIN:1,MAX:1},
            {ID:'testindicator',MIN:1,MAX:1},
            {ID:'docnum',MIN:1,MAX:1},
        ]},
    ],QUERIES:{
        'frompartner':  {'BOTSID':'message','sender':None},
        'topartner':    {'BOTSID':'message','receiver':None},
        'testindicator':{'BOTSID':'message','test':None},
    }}
]

recorddefs = {
    'request':[
        ['BOTSID','M',255,'A'],
    ],
    'new_order':[
        ['BOTSID','M',255,'A'],
    ],
    'auth':[
        ['BOTSID','M',255,'A']
    ],
    'key':[
        ['BOTSID','M',255,'A'],
        ['BOTSCONTENT','M',40,'AN']
    ],
    'sender':[
        ['BOTSID','M',255,'A'],
        ['BOTSCONTENT','M',40,'AN']
    ],
    'receiver':[
        ['BOTSID','M',255,'A'],
        ['BOTSCONTENT','M',40,'AN']
    ],
    'testindicator':[
        ['BOTSID','M',255,'A'],
        ['BOTSCONTENT','C', 3, 'AN'],
    ],
    'docnum':[
        ['BOTSID','M',255,'A'],
        ['BOTSCONTENT','M', 35, 'AN'],
    ],
}


My mapping:

#mapping-script
from x12lib import get_art_num          #import x12 specifc helper function
import bots.transform as transform      #import div bots helper functions

def main(inn,out):
    
    out.put({'BOTSID':'request'},{'BOTSID':'new_order'},{'BOTSID':'auth'},{'BOTSID':'key','BOTSCONTENT':'x12345'})
    out.put({'BOTSID':'request'},{'BOTSID':'new_order'},{'BOTSID':'sender','BOTSCONTENT':inn.ta_info['frompartner']})
    out.put({'BOTSID':'request'},{'BOTSID':'new_order'},{'BOTSID':'receiver','BOTSCONTENT':inn.ta_info['topartner']})
    out.put({'BOTSID':'request'},{'BOTSID':'new_order'},{'BOTSID':'testindicator','BOTSCONTENT':inn.ta_info['testindicator']})

    docnum = inn.get({'BOTSID':'ST'},{'BOTSID':'BEG','BEG04':None})
    out.put({'BOTSID':'request'},{'BOTSID':'new_order'},{'BOTSID':'docnum','BOTSCONTENT':docnum})
    inn.ta_info['botskey']=docnum
    out.ta_info['botskey']=docnum


The part I did not quite understand until something just hit me yesterday was what needs to be done with the grammar. If I understand this correctly now, instead of just specifying the main root tags being used in the structure and all of the set fields under the root tag in the record defs, you have to specify ALL the fields in the structure in the order you want, and then in the record defs each field becomes its own main tag with the 'BOTSCONTENT' field specified below it. In the mapping, you apply the values to the tag using the BOTSCONTENT field. This gave me the desired ordering of my fields. Here's the output I got after making this change:

<?xml version="1.0" encoding="utf-8" ?>
<request>
    <new_order>
        <auth>
            <key>x12345</key>
        </auth>
        <sender>abc123</sender>
        <receiver>def456</receiver>
        <testindicator>P</testindicator>
        <docnum>XYZ789</docnum>
    </new_order>
</request>

Please let me know if I did anything wrong here. It seems to work, but perhaps I am missing something that would be more efficient?

henk-jan ebbers

unread,
May 16, 2013, 9:19:58 AM5/16/13
to bots...@googlegroups.com
hi Steve,

great!

thank you,

kind regards,
henk-jan
Reply all
Reply to author
Forward
0 new messages