BlueCloudNL
BlueCloudNL

Reputation: 45

Fetching node name using XPath

I have this XML and want the get the name of 2nd element in the node "ObjectDetails". The name is "Koop"

$xml = '
    <ObjectenLijst TimeStamp="17-07-2012 09:14:46" Versie="12">
        <Object>
            <ObjectDetails>
                <Adres>Niebergweg 1</Adres>
                <Koop>
                    <Prijsvoorvoegsel>vraagprijs</Prijsvoorvoegsel>
                    <Koopprijs>31000</Koopprijs>
                    <KoopConditie>kosten koper</KoopConditie>
                </Koop>
            </ObjectDetails>
        </Object>
    <ObjectenLijst>' ;

When I use this XPath, an empty array is returned:

$xml = simplexml_load_string($xml);
$result =  $xml->xpath('name(//Object/ObjectDetails/*[2])');

Upvotes: 2

Views: 445

Answers (1)

user142162
user142162

Reputation:

As far as I know, SimpleXMLElement::xpath can only return an array of SimpleXMLElement nodes, so what you're trying to do would be invalid. You can, however, fetch the name of node this way:

$result =  $xml->xpath('/ObjectenLijst/Object/ObjectDetails/*[2]');
$name = $result[0]->getName(); // Koop

Upvotes: 1

Related Questions