XML Data Extraction

Not strictly OpenHAB but maybe someone has already done what I’m looking at. I commute daily by train, as with all public transport it’s not always operating as you’d expect. After a little research I’ve found a good source that can provide train data in an XML format. There’s an example below. What I’m looking to achieve is to extract the current status of a certain train, these are identifies in the first line; “1B22” in the example.

Has anyone done any work with extracting information from XML files and imported it in to OpenHAB, I’m only looking at certain trains at certain times. So that when I get up and dressed in the morning I’ll have an idea if the train is running on time or not.

Thanks for looking.

<Service Headcode="1B22" Uid="C20052" RetailID="" TigerID="1">
<ServiceType Type="Through"/>
<ArriveTime time="1112" Arrived="No" timestamp="20170206111200"/>
<DepartTime time="1113" timestamp="20170206111300" sorttimestamp="20170206111300"/>
<Platform Number="4" Changed="No" Parent="4"/>
<SecondaryServiceStatus/>
<ServiceStatus Status="On time"/>
<ExpectedDepartTime time="On time"/>
<ExpectedArriveTime time="On time"/>
<Delay Minutes="0"/>
<ExpectedDepartStatus time="1115"/>
<ExpectedArriveStatus time="1113"/>
<DelayCause/>
<LastReport tiploc="" time="1112" type="T" station1="Swindon" station2=""/>
<CommentLine>Formed of 8 coaches</CommentLine>
<CommentLine2>First Class at the REAR</CommentLine2>
<ArrivalComment1/>
<ArrivalComment2/>
<PlatformComment1>Formed of 8 coaches</PlatformComment1>
<PlatformComment2>First Class at the REAR</PlatformComment2>
<DepartureComment1/>
<DepartureComment2/>
<AssociatedPageNotices/>
<ChangeAt/>
<Operator name="GWR service" code="GW" brand=""/>
<Origin1 name="London Paddington" tiploc="PADTON" crs="PAD"/>
<Destination1 name="Cardiff Central" tiploc="CRDFCEN" crs="CDF" ttarr="1221" etarr="1221"/>
<Via/>
<Coaches1/>
<Incident/>
<Dest1CallingPoints NumCallingPoints="2">
<CallingPoint Name="Bristol Parkway" tiploc="BRSTPWY" crs="BPW" ttarr="1142" ttdep="1143" etarr="1142" etdep="1143" type="T"/>
<CallingPoint Name="Newport (South Wales)" tiploc="NWPTRTG" crs="NWP" ttarr="1204" ttdep="1206" etarr="1204" etdep="1206" type="T"/>
</Dest1CallingPoints>
</Service>

You have two transforms to choose from: XPATH and XSLT.

In this case I think XPATH is the right choice. So assuming you are using the HTTP binding your Item would looks something like:

String TrainStatus "1B22 is currently [%s]" <train> { http="<[http://url:3600000:XPATH(//Service[@Headcode="1B22"]/ServiceStatus/@Status)]" }

Clearly you need to replace the “url” with the URL to this XML. The 3,600,000 is basically check every hour. You will want to make that an appropriate value OR you can instead use the http actions in a rule triggered by a cron (lets say you only care to update this once per morning) with something like:

rule "Get Train Status"
when
    Time cron "0 0 6 * * * ?"
then
    val xml = sendHttpGetRequest("http://url")
    val status = transform(xml, "xpath", "//Service[@Headcode="1B22"]/ServiceStatus/@Status")

    // process the status, send alert, etc
    TrainStatus.postUpdate(status)
end

Note, I just typed the above in. It likely contains typos and mistakes.

Thanks, I’ll give that a go later on an will report back if I get stuck.

Getting this error below, using the train code of 1C36 for testing.

2017-02-08 21:08:00.394 [ERROR] [.o.m.r.i.engine.ExecuteRuleJob] - Error during the execution of rule Get Train Status
java.lang.RuntimeException: The name ‘C36’ cannot be resolved to an item or type.

Here’s the full URL; http://iris2.rail.co.uk/tiger/30353A.xml

I couldn’t see that code in the XML (at the time of checking - I don’t know if codes disappear throughout the day) so I would assume that maybe the XPATH is failing to return a value.

Thanks for taking a look @danielwalters86. Yes the codes do disappear as the service completes. For example if you check at 6pm for a train that has already arrived at it’s destination then it will no longer be reported in the XML view.

I think the problem is down to the transformation that @rlkoshak has kindly provided above. As I know very little about XML or how to use transformations in OpenHAB I’m at a lose on how to trouble shoot the problem further. It could be that I didn’t provide enough information/data in the first instance and what Rich provided was designed to work with the data I originally provided.

The URL I provided above will provide the as is information when it is called. Each train service starts with ; ` Where 1B22 is the service definition and will change depending on the starting and finishing locations.

Thanks once again for helping. `

The XPath above is indeed only for the XML provided. If there are elements above the Service element you need to include them in the path.

If I understand what your saying correctly then this method isn’t going to work, sadly. The XML files changes on a regular basis. Here’s one from right now.

If the train code I’m looking for is; Service Headcode=“2B96” as you can see that’s some way down the file, If I understand you, that means everything above that needs to be in the xpath for it to work. Is that correct?

Once again, thanks for your help.

