With a XML-structure like this:
<w:document>
<w:body>
<w:p>{textcontent}</w:p>
<w:p>
<w:drawing>
</w:drawing>
</w:p>
<w:p>{no textcontent}</w:p>
<w:p>
<w:drawing>
</w:drawing>
</w:p>
<w:p>{no textcontent}</w:p>
<w:p>text..</w:p>
<w:tbl>three</w:tbl>
<w:p>four</w:p>
</w:body>
</w:document>
I want to check occurrence of and count nr of drawings
I've tried this:
$document = new DOMDocument();
$document->loadXML($xml_str);
$xpath = new DOMXpath($document);
$xpath->registerNamespace(
'wspace', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'
);
$pindex = 0;
foreach ($xpath->evaluate('//wspace:p') as $index => $p_node)
{
if (strlen($p_node->textContent)>0) {
//I just want to know if drawing exists in this $p_node..
//if the the node contains any content
$child_nodes = $xpath->evaluate('//wspace:drawing', $p_node);
var_dump($child_nodes[0]);
$pindex++;
//But I get an object for each pnode. I just want an object when there
//is an actualy <w:drawing> inside of <p> (item with index 1,3 in above example)
}
}
How do I check nr of occurences for the <w:p><w:drawing>
?
I could do like this to count the occurences:
foreach ($xpath->evaluate('//wspace:p//wspace:drawing') as $index => $node)
{
var_dump($node);
}
but I need the index of the p ($pindex
) as well.
I've also been trying with evaluate('/child')
, evaluate('node()/child')
without success.
What is going wrong with my expression?