I'm using xpath to grab information from a document, the only issue is I havn't been able to combine them into 1 for loop so the information displays correctly on the page. My code is:
<?php
$doc = new DOMDocument;
$doc->preserveWhiteSpace = FALSE;
$doc->load('http://mdoerrdev.com/xml/updates-mits.xml');
$xpath = new DOMXPath($doc);
$xpath->registerNamespace('MITS', "http://www.mitsproject.org/namespace");
$unitName = $xpath->evaluate("//ILS_Unit[@FloorplanID='550584']/Unit/MITS:MarketingName");
$unitPrice = $xpath->evaluate("//ILS_Unit[@FloorplanID='550584']/Unit/MITS:Information/MITS:MarketRent");
?>
<div class="unit-name">
<?php
foreach ($unitName as $un) {
echo $un->nodeValue . "\n";
}
?>
</div>
<div class="floor-plan-box fpb-one-bedroom cleafix"> <?php
foreach ($unitPrice as $up) {
echo $up->nodeValue . "\n";
?><img src='<?php bloginfo('stylesheet_directory'); ?>/img/floorplans/<?php echo "550584" ?>.jpg' /> <?php
};
?>
</div>
(from: http://pastie.org/8360106)
I need to combine the MarketRent and MarketingName information together as opposed to having them display separately as they do in the current code.
This depends a bit on what you're trying to achieve. You can combine two xpath expressions by using the Union operator (pipe
|
):Which then would return all evaluated nodes in document order:
Which is probably not what you're looking for. Instead you want to either evaluate over both results the same time, which can be achieved with a
MultipleIterator
:Output:
This is probably more what you're looking for. Also for what you do, SimpleXMLElement might be more easy to use because it already allows to output nodes in string context.
Additionally there is another concept that directly maps shallow objects to an underlying XML document and it has been outlined here:
It is quite an interesting concept. IIRC I wrote something similar like applying one xpath query onto all nodes of another one, but I don't find it right now.
Use the context node argument of
$xpath->evaluate()
...Your code with these adjustments:
Yields this output with MarketingName and MarketRent displayed together rather than separately per your request:
I couldn't resist to give this another try to apply a model on some XML source. I opted for
SimpleXMLElement
for this answer. It is merely transparent and has mostly only implications on the xpath expressions. It should be possible to translate the same interface toDOMDocument
.The source of the used types like
XPrototype
andXList
is on Github:XObjects.php
.