Colons in the XML

69 views
Skip to first unread message

George Harris

unread,
Apr 22, 2016, 4:28:31 AM4/22/16
to A gathering place for the Open Rail Data community
Hey all,

This is probably a simple problem to fix but I've been frantically googling and can't find anything to fix the problem I've been having.

I'm currently using Python to receive the feed from the Darwin push ports and while I can easily get python to print certain elements of the XML I'm struggling to access the any that have a colon for example from the below "ns3:Location".

I have been using xmltodict,

I'm fairly new to XML never really touched it so any help would be greatly appreciated any links for further reading :D.

The code I am using:

p = xmltodict.parse(XML)
uid = p['Pport']['uR']['TS']['@uid']
ssd = p['Pport']['uR']['TS']['@ssd']
print uid, ssd

If I try using the above to access the tiploc

 locationA = p['Pport']['uR']['TS']['ns3:Location']['@tpl']
 print uid, ssd, locationA

This sometimes prints out the tipoc but other times I receive the error message "typeError: list indices must be integers, not str"

<?xml version="1.0" encoding="UTF-8"?>
<Pport
    xmlns="http://www.thalesgroup.com/rtti/PushPort/v12"
    xmlns:ns3="http://www.thalesgroup.com/rtti/PushPort/Forecasts/v2" ts="2016-04-22T08:23:41.1016956+01:00" version="12.0">
    <uR updateOrigin="TD">
        <TS rid="201604221220927" ssd="2016-04-22" uid="W02997">
            <ns3:Location pta="08:28" tpl="CANONST" wta="08:26">
                <ns3:arr at="08:24" src="TD"/>
            </ns3:Location>
        </TS>
    </uR>
</Pport>

petermount

unread,
Apr 22, 2016, 4:44:14 AM4/22/16
to A gathering place for the Open Rail Data community
The colon's separate the name space from the local name. In xml an element and it's attributes are defined within a namespace. In this case ns3. However ns3 is an alias as it's defined in the root. For example:

<Pport xmlns="http://www.thalesgroup.com/rtti/PushPort/v12" xmlns:ns3="http://www.thalesgroup.com/rtti/PushPort/Forecasts/v2" ts="2016-04-09T04:56:00.0474979+01:00" version="12.0"><uR updateOrigin="Darwin"><TS rid="201604080778827" ssd="2016-04-08" uid="L83640"><ns3:Location tpl="CAMBDCS" wta="23:01"><ns3:arr delayed="true" et="04:56" src="Darwin"/></ns3:Location></TS></uR></Pport>

so in Pport ns3 is defined by  xmlns:ns3="http://www.thalesgroup.com/rtti/PushPort/Forecasts/v2" and that points to the relevant schema which defines what is valid. In this case it's "http://www.thalesgroup.com/rtti/PushPort/Forecasts/v2" (note: not a URL can be almost anything).

Be aware, it's called ns3 here but it could change. The key is ns3:xxx needs an xmlns:ns3 higher up (not necessarily in the root). The best way is to use a proper xml parser rather than writing your own, as parsing xml isn't as easy as it looks (been there :-S )

Reply all
Reply to author
Forward
0 new messages