Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : DOM XML Functions : DomNode->add_namespace

DomNode->add_namespace

Adds a namespace declaration to a node ()


Code Examples / Notes » domnode_add_namespace

tkearns

<?php
// EXAMPLE USING PHP 4.3.3
$objDoc = domxml_new_doc("1.0");
$ndRoot = $objDoc->create_element("root");
$ndRoot = $objDoc->append_child($ndRoot);
$ndRoot->set_namespace("foo/the/bar");
// using ramdom prefix, produces something like
// <a3767:root xmlns:a3767="foo/the/bar"/>
$ndRoot->set_namespace("yet/another/namespace","yan");
// modifies the existing prefix (again) produces something like
// <yan:root xmlns:a3767="foo/the/bar" xmlns:yan="yet/another/namespace"/>
$ndRoot->set_namespace("yet/another/namespace","foo");
// attempt to use the same ns id has again no effect. element is un-touched
// <yan:root xmlns:a3767="foo/the/bar" xmlns:yan="yet/another/namespace"/>
$ndRoot->set_namespace("new/ns/no/prefix");
// munges the existing element prefix again as well as adding declaration
// <a1757:root
//     xmlns:a3767="foo/the/bar"
//     xmlns:yan="yet/another/namespace"
//     xmlns:a1757="new/ns/no/prefix"
// />
/*
It seems that the only way to add a DEFAULT namespace to a node (without
affecting the current prefix), is to use set_attribute("xmlns",$ndId).
*/
$ndRoot->set_attribute("xmlns","default/ns/no/prefix");
// now we have
// <a1757:root
//     xmlns:a3767="foo/the/bar"
//     xmlns:yan="yet/another/namespace"
//     xmlns:a1757="new/ns/no/prefix"
//     xmlns="default/ns/no/prefix"
// />
/*
Note that namespace declarations inserted using set_namespace are not classed as
attributes, so they do not show up in an attribute list. Since we set our
default namespace using the set_attribute() method, it is the only one that will
show up.
*/
var_dump($ndRoot->attributes());
// see that only the defaul namespace is returned because it is of type
// "domattribute" because we set it with an attribute method.
$ndRoot->set_attribute("xmlns:pfx","non/default/with/prefix");
// now we have
// <a1757:root
//     xmlns:a3767="foo/the/bar"
//     xmlns:yan="yet/another/namespace"
//     xmlns:a1757="new/ns/no/prefix"
//     xmlns="default/ns/no/prefix"
//     xmlns:pfx="non/default/with/prefix"
// />
/*
Any XML processor still regards it as a namespace declaration the same way
they recognise the other namespaces we declared. Problem for us is that we
cannot ascertain the existing namespace declarations on any element if they are
not declared using set_attribute. Furthermore, external files imported into
DOMXML objects do NOT have their namespace declarations detected as attributes -
and rightly so. But the problem arises that now there is no way to detect these
declarations.
*/
var_dump($ndRoot->attributes());
// Our new "xmlns:pfx" *attribute* now shows up because we set it with an
// attribute method and it is of type "domattribute". So what of our namespace
// declarations using set_namespace() then? Do they have a type?
/*
Well there doesn't seem to be any other dom type where our declarations can
reside. This is problably the reason why we have no way of detecting them.
It's important to note that when parsing in external XML files, the prefixed
namespace declarations do NOT show up in the list of attribute nodes.
*/
/*
I would also argue that destroying the existing element prefix by forcing the
[re]population of it with a new one when using set_namespace() should be
considdered "unexpected behaviour". If the user doesn not supply a prefix, then
it should be added as the default namespace (with no prefix).
Using set_attribute() is a workaround to the "problem" and not a solution.
*/
/*
The behaviour of set_namespace() is not the biggest problem though.
Inability to detect existing namespace declarations is. How do I find out if
a namespace with id of "yet/another/namespace" has been declared in this
element? The answer is I can't. And if I could, I would also want to know what
prefix it is using.
*/
echo htmlentities($objDoc->dump_mem(true));
?>


Change Language


Follow Navioo On Twitter
DomAttribute->name
DomAttribute->set_value
DomAttribute->specified
DomAttribute->value
DomDocument->add_root
DomDocument->create_attribute
DomDocument->create_cdata_section
DomDocument->create_comment
DomDocument->create_element_ns
DomDocument->create_element
DomDocument->create_entity_reference
DomDocument->create_processing_instruction
DomDocument->create_text_node
DomDocument->doctype
DomDocument->document_element
DomDocument->dump_file
DomDocument->dump_mem
DomDocument->get_element_by_id
DomDocument->get_elements_by_tagname
DomDocument->html_dump_mem
DomDocument->xinclude
DomDocumentType->entities()
DomDocumentType->internal_subset()
DomDocumentType->name()
DomDocumentType->notations()
DomDocumentType->public_id()
DomDocumentType->system_id()
DomElement->get_attribute_node()
DomElement->get_attribute()
DomElement->get_elements_by_tagname()
DomElement->has_attribute()
DomElement->remove_attribute()
DomElement->set_attribute_node()
DomElement->set_attribute()
DomElement->tagname()
DomNode->add_namespace
DomNode->append_child
DomNode->append_sibling
DomNode->attributes
DomNode->child_nodes
DomNode->clone_node
DomNode->dump_node
DomNode->first_child
DomNode->get_content
DomNode->has_attributes
DomNode->has_child_nodes
DomNode->insert_before
DomNode->is_blank_node
DomNode->last_child
DomNode->next_sibling
DomNode->node_name
DomNode->node_type
DomNode->node_value
DomNode->owner_document
DomNode->parent_node
DomNode->prefix
DomNode->previous_sibling
DomNode->remove_child
DomNode->replace_child
DomNode->replace_node
DomNode->set_content
DomNode->set_name
DomNode->set_namespace
DomNode->unlink_node
DomProcessingInstruction->data
DomProcessingInstruction->target
DomXsltStylesheet->process()
DomXsltStylesheet->result_dump_file()
DomXsltStylesheet->result_dump_mem()
domxml_new_doc
domxml_open_file
domxml_open_mem
domxml_version
domxml_xmltree
domxml_xslt_stylesheet_doc
domxml_xslt_stylesheet_file
domxml_xslt_stylesheet
domxml_xslt_version
xpath_eval_expression
xpath_eval
xpath_new_context
xpath_register_ns_auto
xpath_register_ns
xptr_eval
xptr_new_context
eXTReMe Tracker