Skip to content
Advertisement

Finding the first entry of XML and displaying it

Im working on compiling a list using XML and PHP. I’m looking to find the first “destination tag” found in the RailVehicleStateClass for each train ID and echo it out. I tried doing a foreach loop to gather the destination tag but it just loops the same data for each train ID until the end of the xml file. Below is a snippet of the XML file, the full version has well over 700 entries and each train can have anywhere from 1 to 100+ railvehiclaes associated with it.

XML

JavaScript

PHP

JavaScript

Advertisement

Answer

Your xpath query to get the RailVehicleStateClass elements needs to be made relative to the current $train. You can do that using:

JavaScript

Note the use of . in front of // to make the path relative to the current $train element.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement