php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54905 inconsistant namespace behaviour with DocumentFragment
Submitted: 2011-05-23 10:05 UTC Modified: 2014-12-30 10:41 UTC
Votes:2
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: andrew at miniatureworldmaker dot com dot au Assigned:
Status: No Feedback Package: DOM XML related
PHP Version: 5.3.6 OS: Fedora / Debian
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: andrew at miniatureworldmaker dot com dot au
New email:
PHP Version: OS:

 

 [2011-05-23 10:05 UTC] andrew at miniatureworldmaker dot com dot au
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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-12-02 17:35 UTC] mike@php.net
-Status: Open +Status: Feedback
 [2013-12-02 17:35 UTC] mike@php.net
Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves. 

A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external 
resources such as databases, etc. If the script requires a 
database to demonstrate the issue, please make sure it creates 
all necessary tables, stored procedures etc.

Please avoid embedding huge scripts into the report.

This test case is quite convoluted, I think I get the opposite result, but I can't really tell.

Please re-open this bug with a proper reproduce script.
 [2014-12-30 10:41 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 18:01:29 2024 UTC