XML namespace - how to remove ns0

2,884 views
Skip to first unread message

duff...@gmail.com

unread,
Nov 7, 2014, 8:46:01 AM11/7/14
to bots...@googlegroups.com
Hi
I am doing an XML to XML translation which is working fine, however the output XML has the ns0 before every tag, which I want to remove and not have anything there (as the input XML is)

I have read all the XML namespace documentation and posts here but can't get it to work. I dont want to replace the ns0 with anything else, just remove it and just have the tags as normal.

I have used xmlns in the grammar and set it to the correct NS from the input XML. Should this just work as is, or is there something in the mapping script I need to do?

Thanks

BikeMike

unread,
Nov 7, 2014, 7:19:40 PM11/7/14
to bots...@googlegroups.com
XML namespaces are a real pain!

Some of the posts you probably found are mine, I have run into these problems before. I recently worked out a new simpler way to "ignore" namespaces on incoming files so that bots never sees them, using a simple preprocessing script. Using this method, your grammars and mapping scripts are simpler and no namespage is used in the output :-))

Of course if your partner requires namespaces in the output you need to deal with them and can't do it this way!

#-------------------------------------------------------------------------------
# preprocess - Replace 'xmlns=' with 'xmlns:NOTUSED=' to simplify grammar and mapping
# Generally Bots does not need to use the xmlns for incoming files
# Also replace named prefix 'ENV:' used in HIWG xml orders
# Import this into routescript like this:
#   from routelib import postincommunication_remove_xmlns as postincommunication

def postincommunication_xmlns(routedict):

   
def _preprocess(ta_from,endstatus,**argv):

       
# copy ta for preprocessing
        ta_to
= ta_from.copyta(status=endstatus)

       
# open the files
        infile
= botslib.opendata(ta_from.filename,'r')
        tofile
= botslib.opendata(str(ta_to.idta),'wb')

       
for line in infile:
            tofile
.write(line.replace('xmlns=','xmlns:NOTUSED=').replace('<ENV:','<').replace('</ENV:','</'))

       
# close files and update outmessage transaction
        infile
.close()
        tofile
.close()
        ta_to
.update(statust=OK,filename=str(ta_to.idta))

    preprocess
.preprocess(routedict,_preprocess)


Kind Regards,
Mike

Duff

unread,
Nov 10, 2014, 9:00:09 AM11/10/14
to bots...@googlegroups.com
Hi Mike

Thanks for the reply. Yes they are a real pain!

Can you please clarify, if I use your preprocessing script does that mean I would need to remove any "xmlns" references in the grammar and the mapping scripts?

Currently, I have followed the instructions here (https://code.google.com/p/bots/wiki/XMLnamespace) and put the xmlns=mynamespace in the grammar and then used xmlns+'FIELD' in the mapping script.

Thanks again

BikeMike

unread,
Nov 10, 2014, 6:20:05 PM11/10/14
to bots...@googlegroups.com
Hi Duff,
Yes you can remove all xmlns references in both grammar and mapping :-)
That is why I think this method is much better if you do not require any xmlns in the output (or mapping from xml to something else).

In effect you are assigning the namespace to a prefix "NOTUSED", and since no xml tags in the incoming file have that prefix, the namespace is effectively ignored.

Kind Regards,
Mike
Reply all
Reply to author
Forward
0 new messages