php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #34276 setAttributeNS doesn't work with non-prefixed attributes from default namespace
Submitted: 2005-08-27 02:26 UTC Modified: 2005-08-28 18:27 UTC
From: molily at gmx dot de Assigned:
Status: Closed Package: DOM XML related
PHP Version: 5CVS-2005-08-27 (snap) OS: Gentoo Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: molily at gmx dot de
New email:
PHP Version: OS:

 

 [2005-08-27 02:26 UTC] molily at gmx dot de
Description:
------------
It's not possible to use hasAttributeNS and getAttributeNS on attributes from the default namespace (i.e. without namespace prefix). They do not find the right attribute.

In addition to this, setAttributeNS cannot set an attribute in the default namespace. The new atttribute gets an incorrect namespace prefix.

I'm using the latest source snapshot php5-200508262230 (standard configuration but --without-pear) with libxml 2.6.20 (-r2 in Gentoo Portage).

Is this a libxml problem, like many DOM extension bugs before? Or do I misunderstand DOM 2 Core / XML Namespaces?

Reproduce code:
---------------
<?php
$dom = DOMDocument::loadXML('<a xmlns="http://namespaces.molily.de/x" xmlns:y="http://namespaces.molily.de/y" attra="attra" y:attrb="attrb" />');
$b = $dom->documentElement;
var_dump($b->hasAttributeNS('http://namespaces.molily.de/x', 'attra'));
var_dump($b->hasAttributeNS('http://namespaces.molily.de/y', 'attrb'));
var_dump($b->getAttributeNS('http://namespaces.molily.de/x', 'attra'));
var_dump($b->getAttributeNS('http://namespaces.molily.de/y', 'attrb'));
$b->setAttributeNS('http://namespaces.molily.de/x', 'attra', 'attra neu');
$b->setAttributeNS('http://namespaces.molily.de/y', 'attrb', 'attrb neu');
$b->setAttributeNS('http://namespaces.molily.de/x', 'attrc', 'attrc');
$b->setAttributeNS('http://namespaces.molily.de/y', 'attrd', 'attrd');
echo("\n");
var_dump($b->getAttributeNS('http://namespaces.molily.de/x', 'attra'));
var_dump($b->getAttributeNS('http://namespaces.molily.de/y', 'attrb'));
var_dump($b->getAttributeNS('http://namespaces.molily.de/x', 'attrc'));
var_dump($b->getAttributeNS('http://namespaces.molily.de/y', 'attrd'));
echo("\n");
var_dump($b->attributes->length);
echo($dom->saveXML());
?>

Expected result:
----------------
(As far as I know:)

bool(true)
bool(true)
string(5) "attra"
string(5) "attrb"

string(9) "attra neu"
string(9) "attrb neu"
string(5) "attrc"
string(5) "attrd"

int(4)
<?xml version="1.0"?>
<a xmlns="http://namespaces.molily.de/x" xmlns:y="http://namespaces.molily.de/y" attra="attra neu" y:attrb="attrb neu" attrc="attrc" y:attrd="attrd"/>

Actual result:
--------------
bool(false)
bool(true)
string(0) ""
string(5) "attrb"

string(0) ""
string(9) "attrb neu"
string(0) ""
string(5) "attrd"

int(5)
<?xml version="1.0"?>
<a xmlns="http://namespaces.molily.de/x" xmlns:y="http://namespaces.molily.de/y" attra="attra" y:attrb="attrb neu" y:attra="attra neu" y:attrc="attrc" y:attrd="attrd"/>

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-08-27 03:32 UTC] rrichards@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

you misunderstood DOM 2 Core / XML Namespaces. Attributes dont  inherit from default namespace.
 [2005-08-27 21:46 UTC] molily at gmx dot de
Thanks, this explains why the first getAttributeNS call in the example doesn't work as I expected. The workaround is getAttributeNS(null, "attra") as per DOM 3 Core. The same goes for setAttributeNS if you want to set an attribute without namespace (it's possible to handle these attributes as if they use the default namespace, but there is this silly rule in XML Namespaces).

But what's about the setAttributeNS problem?
$b->setAttributeNS('http://namespaces.molily.de/x', 'attrc', 'attrc');
Why does this add an attribute with the namespace prefix ?y?? (see actual result: y:attrc="attrc" and expected result: attrc="attrc") ?y? is the prefix for the other namespace used in the example document.

After running setAttributeNS, the attribute ?attrc? should be explicitly in the mentioned namespace ?http://namespaces.molily.de/x?. The rule ?default namespaces do not apply directly to attributes? doesn't apply.
Why isn't it possible to getAttributeNS('http://namespaces.molily.de/x', 'attrc') after that?
By the way, getAttributeNS('http://namespaces.molily.de/y', 'attrc') works, but why? I never told to use namespace ?y?, but it is used probably because it is the only namespace declared with prefix.
 [2005-08-28 18:27 UTC] rrichards@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

Yup, missed that part of the initial report
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 12:01:29 2024 UTC