2

I know that this is a duplicate, but I couldn't apply the answer on this one: LINK I can parse the XML without the xmlns, but the feed I'm parsing from is using it so...

The WORKING XML:

<DPS>
<Buses>
<DpsBus>
<SiteId>9520</SiteId>
<StopAreaNumber>75821</StopAreaNumber>
<TransportMode>BUS</TransportMode>
<StopAreaName>Södertälje centrum</StopAreaName>
<LineNumber>754</LineNumber>
<Destination>Geneta (Scaniarinken)</Destination>
<TimeTabledDateTime>2013-10-31T16:08:00</TimeTabledDateTime>
<ExpectedDateTime>2013-10-31T16:08:00</ExpectedDateTime>
<DisplayTime>0 min</DisplayTime>
</DpsBus>
</Buses>
</DPS>

The NON-WORKING XML

<DPS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www1.sl.se/realtidws/">
<Buses>
<DpsBus>
<SiteId>9520</SiteId>
<StopAreaNumber>75821</StopAreaNumber>
<TransportMode>BUS</TransportMode>
<StopAreaName>Södertälje centrum</StopAreaName>
<LineNumber>754</LineNumber>
<Destination>Geneta (Scaniarinken)</Destination>
<TimeTabledDateTime>2013-10-31T16:08:00</TimeTabledDateTime>
<ExpectedDateTime>2013-10-31T16:08:00</ExpectedDateTime>
<DisplayTime>0 min</DisplayTime>
</DpsBus>
</Buses>
</DPS>

PHP

$xmldata = '<DPS xmlns:xsi="..."';
$xml = simplexml_load_string($xmlData);
$arrLines = $xml->xpath('/DPS/Buses/DpsBus[TransportMode="BUS"]');
foreach($arrLines as $line) {
    echo $line->LineNumber.'<br>';
}

Any ideas here? Thanks!

1
  • Please define working. Those two files have different XML tags so they should not work the same. The behavior you describe looks actually technically correct. Commented Nov 2, 2013 at 14:27

1 Answer 1

1

Register the namespace via registerXPathNamespace() and update the XPath: /r:DPS/r:Buses/r:DpsBus[r:TransportMode="BUS"]

Then your updated PHP code:

<?php
$xmlData = <<<XML
<?xml version="1.0"?>
<DPS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www1.sl.se/realtidws/">
  <Buses>
    <DpsBus>
      <SiteId>9520</SiteId>
      <StopAreaNumber>75821</StopAreaNumber>
      <TransportMode>BUS</TransportMode>
      <StopAreaName>Södertälje centrum</StopAreaName>
      <LineNumber>754</LineNumber>
      <Destination>Geneta (Scaniarinken)</Destination>
      <TimeTabledDateTime>2013-10-31T16:08:00</TimeTabledDateTime>
      <ExpectedDateTime>2013-10-31T16:08:00</ExpectedDateTime>
      <DisplayTime>0 min</DisplayTime>
    </DpsBus>
  </Buses>
</DPS>
XML;

$xml = simplexml_load_string($xmlData);
$xml->registerXPathNamespace('r', 'http://www1.sl.se/realtidws/');
$arrLines = $xml->xpath('/r:DPS/r:Buses/r:DpsBus[r:TransportMode="BUS"]');
foreach ($arrLines as $line) {
    echo $line->LineNumber, "<br>\n";
}

Yields your desired output:

754<br>
Sign up to request clarification or add additional context in comments.

2 Comments

Excellente!! :) I did not add the ISO-8859-1 encoding. It was the registerXPathNamespace that did the trick. Thanks!
There was no issue with ISO-8859-1 so I removed that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.