Answer the question
In order to leave comments, you need to log in
How to properly parse XML through Xpath?
Good afternoon! There is an xml like this:
<street street="улица1">
<house house="1">
<appartment appartment="1"/>
<appartment appartment="2"/>
<appartment appartment="4"/>
</house>
<house house="8">
<appartment appartment="1"/>
<appartment appartment="2"/>
</house>
</street >
<street street="улица2">
<house house="1">
<appartment appartment="1"/>
<appartment appartment="2"/>
<appartment appartment="4"/>
</house>
<house house="8">
<appartment appartment="1"/>
<appartment appartment="2"/>
</house>
</street >
$xml = simplexml_load_file('file.xml');
$names = $xml->xpath("//street[@street='".$street."']");
foreach($names as $street) {
foreach($street->house as $house) {
$house_num = $house[house];
echo '<option value="' . $house_num . '">' . $house_num . '</option>';
}
$xml->xpath("//house[@house='".$house."' AND ../street[@street='".$street."']]");
Answer the question
In order to leave comments, you need to log in
It's not quite clear what exactly you want to get (at least on the example of your test XML).
Expression of the form
//street[@street="baker street"]/house[@house="221B"]/appartment
should select all apartments of the specified building on the specified street. If you need rooms, add /@appartment to the end. Is it possible to select a house through x-path, where attribute = something, which is in the street tag, where attribute = something....Using the axis ancestor or, if the relationship is direct, parent.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question