For this XML:
<row>
<entry XY="1" al="23" se="5" ag="fgh"> </entry>
<entry XY="1" al="23" se="5" ag="fgh">Content1</entry>
<entry XY="1" al="23" se="5" ag="fgh">Content2</entry>
<entry XY="1" al="23" se="5" ag="fgh"> </entry>
<entry XY="1" al="23" se="5" ag="fgh">Content3</entry>
</row>
I have the following loop:
foreach ($toc_element->entry as $toc_element_line) {
if ($toc_element_line->xpath('/*[text()]')) {
array_push($arr, $toc_element_line);
}
}
I am trying to test if the current element is empty. The Object itself is never empty, because the potentially empty element, always has attributes (that I don't care about).
Is the XPath approach (which I am noticeably new to) correct? If so, how would I write it?
Thanks a lot in advance. (I know that there are some questions of that kind here on stackoverflow, but I couldn't quite figure it out)
<entry> </entry>
is NOT empty, it contains ' '
in contrast to <entry></entry>
or <entry />
which is empty.
So, taken your XML example, use xpath
to create an array of nodes NOT empty and NOT ' '
:
$xml = simplexml_load_string($x); // assume XML in $x
$nodes = $xml->xpath("//entry[text() and text()!=' ']");
foreach ($nodes as $node) echo "$node<br />"; // output
See it working: http://3v4l.org/8Y7uN