I’m familiar with the DOMDocument::importNode method for importing a tree of nodes from some other document element.
However, what I was wondering is if I can automatically change the namespace prefix on a tree of nodes as I import them, that is, specify a new prefix for all nodes of that namespace.
Say the nodes, in their existing document, all have names like “name”, “identity”, and so on. When importing them into my new document they will be alongside other namespaces, so I’d like them to appear as “nicnames:name”, “nicnames:identity” and so on. I’d like to be able to change this prefix programmatically so that in another context I may be able to import them as, for instance, “myprefix:name”, “myprefix:identity” depending on the document they’re imported into.
Edit: as per the explanation in my answer, I figured out I don’t actually need to do this. I was misunderstanding namespaces in XML.
I discovered I’d misunderstood XML namespaces. They are actually much better than I thought they were.
I’d thought that each XML namespace used in a single document had to have a different namespace prefix. This is not true.
You can use different namespaces throughout your document even without namespace prefixes, just by including the xmlns attribute where appropriate, and that xmlns attribute only has effect for that element and its descendants, overriding the namespace for that prefix which may have been set higher up the tree.
For example, to have one namespace within another, you don’t have to do:
You can just do
Namespace prefixes are a good shortcut in certain situations, but not necessary when just including one document inside another of a different namespace.