<StationBoard name="Swindon" tiploc="SDON" crs="SWI" PlatformData="Yes" Timestamp="09/02/2017 16:09:38">
<TridentId>88428468</TridentId>
<Service Headcode="1A23" Uid="C20022" RetailID="" TigerID="1">
<ServiceType Type="Through"/>
<ArriveTime time="1610" Arrived="No" timestamp="20170209161000"/>
<DepartTime time="1611" timestamp="20170209161100" sorttimestamp="20170209161100"/>
<Platform Number="1" Changed="No" Parent="1"/>
<SecondaryServiceStatus/>
<ServiceStatus Status="On time"/>
<ExpectedDepartTime time="On time"/>
<ExpectedArriveTime time="On time"/>
<Delay Minutes="0"/>
<ExpectedDepartStatus time="On time"/>
<ExpectedArriveStatus time="On time"/>
<DelayCause/>
<LastReport tiploc="" time="1608" type="T" station1="Swindon" station2=""/>
<CommentLine/>
<CommentLine2>First Class at the REAR</CommentLine2>
<ArrivalComment1/>
<ArrivalComment2/>
<PlatformComment1/>
<PlatformComment2>First Class at the REAR</PlatformComment2>
<DepartureComment1>First Class at the REAR</DepartureComment1>
<DepartureComment2>Calls at Didcot Parkway</DepartureComment2>
<AssociatedPageNotices/>
<ChangeAt/>
<Operator name="GWR service" code="GW" brand=""/>
<Origin1 name="Bristol Temple Meads" tiploc="BRSTLTM" crs="BRI"/>
<Destination1 name="London Paddington" tiploc="PADTON" crs="PAD" ttarr="1714" etarr="1714"/>
<Via/>
<Coaches1/>
<Incident/>
<Dest1CallingPoints NumCallingPoints="2">
<CallingPoint Name="Didcot Parkway" tiploc="DIDCOTP" crs="DID" ttarr="1628" ttdep="1629" etarr="1628" etdep="1629" type="T"/>
<CallingPoint Name="Reading" tiploc="RDNGSTN" crs="RDG" ttarr="1643" ttdep="1645" etarr="1643" etdep="1645" type="T"/>
</Dest1CallingPoints>
</Service>
<Service Headcode="1B48" Uid="C20062" RetailID="" TigerID="2">
<ServiceType Type="Through"/>
<ArriveTime time="1612" Arrived="No" timestamp="20170209161200"/>
<DepartTime time="1614" timestamp="20170209161400" sorttimestamp="20170209161400"/>
<Platform Number="4" Changed="No" Parent="4"/>
<SecondaryServiceStatus/>
<ServiceStatus Status="On time"/>
<ExpectedDepartTime time="On time"/>
<ExpectedArriveTime time="On time"/>
<Delay Minutes="0"/>
<ExpectedDepartStatus time="On time"/>
<ExpectedArriveStatus time="On time"/>
<DelayCause/>
<LastReport tiploc="" time="1557" type="B" station1="Didcot Parkway" station2="Swindon"/>
<CommentLine/>
<CommentLine2>First Class at the REAR</CommentLine2>
<ArrivalComment1/>
<ArrivalComment2/>
<PlatformComment1/>
<PlatformComment2>First Class at the REAR</PlatformComment2>
<DepartureComment1/>
<DepartureComment2/>
<AssociatedPageNotices/>
<ChangeAt/>
<Operator name="GWR service" code="GW" brand=""/>
<Origin1 name="London Paddington" tiploc="PADTON" crs="PAD"/>
<Destination1 name="Cardiff Central" tiploc="CRDFCEN" crs="CDF" ttarr="1723" etarr="1723"/>
<Via/>
<Coaches1/>
<Incident/>
<Dest1CallingPoints NumCallingPoints="2">
<CallingPoint Name="Bristol Parkway" tiploc="BRSTPWY" crs="BPW" ttarr="1638" ttdep="1640" etarr="1638" etdep="1640" type="T"/>
<CallingPoint Name="Newport (South Wales)" tiploc="NWPTRTG" crs="NWP" ttarr="1706" ttdep="1706" etarr="1706" etdep="1706" type="T"/>
</Dest1CallingPoints>
</Service>
<Service Headcode="2B96" Uid="C20478" RetailID="" TigerID="0">
<ServiceType Type="Terminating"/>
<ArriveTime time="1624" Arrived="No" timestamp="20170209162400"/>
<DepartTime time="" timestamp="" sorttimestamp="20170209162400"/>
<Platform Number="2" Changed="No" Parent="2"/>
<SecondaryServiceStatus/>
<ServiceStatus Status="On time"/>
<ExpectedDepartTime time=""/>
<ExpectedArriveTime time="On time"/>
<Delay Minutes="0"/>
<ExpectedDepartStatus time=""/>
<ExpectedArriveStatus time="On time"/>
<DelayCause/>
<LastReport tiploc="" time="1606" type="T" station1="Kemble" station2=""/>
<CommentLine/>
<CommentLine2/>
<ArrivalComment1/>
<ArrivalComment2/>
<PlatformComment1/>
<PlatformComment2/>
<DepartureComment1/>
<DepartureComment2/>
<AssociatedPageNotices/>
<ChangeAt/>
<Operator name="GWR service" code="GW" brand=""/>
<Origin1 name="Cheltenham Spa" tiploc="CHLTNHM" crs="CNM"/>
<Destination1 name="**Terminates**" tiploc="SDON" crs="SWI" ttarr="1624" etarr="1624"/>
<Via/>
<Coaches1/>
<Incident/>
<Dest1CallingPoints NumCallingPoints="0"></Dest1CallingPoints>
</Service>
<Service Headcode="1L71" Uid="C20203" RetailID="" TigerID="3">
<ServiceType Type="Through"/>
<ArriveTime time="1627" Arrived="No" timestamp="20170209162700"/>
<DepartTime time="1629" timestamp="20170209162900" sorttimestamp="20170209162900"/>
<Platform Number="3" Changed="No" Parent="3"/>
<SecondaryServiceStatus/>
<ServiceStatus Status="On time"/>
<ExpectedDepartTime time="On time"/>
<ExpectedArriveTime time="On time"/>
<Delay Minutes="0"/>
<ExpectedDepartStatus time="On time"/>
<ExpectedArriveStatus time="On time"/>
<DelayCause/>
<LastReport tiploc="" time="1606" type="B" station1="Bristol Parkway" station2="Swindon"/>
<CommentLine/>
<CommentLine2>First Class at the FRONT</CommentLine2>
<ArrivalComment1/>
<ArrivalComment2/>
<PlatformComment1/>
<PlatformComment2>First Class at the FRONT</PlatformComment2>
<DepartureComment1/>
<DepartureComment2>Calls at Didcot Parkway</DepartureComment2>
<AssociatedPageNotices/>
<ChangeAt/>
<Operator name="GWR service" code="GW" brand=""/>
<Origin1 name="Swansea" tiploc="SWANSEA" crs="SWA"/>
<Destination1 name="London Paddington" tiploc="PADTON" crs="PAD" ttarr="1730" etarr="1730"/>
<Via/>
<Coaches1/>
<Incident/>
<Dest1CallingPoints NumCallingPoints="2">
<CallingPoint Name="Didcot Parkway" tiploc="DIDCOTP" crs="DID" ttarr="1645" ttdep="1647" etarr="1645" etdep="1647" type="T"/>
<CallingPoint Name="Reading" tiploc="RDNGSTN" crs="RDG" ttarr="1701" ttdep="1703" etarr="1701" etdep="1703" type="T"/>
</Dest1CallingPoints>
</Service>
<Service Headcode="1C20" Uid="C20085" RetailID="" TigerID="4">
<ServiceType Type="Through"/>
<ArriveTime time="1629" Arrived="No" timestamp="20170209162900"/>
<DepartTime time="1629" timestamp="20170209162900" sorttimestamp="20170209162900"/>
<Platform Number="4" Changed="No" Parent="4"/>
<SecondaryServiceStatus/>
<ServiceStatus Status="On time"/>
<ExpectedDepartTime time="On time"/>
<ExpectedArriveTime time="On time"/>
<Delay Minutes="0"/>
<ExpectedDepartStatus time="On time"/>
<ExpectedArriveStatus time="On time"/>
<DelayCause/>
<LastReport tiploc="" time="1608" type="T" station1="Didcot Parkway" station2=""/>
<CommentLine/>
<CommentLine2>First Class at the REAR</CommentLine2>
<ArrivalComment1/>
<ArrivalComment2/>
<PlatformComment1/>
<PlatformComment2>First Class at the REAR</PlatformComment2>
<DepartureComment1/>
<DepartureComment2/>
<AssociatedPageNotices/>
<ChangeAt/>
<Operator name="GWR service" code="GW" brand=""/>
<Origin1 name="London Paddington" tiploc="PADTON" crs="PAD"/>
<Destination1 name="Weston-super-Mare" tiploc="WSMARE" crs="WSM" ttarr="1752" etarr="1752"/>
<Via/>
<Coaches1/>
<Incident/>
<Dest1CallingPoints NumCallingPoints="6">
<CallingPoint Name="Chippenham" tiploc="CHIPNHM" crs="CPM" ttarr="1643" ttdep="1644" etarr="1643" etdep="1644" type="T"/>
<CallingPoint Name="Bath Spa" tiploc="BATHSPA" crs="BTH" ttarr="1658" ttdep="1659" etarr="1658" etdep="1659" type="T"/>
<CallingPoint Name="Bristol Temple Meads" tiploc="BRSTLTM" crs="BRI" ttarr="1714" ttdep="1717" etarr="1714" etdep="1717" type="T"/>
<CallingPoint Name="Nailsea & Backwell" tiploc="NAILSEA" crs="NLS" ttarr="1726" ttdep="1727" etarr="1726" etdep="1727" type="T"/>
<CallingPoint Name="Yatton" tiploc="YATTON" crs="YAT" ttarr="1733" ttdep="1733" etarr="1733" etdep="1733" type="T"/>
<CallingPoint Name="Worle" tiploc="WORLE" crs="WOR" ttarr="1740" ttdep="1740" etarr="1740" etdep="1740" type="T"/>
</Dest1CallingPoints>
</Service>
<Service Headcode="1G47" Uid="C20151" RetailID="" TigerID="5">
<ServiceType Type="Through"/>
<ArriveTime time="1636" Arrived="No" timestamp="20170209163600"/>
<DepartTime time="1638" timestamp="20170209163800" sorttimestamp="20170209163800"/>
<Platform Number="1" Changed="No" Parent="1"/>
<SecondaryServiceStatus/>
<ServiceStatus Status="On time"/>
<ExpectedDepartTime time="On time"/>
<ExpectedArriveTime time="On time"/>
<Delay Minutes="0"/>
<ExpectedDepartStatus time="On time"/>
<ExpectedArriveStatus time="On time"/>
<DelayCause/>
<LastReport tiploc="" time="1608" type="B" station1="Goring & Streatley" station2="Cholsey"/>
<CommentLine/>
<CommentLine2>First Class at the REAR</CommentLine2>
<ArrivalComment1/>
<ArrivalComment2/>
<PlatformComment1/>
<PlatformComment2>First Class at the REAR</PlatformComment2>
<DepartureComment1/>
<DepartureComment2/>
<AssociatedPageNotices/>
<ChangeAt/>
<Operator name="GWR service" code="GW" brand=""/>
<Origin1 name="London Paddington" tiploc="PADTON" crs="PAD"/>
<Destination1 name="Cheltenham Spa" tiploc="CHLTNHM" crs="CNM" ttarr="1752" etarr="1752"/>
<Via/>
<Coaches1/>
<Incident/>
<Dest1CallingPoints NumCallingPoints="4">
<CallingPoint Name="Kemble" tiploc="KEMBLE" crs="KEM" ttarr="1650" ttdep="1652" etarr="1650" etdep="1652" type="T"/>
<CallingPoint Name="Stroud" tiploc="STRUD" crs="STD" ttarr="1706" ttdep="1708" etarr="1706" etdep="1708" type="T"/>
<CallingPoint Name="Stonehouse" tiploc="STNHSE" crs="SHU" ttarr="1712" ttdep="1714" etarr="1712" etdep="1714" type="T"/>
<CallingPoint Name="Gloucester" tiploc="GLOSTER" crs="GCR" ttarr="1729" ttdep="1740" etarr="1729" etdep="1740" type="T"/>
</Dest1CallingPoints>
</Service>
<Service Headcode="1A24" Uid="C20023" RetailID="" TigerID="6">
<ServiceType Type="Through"/>
<ArriveTime time="1640" Arrived="No" timestamp="20170209164000"/>
<DepartTime time="1641" timestamp="20170209164100" sorttimestamp="20170209164100"/>
<Platform Number="3" Changed="No" Parent="3"/>
<SecondaryServiceStatus/>
<ServiceStatus Status="On time"/>
<ExpectedDepartTime time="On time"/>
<ExpectedArriveTime time="On time"/>
<Delay Minutes="0"/>
<ExpectedDepartStatus time="On time"/>
<ExpectedArriveStatus time="On time"/>
<DelayCause/>
<LastReport tiploc="" time="1609" type="T" station1="Oldfield Park" station2=""/>
<CommentLine/>
<CommentLine2>First Class at the FRONT</CommentLine2>
<ArrivalComment1/>
<ArrivalComment2/>
<PlatformComment1/>
<PlatformComment2>First Class at the FRONT</PlatformComment2>
<DepartureComment1/>
<DepartureComment2/>
<AssociatedPageNotices>The Merchant Venturer</AssociatedPageNotices>
<ChangeAt/>
<Operator name="GWR service" code="GW" brand=""/>
<Origin1 name="Bristol Temple Meads" tiploc="BRSTLTM" crs="BRI"/>
<Destination1 name="London Paddington" tiploc="PADTON" crs="PAD" ttarr="1737" etarr="1737"/>
<Via/>
<Coaches1/>
<Incident/>
<Dest1CallingPoints NumCallingPoints="1">
<CallingPoint Name="Reading" tiploc="RDNGSTN" crs="RDG" ttarr="1708" ttdep="1711" etarr="1708" etdep="1711" type="T"/>
</Dest1CallingPoints>
</Service>
<Service Headcode="1B51" Uid="C20063" RetailID="" TigerID="7">
<ServiceType Type="Through"/>
<ArriveTime time="1640" Arrived="No" timestamp="20170209164000"/>
<DepartTime time="1641" timestamp="20170209164100" sorttimestamp="20170209164100"/>
<Platform Number="4" Changed="No" Parent="4"/>
<SecondaryServiceStatus/>
<ServiceStatus Status="On time"/>
<ExpectedDepartTime time="On time"/>
<ExpectedArriveTime time="On time"/>
<Delay Minutes="0"/>
<ExpectedDepartStatus time="On time"/>
<ExpectedArriveStatus time="On time"/>
<DelayCause/>
<LastReport tiploc="" time="1607" type="T" station1="Reading" station2=""/>
<CommentLine/>
<CommentLine2>First Class at the REAR</CommentLine2>
<ArrivalComment1/>
<ArrivalComment2/>
<PlatformComment1/>
<PlatformComment2>First Class at the REAR</PlatformComment2>
<DepartureComment1/>
<DepartureComment2/>
<AssociatedPageNotices/>
<ChangeAt/>
<Operator name="GWR service" code="GW" brand=""/>
<Origin1 name="London Paddington" tiploc="PADTON" crs="PAD"/>
<Destination1 name="Swansea" tiploc="SWANSEA" crs="SWA" ttarr="1845" etarr="1845"/>
<Via/>
<Coaches1/>
<Incident/>
<Dest1CallingPoints NumCallingPoints="6">
<CallingPoint Name="Bristol Parkway" tiploc="BRSTPWY" crs="BPW" ttarr="1706" ttdep="1708" etarr="1706" etdep="1708" type="T"/>
<CallingPoint Name="Newport (South Wales)" tiploc="NWPTRTG" crs="NWP" ttarr="1732" ttdep="1733" etarr="1732" etdep="1733" type="T"/>
<CallingPoint Name="Cardiff Central" tiploc="CRDFCEN" crs="CDF" ttarr="1748" ttdep="1750" etarr="1748" etdep="1750" type="T"/>
<CallingPoint Name="Bridgend" tiploc="BRGEND" crs="BGN" ttarr="1811" ttdep="1812" etarr="1811" etdep="1812" type="T"/>
<CallingPoint Name="Port Talbot Parkway" tiploc="PTALBOT" crs="PTA" ttarr="1824" ttdep="1825" etarr="1824" etdep="1825" type="T"/>
<CallingPoint Name="Neath" tiploc="NEATH" crs="NTH" ttarr="1830" ttdep="1831" etarr="1830" etdep="1831" type="T"/>
</Dest1CallingPoints>
</Service>
<Service Headcode="1C21" Uid="C20086" RetailID="" TigerID="8">
<ServiceType Type="Through"/>
<ArriveTime time="1655" Arrived="No" timestamp="20170209165500"/>
<DepartTime time="1657" timestamp="20170209165700" sorttimestamp="20170209165700"/>
<Platform Number="4" Changed="No" Parent="4"/>
<SecondaryServiceStatus/>
<ServiceStatus Status="On time"/>
<ExpectedDepartTime time="On time"/>
<ExpectedArriveTime time="On time"/>
<Delay Minutes="0"/>
<ExpectedDepartStatus time="On time"/>
<ExpectedArriveStatus time="On time"/>
<DelayCause/>
<LastReport tiploc="" time="1609" type="B" station1="Hayes & Harlington" station2="West Drayton"/>
<CommentLine/>
<CommentLine2>First Class at the REAR</CommentLine2>
<ArrivalComment1/>
<ArrivalComment2/>
<PlatformComment1/>
<PlatformComment2>First Class at the REAR</PlatformComment2>
<DepartureComment1/>
<DepartureComment2/>
<AssociatedPageNotices/>
<ChangeAt/>
<Operator name="GWR service" code="GW" brand=""/>
<Origin1 name="London Paddington" tiploc="PADTON" crs="PAD"/>
<Destination1 name="Bristol Temple Meads" tiploc="BRSTLTM" crs="BRI" ttarr="1739" etarr="1739"/>
<Via/>
<Coaches1/>
<Incident/>
<Dest1CallingPoints NumCallingPoints="2">
<CallingPoint Name="Chippenham" tiploc="CHIPNHM" crs="CPM" ttarr="1710" ttdep="1711" etarr="1710" etdep="1711" type="T"/>
<CallingPoint Name="Bath Spa" tiploc="BATHSPA" crs="BTH" ttarr="1724" ttdep="1726" etarr="1724" etdep="1726" type="T"/>
</Dest1CallingPoints>
</Service>
<Service Headcode="1L74" Uid="C20204" RetailID="" TigerID="9">
<ServiceType Type="Through"/>
<ArriveTime time="1657" Arrived="No" timestamp="20170209165700"/>
<DepartTime time="1659" timestamp="20170209165900" sorttimestamp="20170209165900"/>
<Platform Number="3" Changed="No" Parent="3"/>
<SecondaryServiceStatus/>
<ServiceStatus Status="On time"/>
<ExpectedDepartTime time="On time"/>
<ExpectedArriveTime time="On time"/>
<Delay Minutes="0"/>
<ExpectedDepartStatus time="On time"/>
<ExpectedArriveStatus time="On time"/>
<DelayCause/>
<LastReport tiploc="" time="1607" type="T" station1="Newport (South Wales)" station2=""/>
<CommentLine/>
<CommentLine2>First Class at the FRONT</CommentLine2>
<ArrivalComment1/>
<ArrivalComment2/>
<PlatformComment1/>
<PlatformComment2>First Class at the FRONT</PlatformComment2>
<DepartureComment1/>
<DepartureComment2>Calls at Didcot Parkway</DepartureComment2>
<AssociatedPageNotices/>
<ChangeAt/>
<Operator name="GWR service" code="GW" brand=""/>
<Origin1 name="Cardiff Central" tiploc="CRDFCEN" crs="CDF"/>
<Destination1 name="London Paddington" tiploc="PADTON" crs="PAD" ttarr="1809" etarr="1809"/>
<Via/>
<Coaches1/>
<Incident/>
<Dest1CallingPoints NumCallingPoints="3">
<CallingPoint Name="Didcot Parkway" tiploc="DIDCOTP" crs="DID" ttarr="1716" ttdep="1716" etarr="1716" etdep="1716" type="T"/>
<CallingPoint Name="Reading" tiploc="RDNGSTN" crs="RDG" ttarr="1731" ttdep="1735" etarr="1731" etdep="1735" type="T"/>
<CallingPoint Name="Slough" tiploc="SLOUGH" crs="SLO" ttarr="1748" ttdep="1750" etarr="1748" etdep="1750" type="T"/>
</Dest1CallingPoints>
</Service>
<Service Headcode="2M14" Uid="C20877" RetailID="" TigerID="0">
<ServiceType Type="Terminating"/>
<ArriveTime time="1706" Arrived="No" timestamp="20170209170600"/>
<DepartTime time="" timestamp="" sorttimestamp="20170209170600"/>
<Platform Number="2" Changed="No" Parent="2"/>
<SecondaryServiceStatus/>
<ServiceStatus Status="On time"/>
<ExpectedDepartTime time=""/>
<ExpectedArriveTime time="On time"/>
<Delay Minutes="0"/>
<ExpectedDepartStatus time=""/>
<ExpectedArriveStatus time="On time"/>
<DelayCause/>
<LastReport tiploc="" time="" type="" station1="" station2=""/>
<CommentLine/>
<CommentLine2/>
<ArrivalComment1/>
<ArrivalComment2/>
<PlatformComment1/>
<PlatformComment2/>
<DepartureComment1/>
<DepartureComment2/>
<AssociatedPageNotices/>
<ChangeAt/>
<Operator name="GWR service" code="GW" brand=""/>
<Origin1 name="Westbury" tiploc="WSTBRYW" crs="WSB"/>
<Destination1 name="**Terminates**" tiploc="SDON" crs="SWI" ttarr="1706" etarr="1706"/>
<Via/>
<Coaches1/>
<Incident/>
<Dest1CallingPoints NumCallingPoints="0"></Dest1CallingPoints>
</Service>
<Service Headcode="1A25" Uid="C20024" RetailID="" TigerID="10">
<ServiceType Type="Through"/>
<ArriveTime time="1710" Arrived="No" timestamp="20170209171000"/>
<DepartTime time="1711" timestamp="20170209171100" sorttimestamp="20170209171100"/>
<Platform Number="3" Changed="No" Parent="3"/>
<SecondaryServiceStatus/>
<ServiceStatus Status="On time"/>
<ExpectedDepartTime time="On time"/>
<ExpectedArriveTime time="On time"/>
<Delay Minutes="0"/>
<ExpectedDepartStatus time="On time"/>
<ExpectedArriveStatus time="On time"/>
<DelayCause/>
<LastReport tiploc="" time="1540" type="A" station1="Bristol Temple Meads" station2=""/>
<CommentLine>Formed of 8 coaches</CommentLine>
<CommentLine2>First Class at the FRONT</CommentLine2>
<ArrivalComment1/>
<ArrivalComment2/>
<PlatformComment1>Formed of 8 coaches</PlatformComment1>
<PlatformComment2>First Class at the FRONT</PlatformComment2>
<DepartureComment1/>
<DepartureComment2>Calls at Didcot Parkway</DepartureComment2>
<AssociatedPageNotices/>
<ChangeAt/>
<Operator name="GWR service" code="GW" brand=""/>
<Origin1 name="Bristol Temple Meads" tiploc="BRSTLTM" crs="BRI"/>
<Destination1 name="London Paddington" tiploc="PADTON" crs="PAD" ttarr="1814" etarr="1814"/>
<Via/>
<Coaches1/>
<Incident/>
<Dest1CallingPoints NumCallingPoints="2">
<CallingPoint Name="Didcot Parkway" tiploc="DIDCOTP" crs="DID" ttarr="1728" ttdep="1729" etarr="1728" etdep="1729" type="T"/>
<CallingPoint Name="Reading" tiploc="RDNGSTN" crs="RDG" ttarr="1743" ttdep="1747" etarr="1743" etdep="1747" type="T"/>
</Dest1CallingPoints>
</Service>
<Service Headcode="1B55" Uid="C20064" RetailID="" TigerID="11">
<ServiceType Type="Through"/>
<ArriveTime time="1712" Arrived="No" timestamp="20170209171200"/>
<DepartTime time="1715" timestamp="20170209171500" sorttimestamp="20170209171500"/>
<Platform Number="4" Changed="No" Parent="4"/>
<SecondaryServiceStatus/>
<ServiceStatus Status="On time"/>
<ExpectedDepartTime time="On time"/>
<ExpectedArriveTime time="On time"/>
<Delay Minutes="0"/>
<ExpectedDepartStatus time="On time"/>
<ExpectedArriveStatus time="On time"/>
<DelayCause/>
<LastReport tiploc="" time="" type="" station1="" station2=""/>
<CommentLine/>
<CommentLine2>First Class at the REAR</CommentLine2>
<ArrivalComment1/>
<ArrivalComment2/>
<PlatformComment1/>
<PlatformComment2>First Class at the REAR</PlatformComment2>
<DepartureComment1/>
<DepartureComment2/>
<AssociatedPageNotices/>
<ChangeAt/>
<Operator name="GWR service" code="GW" brand=""/>
<Origin1 name="London Paddington" tiploc="PADTON" crs="PAD"/>
<Destination1 name="Swansea" tiploc="SWANSEA" crs="SWA" ttarr="1923" etarr="1923"/>
<Via/>
<Coaches1/>
<Incident/>
<Dest1CallingPoints NumCallingPoints="6">
<CallingPoint Name="Bristol Parkway" tiploc="BRSTPWY" crs="BPW" ttarr="1739" ttdep="1741" etarr="1739" etdep="1741" type="T"/>
<CallingPoint Name="Newport (South Wales)" tiploc="NWPTRTG" crs="NWP" ttarr="1803" ttdep="1805" etarr="1803" etdep="1805" type="T"/>
<CallingPoint Name="Cardiff Central" tiploc="CRDFCEN" crs="CDF" ttarr="1821" ttdep="1825" etarr="1821" etdep="1825" type="T"/>
<CallingPoint Name="Bridgend" tiploc="BRGEND" crs="BGN" ttarr="1849" ttdep="1850" etarr="1849" etdep="1850" type="T"/>
<CallingPoint Name="Port Talbot Parkway" tiploc="PTALBOT" crs="PTA" ttarr="1902" ttdep="1903" etarr="1902" etdep="1903" type="T"/>
<CallingPoint Name="Neath" tiploc="NEATH" crs="NTH" ttarr="1910" ttdep="1911" etarr="1910" etdep="1911" type="T"/>
</Dest1CallingPoints>
</Service>
<Service Headcode="1L76" Uid="C20205" RetailID="" TigerID="12">
<ServiceType Type="Through"/>
<ArriveTime time="1728" Arrived="No" timestamp="20170209172800"/>
<DepartTime time="1730" timestamp="20170209173000" sorttimestamp="20170209173000"/>
<Platform Number="3" Changed="No" Parent="3"/>
<SecondaryServiceStatus/>
<ServiceStatus Status="On time"/>
<ExpectedDepartTime time="On time"/>
<ExpectedArriveTime time="On time"/>
<Delay Minutes="0"/>
<ExpectedDepartStatus time="On time"/>
<ExpectedArriveStatus time="On time"/>
<DelayCause/>
<LastReport tiploc="" time="1606" type="B" station1="Bridgend" station2="Cardiff Central"/>
<CommentLine/>
<CommentLine2>First Class at the FRONT</CommentLine2>
<ArrivalComment1/>
<ArrivalComment2/>
<PlatformComment1/>
<PlatformComment2>First Class at the FRONT</PlatformComment2>
<DepartureComment1/>
<DepartureComment2/>
<AssociatedPageNotices/>
<ChangeAt/>
<Operator name="GWR service" code="GW" brand=""/>
<Origin1 name="Swansea" tiploc="SWANSEA" crs="SWA"/>
<Destination1 name="London Paddington" tiploc="PADTON" crs="PAD" ttarr="1832" etarr="1832"/>
<Via/>
<Coaches1/>
<Incident/>
<Dest1CallingPoints NumCallingPoints="1">
<CallingPoint Name="Reading" tiploc="RDNGSTN" crs="RDG" ttarr="1759" ttdep="1803" etarr="1759" etdep="1803" type="T"/>
</Dest1CallingPoints>
</Service>
<Service Headcode="1C22" Uid="C20087" RetailID="" TigerID="13">
<ServiceType Type="Through"/>
<ArriveTime time="1730" Arrived="No" timestamp="20170209173000"/>
<DepartTime time="1730" timestamp="20170209173000" sorttimestamp="20170209173000"/>
<Platform Number="4" Changed="No" Parent="4"/>
<SecondaryServiceStatus/>
<ServiceStatus Status="On time"/>
<ExpectedDepartTime time="On time"/>
<ExpectedArriveTime time="On time"/>
<Delay Minutes="0"/>
<ExpectedDepartStatus time="On time"/>
<ExpectedArriveStatus time="On time"/>
<DelayCause/>
<LastReport tiploc="" time="" type="" station1="" station2=""/>
<CommentLine>Formed of 8 coaches</CommentLine>
<CommentLine2>First Class at the REAR</CommentLine2>
<ArrivalComment1/>
<ArrivalComment2/>
<PlatformComment1>Formed of 8 coaches</PlatformComment1>
<PlatformComment2>First Class at the REAR</PlatformComment2>
<DepartureComment1/>
<DepartureComment2/>
<AssociatedPageNotices/>
<ChangeAt/>
<Operator name="GWR service" code="GW" brand=""/>
<Origin1 name="London Paddington" tiploc="PADTON" crs="PAD"/>
<Destination1 name="Taunton" tiploc="TAUNTON" crs="TAU" ttarr="1929" etarr="1929"/>
<Via/>
<Coaches1/>
<Incident/>
<Dest1CallingPoints NumCallingPoints="9">
<CallingPoint Name="Chippenham" tiploc="CHIPNHM" crs="CPM" ttarr="1744" ttdep="1745" etarr="1744" etdep="1745" type="T"/>
<CallingPoint Name="Bath Spa" tiploc="BATHSPA" crs="BTH" ttarr="1759" ttdep="1800" etarr="1759" etdep="1800" type="T"/>
<CallingPoint Name="Bristol Temple Meads" tiploc="BRSTLTM" crs="BRI" ttarr="1814" ttdep="1820" etarr="1814" etdep="1820" type="T"/>
<CallingPoint Name="Nailsea & Backwell" tiploc="NAILSEA" crs="NLS" ttarr="1830" ttdep="1831" etarr="1830" etdep="1831" type="T"/>
<CallingPoint Name="Yatton" tiploc="YATTON" crs="YAT" ttarr="1837" ttdep="1838" etarr="1837" etdep="1838" type="T"/>
<CallingPoint Name="Worle" tiploc="WORLE" crs="WOR" ttarr="1844" ttdep="1845" etarr="1844" etdep="1845" type="T"/>
<CallingPoint Name="Weston-super-Mare" tiploc="WSMARE" crs="WSM" ttarr="1851" ttdep="1855" etarr="1851" etdep="1855" type="T"/>
<CallingPoint Name="Highbridge & Burnham" tiploc="HGHBRDG" crs="HIG" ttarr="1908" ttdep="1910" etarr="1908" etdep="1910" type="T"/>
<CallingPoint Name="Bridgwater" tiploc="BRDGWTR" crs="BWT" ttarr="1916" ttdep="1918" etarr="1916" etdep="1918" type="T"/>
</Dest1CallingPoints>
</Service>
<Service Headcode="1L80" Uid="C20206" RetailID="" TigerID="14">
<ServiceType Type="Through"/>
<ArriveTime time="1733" Arrived="No" timestamp="20170209173300"/>
<DepartTime time="1735" timestamp="20170209173500" sorttimestamp="20170209173500"/>
<Platform Number="1" Changed="No" Parent="1"/>
<SecondaryServiceStatus/>
<ServiceStatus Status="On time"/>
<ExpectedDepartTime time="On time"/>
<ExpectedArriveTime time="On time"/>
<Delay Minutes="0"/>
<ExpectedDepartStatus time="On time"/>
<ExpectedArriveStatus time="On time"/>
<DelayCause/>
<LastReport tiploc="" time="" type="" station1="" station2=""/>
<CommentLine/>
<CommentLine2>First Class at the FRONT</CommentLine2>
<ArrivalComment1/>
<ArrivalComment2/>
<PlatformComment1/>
<PlatformComment2>First Class at the FRONT</PlatformComment2>
<DepartureComment1/>
<DepartureComment2/>
<AssociatedPageNotices/>
<ChangeAt/>
<Operator name="GWR service" code="GW" brand=""/>
<Origin1 name="Cheltenham Spa" tiploc="CHLTNHM" crs="CNM"/>
<Destination1 name="London Paddington" tiploc="PADTON" crs="PAD" ttarr="1839" etarr="1839"/>
<Via/>
<Coaches1/>
<Incident/>
<Dest1CallingPoints NumCallingPoints="1">
<CallingPoint Name="Reading" tiploc="RDNGSTN" crs="RDG" ttarr="1803" ttdep="1808" etarr="1803" etdep="1808" type="T"/>
</Dest1CallingPoints>
</Service>
<Service Headcode="2M15" Uid="C20878" RetailID="" TigerID="15">
<ServiceType Type="Originating"/>
<ArriveTime time="" Arrived="No" timestamp=""/>
<DepartTime time="1736" timestamp="20170209173600" sorttimestamp="20170209173600"/>
<Platform Number="2" Changed="No" Parent="2"/>
<SecondaryServiceStatus/>
<ServiceStatus Status="On time"/>
<ExpectedDepartTime time="On time"/>
<ExpectedArriveTime time=""/>
<Delay Minutes="0"/>
<ExpectedDepartStatus time="On time"/>
<ExpectedArriveStatus time=""/>
<DelayCause/>
<LastReport tiploc="" time="" type="" station1="" station2=""/>
<CommentLine>Formed of 1 coach</CommentLine>
<CommentLine2/>
<ArrivalComment1/>
<ArrivalComment2/>
<PlatformComment1>Formed of 1 coach</PlatformComment1>
<PlatformComment2/>
<DepartureComment1/>
<DepartureComment2/>
<AssociatedPageNotices/>
<ChangeAt/>
<Operator name="GWR service" code="GW" brand=""/>
<Origin1 name="Swindon" tiploc="SDON" crs="SWI"/>
<Destination1 name="Westbury" tiploc="WSTBRYW" crs="WSB" ttarr="1821" etarr="1821"/>
<Via>Melksham</Via>
<Coaches1/>
<Incident/>
<Dest1CallingPoints NumCallingPoints="3">
<CallingPoint Name="Chippenham" tiploc="CHIPNHM" crs="CPM" ttarr="1752" ttdep="1753" etarr="1752" etdep="1753" type="T"/>
<CallingPoint Name="Melksham" tiploc="MELKSHM" crs="MKM" ttarr="1803" ttdep="1803" etarr="1803" etdep="1803" type="T"/>
<CallingPoint Name="Trowbridge" tiploc="TRWBRDG" crs="TRO" ttarr="1812" ttdep="1813" etarr="1812" etdep="1813" type="T"/>
</Dest1CallingPoints>
</Service>
<Service Headcode="1B59" Uid="C20065" RetailID="" TigerID="16">
<ServiceType Type="Through"/>
<ArriveTime time="1737" Arrived="No" timestamp="20170209173700"/>
<DepartTime time="1739" timestamp="20170209173900" sorttimestamp="20170209173900"/>
<Platform Number="4" Changed="No" Parent="4"/>
<SecondaryServiceStatus/>
<ServiceStatus Status="On time"/>
<ExpectedDepartTime time="On time"/>
<ExpectedArriveTime time="On time"/>
<Delay Minutes="0"/>
<ExpectedDepartStatus time="On time"/>
<ExpectedArriveStatus time="On time"/>
<DelayCause/>
<LastReport tiploc="" time="" type="" station1="" station2=""/>
<CommentLine>Formed of 8 coaches</CommentLine>
<CommentLine2>First Class at the REAR</CommentLine2>
<ArrivalComment1/>
<ArrivalComment2/>
<PlatformComment1>Formed of 8 coaches</PlatformComment1>
<PlatformComment2>First Class at the REAR</PlatformComment2>
<DepartureComment1/>
<DepartureComment2/>
<AssociatedPageNotices>The Capitals United</AssociatedPageNotices>
<ChangeAt/>
<Operator name="GWR service" code="GW" brand=""/>
<Origin1 name="London Paddington" tiploc="PADTON" crs="PAD"/>
<Destination1 name="Swansea" tiploc="SWANSEA" crs="SWA" ttarr="1945" etarr="1945"/>
<Via/>
<Coaches1/>
<Incident/>
<Dest1CallingPoints NumCallingPoints="6">
<CallingPoint Name="Bristol Parkway" tiploc="BRSTPWY" crs="BPW" ttarr="1804" ttdep="1808" etarr="1804" etdep="1808" type="T"/>
<CallingPoint Name="Newport (South Wales)" tiploc="NWPTRTG" crs="NWP" ttarr="1830" ttdep="1831" etarr="1830" etdep="1831" type="T"/>
<CallingPoint Name="Cardiff Central" tiploc="CRDFCEN" crs="CDF" ttarr="1848" ttdep="1850" etarr="1848" etdep="1850" type="T"/>
<CallingPoint Name="Bridgend" tiploc="BRGEND" crs="BGN" ttarr="1909" ttdep="1909" etarr="1909" etdep="1909" type="T"/>
<CallingPoint Name="Port Talbot Parkway" tiploc="PTALBOT" crs="PTA" ttarr="1922" ttdep="1922" etarr="1922" etdep="1922" type="T"/>
<CallingPoint Name="Neath" tiploc="NEATH" crs="NTH" ttarr="1930" ttdep="1930" etarr="1930" etdep="1930" type="T"/>
</Dest1CallingPoints>
</Service>
<Service Headcode="1A26" Uid="C20025" RetailID="" TigerID="17">
<ServiceType Type="Through"/>
<ArriveTime time="1740" Arrived="No" timestamp="20170209174000"/>
<DepartTime time="1741" timestamp="20170209174100" sorttimestamp="20170209174100"/>
<Platform Number="3" Changed="No" Parent="3"/>
<SecondaryServiceStatus/>
<ServiceStatus Status="On time"/>
<ExpectedDepartTime time="On time"/>
<ExpectedArriveTime time="On time"/>
<Delay Minutes="0"/>
<ExpectedDepartStatus time="On time"/>
<ExpectedArriveStatus time="On time"/>
<DelayCause/>
<LastReport tiploc="" time="" type="" station1="" station2=""/>
<CommentLine>Formed of 7 coaches</CommentLine>
<CommentLine2>First Class at the FRONT</CommentLine2>
<ArrivalComment1/>
<ArrivalComment2/>
<PlatformComment1>Formed of 7 coaches</PlatformComment1>
<PlatformComment2>First Class at the FRONT</PlatformComment2>
<DepartureComment1/>
<DepartureComment2>Calls at Didcot Parkway</DepartureComment2>
<AssociatedPageNotices/>
<ChangeAt/>
<Operator name="GWR service" code="GW" brand=""/>
<Origin1 name="Bristol Temple Meads" tiploc="BRSTLTM" crs="BRI"/>
<Destination1 name="London Paddington" tiploc="PADTON" crs="PAD" ttarr="1844" etarr="1844"/>
<Via/>
<Coaches1/>
<Incident/>
<Dest1CallingPoints NumCallingPoints="2">
<CallingPoint Name="Didcot Parkway" tiploc="DIDCOTP" crs="DID" ttarr="1759" ttdep="1800" etarr="1759" etdep="1800" type="T"/>
<CallingPoint Name="Reading" tiploc="RDNGSTN" crs="RDG" ttarr="1813" ttdep="1815" etarr="1813" etdep="1815" type="T"/>
</Dest1CallingPoints>
</Service>
<Incident Summary=" "/>
</StationBoard>

