Xpath strange behavior - not matching text nodes
<p>While doing such things</p>

<pre><code>foreach ($xpath-&gt;query('.//tpl-static', $domTemplateContainer) as $domStatic) {
    /* ... */
    $domStatic-&gt;parentNode-&gt;removeChild($domStatic);
}
</code></pre>

<p>All seems to work fine.</p>

<p>But when dealing with xml-comments and more importantly - text nodes it fails to work as intended:</p>

<pre><code>foreach ($xpath-&gt;query('.//text()[normalize-space() = ""]', $domDocumentFragment) as $domNode) {
    $domNode-&gt;parentNode-&gt;removeChild($domNode);
}
</code></pre>

<p>some text nodes are just not selected but some are. I could not find logic behind this. Predicate does not matter. 
But I also found following query works fine:<code>./descendant-or-self::text()[normalize-space() = ""]</code></p>

<p>Why does <code>.//</code> only work for element nodes but not for text nodes? Is it libxml/php bug or something to be reported or I've missed something?</p>

<p><strong>ADDITION:</strong></p>

<p>Complete example (adapted from complex project):</p>

<p>

<pre><code>$xml = '
&lt;tpl-static&gt;
    &lt;link rel="shortcut icon" type="image/x-icon" href="/static/images/icon.ico" /&gt;
    &lt;link rel="stylesheet" type="text/css" href="/static/css/html5reset-1.6.1.css" /&gt;
    &lt;link rel="stylesheet" type="text/css" href="/static/css/style.css" /&gt;
    &lt;script src="/static/js/underscore.js"&gt;&lt;/script&gt;
    &lt;!-- &lt;script src="/static/js/jquery.adaptive-backgrounds.js"&gt;&lt;/script&gt; --&gt;
    &lt;script src="/static/js/jquery.maskedinput.min.js"&gt;&lt;/script&gt;
    &lt;link href="/static/js/jquery-ui-1.11.2.custom/jquery-ui.css" rel="stylesheet"/&gt;
    &lt;script src="/static/js/jquery-ui-1.11.2.custom/jquery-ui.min.js"&gt;&lt;/script&gt;
    &lt;link rel="stylesheet" href="/static/js/jquery.magnific-popup/magnific-popup.css" /&gt;
    &lt;script src="/static/js/jquery.magnific-popup/jquery.magnific-popup.js"&gt;&lt;/script&gt;
    &lt;script src="/static/templates/dealers-page-includes/page-includes.js"&gt;&lt;/script&gt;
&lt;/tpl-static&gt;
&lt;br/&gt;

';

$domDocument = new \DOMDocument('1.0', 'utf-8');
$xpath = new \DOMXPath($domDocument);
$domDocumentFragment = $domDocument-&gt;createDocumentFragment();
$domDocumentFragment-&gt;appendXml($xml);

$templateName = 'test';
//$it = $this;
$adoptTemplate = function($domTemplateContainer) use (&amp;$adoptTemplate, /*$it,*/ $domDocument, $xpath, $templateName) {

    foreach ($xpath-&gt;query('.//comment()', $domTemplateContainer) as $domComment) {
        $domComment-&gt;parentNode-&gt;removeChild($domComment);
    }

    foreach ($xpath-&gt;query('.//tpl-static', $domTemplateContainer) as $domStatic) {
        foreach ($domStatic-&gt;childNodes as $curChildNode) {
            //$it-&gt;_domDocumentHead-&gt;appendChild($curChildNode-&gt;cloneNode(true));
        }
        $domStatic-&gt;parentNode-&gt;removeChild($domStatic);
    }
};

$adoptTemplate($domDocumentFragment);

// FAIL!
/*foreach ($xpath-&gt;query('.//text()[normalize-space() = ""]', $domDocumentFragment) as $domNode) {
    $domNode-&gt;parentNode-&gt;removeChild($domNode);
}*/
// HERE IS 
// workaround...
foreach ($xpath-&gt;query('./descendant-or-self::text()[normalize-space() = ""]', $domDocumentFragment) as $domNode) {
    $domNode-&gt;parentNode-&gt;removeChild($domNode);
}

if ($domDocumentFragment-&gt;childNodes-&gt;length &gt; 1) {
    throw new \Exception('Single node expected in template "' . $templateName . '", ' . $domDocumentFragment-&gt;childNodes-&gt;length . ' given.');
}
</code></pre>