Answer the question
In order to leave comments, you need to log in
How to get parent tag attribute in xml documents using php?
There is an xml file with the following structure:
<catalog>
-<category id="1">
--<category id="2">
---<product id=1 name="Товар 1" />
---<product id=2 name="Товар 2" />
--</category>
-</category>
-<category id="3">
--<product id=3 name="Товар 3" />
-</category>
-</catalog>
$products= $catalog->getElementsByTagName("product");
echo "<table>";
echo "<tr><th>Id</th><th>Наименование</th></tr>";
foreach ($products as $product) {
echo '<tr><td>'.$product->getAttribute('id') . '</td><td>'.$product->getAttribute('name') . '</td></tr>';
}
echo "</table>";
}
Answer the question
In order to leave comments, you need to log in
Probably: $product->parentNode->getAttribute('id') will return you the ID of the category taken from the tag above
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question