|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2020-06-09 15:25 UTC] and_spam+php_net at rump dot dk
Description:
------------
The test script perform the same operation twice but the result (the last two lines) are different depending on if the XML has been created dynamically or loaded (in this case by storing the dynamically created XML and (re)loading it).
You may reorder the createAttribute lines as you want but the last two lines will always be the same - the last line being correct.
Test script:
---------------
$xml = new DOMDocument();
$element = $xml->createElement('saml:Assertion');
$xml_saml_assertion = $xml->appendChild($element);
$attribute = $xml->createAttribute('id');
$attribute->value = 'IDCard';
$xml_saml_assertion->appendChild($attribute);
$attribute = $xml->createAttribute('xmlns:saml');
$attribute->value = 'urn:oasis:names:tc:SAML:2.0:assertion';
$xml_saml_assertion->appendChild($attribute);
$attribute = $xml->createAttribute('Version');
$attribute->value = '2.0';
$xml_saml_assertion->appendChild($attribute);
print("<pre>" . htmlentities($xml->saveXML()) . "</pre><br>\n");
print("<pre>" . htmlentities($xml->C14N(TRUE)) . "</pre><br>\n");
$xml->loadXML($xml->saveXML());
print("<pre>" . htmlentities($xml->C14N(TRUE)) . "</pre><br>\n");
Expected result:
----------------
<saml:Assertion xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" Version="2.0" id="IDCard"></saml:Assertion>
Actual result:
--------------
<saml:Assertion Version="2.0" id="IDCard" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"></saml:Assertion>
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 02 23:00:02 2025 UTC |
The DOM living standard states[1]: | Return a new attribute whose local name is localName […] and[2]: | Attributes have a […] namespace prefix (null or a non-empty | string), local name (a non-empty string) […] So $xml->createAttribute('xmlns:saml'); creates an attribute with *local name* 'xmlns:saml', which is not magically converted to a namespace declaration on the attribute's element. You can see that when you var_dump() the respective attribute[3]; it's just a regular attribute with the localName 'xmlns:saml', and the namespace URI as testContent. When you convert this element to its textual representation, it looks like its properly namespaced pendant, but it isn't quite the same (unless reparsed). [1] <https://dom.spec.whatwg.org/#dom-document-createattribute> [2] <https://dom.spec.whatwg.org/#concept-attribute-local-name> [3] <https://3v4l.org/NNUsV>