From:
Operating system: Fedora / Debian
PHP version: 5.3.6
Package: DOM XML related
Bug Type: Bug
Bug description:inconsistant namespace behaviour with DocumentFragment
Description:
------------
In an XML document, when an xmlns attribute is present, all child elements
of that
element are also considered to be in that namespace.
When a DocumentFragment is created and the root element contains an xmlns
attribute this is the behaviour currently displayed by PHP.
However if a documentfragment is appended to an element already present in
the DOM
which contains an xmlns, the appended fragment appears to be assigned a
default
'null' namespace.
Test script:
---------------
Scenario 1: xmlns is present in the fragment.
<?php
const BLAH_NS = 'http://blah.com/blah';
$xml = '
<root>
<foo>
<bar />
</foo>
</root>
';
$fragXML = '<content
xmlns="'.BLAH_NS.'"><sneh>some</sneh><grah>here</grah><ul><li>one</li><li>two</li><li>three</li></ul></content>';
$doc = new DOMDocument('1.0','UTF-8');
$doc->formatOutput = true;
$doc->preserveWhiteSpace = false;
$doc->loadXML( $xml );
$xpath = new DOMXPath( $doc );
$barElement = $xpath->query( '//bar' )->item(0);
// create a new element with xmlns="http://blah.com/blah" . The
expectation is that
// every child of this node will be part of this namespace as per the
section on default
// name spaces http://www.w3schools.com/XML/xml_namespaces.asp
// create a new document Fragment
$DOMFragment = $doc->createDocumentFragment();
$DOMFragment->appendXML( $fragXML );
// Attach the fragment to the rest of the document
$barElement->appendChild( $DOMFragment );
// all good so far
echo $doc->saveXML();
// register the bar name space so we can perform a lookup
$xpath->registerNamespace('b', BLAH_NS);
echo "\n\n\n";
// this doesn't work
$grahElement = $xpath->query( '//b:grah' )->item(0);
echo "Find //b:grah " .$grahElement->nodeValue."\n";
// this works
$grahElement = $xpath->query( '//grah' )->item(0);
echo "Find //grah ".$grahElement->nodeValue."\n";
?>
Scenario 2: The xmlns attribute is present on the element the fragment is
attached to
<?php
const BLAH_NS = 'http://blah.com/blah';
$xml = '
<root>
<foo>
<bar />
</foo>
</root>
';
$fragXML =
'<sneh>some</sneh><grah>here</grah><ul><li>one</li><li>two</li><li>three</li></ul>';
$doc = new DOMDocument('1.0','UTF-8');
$doc->formatOutput = true;
$doc->preserveWhiteSpace = false;
$doc->loadXML( $xml );
$xpath = new DOMXPath( $doc );
$barElement = $xpath->query( '//bar' )->item(0);
// create a new element with xmlns="http://blah.com/blah" . The
expectation is that
// every child of this node will be part of this namespace as per the
section on default
// name spaces http://www.w3schools.com/XML/xml_namespaces.asp
$contentElement = $doc->createElementNS( BLAH_NS, 'content' );
$barElement->appendChild( $contentElement );
// create a new document Fragment
$DOMFragment = $doc->createDocumentFragment();
$DOMFragment->appendXML( $fragXML );
// Attach the fragment to the rest of the document
$contentElement->appendChild( $DOMFragment );
// all good so far
echo $doc->saveXML();
// register the bar name space so we can perform a lookup
$xpath->registerNamespace('b', BLAH_NS);
echo "\n\n\n";
// this doesn't work
$grahElement = $xpath->query( '//b:grah' )->item(0);
echo "Find //b:grah " .$grahElement->nodeValue."\n";
// this works
$grahElement = $xpath->query( '//grah' )->item(0);
echo "Find //grah ".$grahElement->nodeValue."\n";
?>
Expected result:
----------------
In both cases I would expect the output of:
Find //b:grah here
Find //grah
Actual result:
--------------
Find //b:grah
Find //grah grahtest
--
Edit bug report at http://bugs.php.net/bug.php?id=54905&edit=1
--
Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=54905&r=trysnapshot52
Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=54905&r=trysnapshot53
Try a snapshot (trunk): http://bugs.php.net/fix.php?id=54905&r=trysnapshottrunk
Fixed in SVN: http://bugs.php.net/fix.php?id=54905&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=54905&r=needdocs
Fixed in release: http://bugs.php.net/fix.php?id=54905&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=54905&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=54905&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=54905&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=54905&r=support
Expected behavior: http://bugs.php.net/fix.php?id=54905&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=54905&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=54905&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=54905&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=54905&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=54905&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=54905&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=54905&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=54905&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=54905&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=54905&r=mysqlcfg