Your question is a bit under-specified, so there are several ways to interpret it. If you want all direct child elements of the current element (with all of their sub-elements), then use
*/*
For your example, this gives you
<span>
<cite>
</cite>
</span>
and
<span>
<cite>
</cite>
</span>
If you want all child nodes, then use node()
instead of *
:
*/node()
For your example, this gives you both sub-elements as above, alongside with newline/indentation text()
nodes.
If, however, you want to have only the child nodes and not their children as well (i.e. only the span
elements, but without their child elements), you must use two expressions:
- select the direct child elements via
*/*
- process the those child elements and select only the text nodes and not the grandchildren elements via
text()
My PHP is a bit rusty, but it should work a bit like this:
$doc = new DOMDocument;
// set up $doc
$xpath = new DOMXPath($doc);
// perform step #1
$childElements = $xpath->query('*/*');
$directChildren = array();
foreach ($childElements as $child) {
// perform step #2
$textChildren = $xpath->query('text()', $child);
foreach ($textChildren as $text) {
$directChildren[] = $text;
}
}
// now, $directChildren contains all text nodes