XML is hierarchical. Like a folder structure.

The root element is StationBoard. Then you have a Service element. So the XPath would be //StationBoard/Service[@Headcode="1B22"]/ServiceStatus/@Status

Which means in English "select the Status of the ServiceStatus element that is under the Service element with the attribute Headcode that equals ‘1B22’ that is under the StationBoard element.

Not getting an error now, but getting this at the end of the xml output; ’ could be found.

After a little search found an example and made this change;

val status = transform(xml, "xpath", "//StationBoard/Service[@Headcode='1B22']/ServiceStatus@Status")

So it seems we’re getting closer to the result I’m looking for.

All I can recommend at this point is experiment. There are Xpath testers online I’m sure that let you paste in your XML and test out the xpath string.

Just checked and I get this result;

Status=On time

Is this what you expected to be in the val status?

Maybe. I’ve not a lot of experience with grabbintg attributes out of XML with xpath. But if that is what your Item gets set to we can deal with it. However, I would have expected it to just be “On time”

I have been playing around with using Perl and have managed to get the data extracted but now find myself stuck at getting the data into OpenHAB using the Rest-API. Here’s my script, where have I gone wrong with updating my TrainStatus item?

#!/usr/bin/perl

use 5.010;
use strict;
use warnings;

use XML::LibXML;
use LWP::UserAgent;
use HTTP::Request::Common;

my $dom = XML::LibXML->load_xml(location => 'http://iris2.rail.co.uk/tiger/30353A.xml');

foreach my $service ($dom->findnodes('//StationBoard/Service')) {
        my $headcode = $service->findvalue('./@Headcode');
        if ($headcode eq "1G72") {
                my $status = $service->findvalue('./ServiceStatus/@Status');
                my $expected = $service->findvalue('./ExpectedArriveTime/@time');
                print "The $headcode is currently; $status\n";
                my $req = POST('http://oh01/rest/items/TrainStatus/state', Content => [param => $status]);
                $req->method('PUT');
                say($req->as_string);
                }
}

I haven’t looked at Perl in over a decade. I likely won’t be much help. The only think I can recommend is get the put working with curl or the REST API UI and use a capturing proxy or IPCAP to see what the request is suppost to look like and what this Perl method is doing and see what is different.

Just for completeness, I finally worked out the code. It may be of use to others so happy to share.

#!/usr/bin/perl

use 5.010;
use strict;
use warnings;

use REST::Client;
use XML::LibXML;
use LWP::UserAgent;
use HTTP::Request::Common;

my $dom = XML::LibXML->load_xml(location => 'http://iris2.rail.co.uk/tiger/30353A.xml');

foreach my $service ($dom->findnodes('//StationBoard/Service')) {
        my $headcode = $service->findvalue('./@Headcode');
        my $status = "Not In Service";
        if ($headcode eq "1A03") {
                $status = $service->findvalue('./ServiceStatus/@Status');
                my $expected = $service->findvalue('./ExpectedArriveTime/@time');
        }
        print "The $headcode is currently; $status\n";
        my $host = 'http://oh01';
        my $client = REST::Client->new(host => $host);
        $client->PUT("/rest/items/TrainStatus/state", $status);
}