PHP - DOMDocument / DOMConfiguration

as indicated here, DOMConfiguration is not yet implemented. I need to normalize namespaces so that namespaces in child elements will be moved to the root element if prefixes are provided.

Is there any non regexp way to do this?

Through, I'm not sure if this is even possible DOMConfiguration , but I could not even try it, as you can see.

Perhaps there is a way to create libxml?

Thanks.

+4
source share
1 answer

Well, as I said, I rewrote my code to take care of the namespaces in the DOM extension. This works as follows:

class Foo { const XMLNS = 'http://www.w3.org/2000/xmlns/'; /** * Register a namespace with an optional prefix to the specified DOMElement. * * @param DOMElement $element * @param string $namespaceUri * @param string $prefix */ public static final function registerNS(DOMElement $element, $namespaceUri, $prefix=null) { Preconditions::checkNotEmpty($namespaceUri); $name = "xmlns"; if (!empty($prefix)) { $name .= ":$prefix"; } $element->setAttributeNS(self::XMLNS, $name, $namespaceUri); } } 

Now, every time (every time here, to ease the situation). I add an element as a child of another element, I call this method and basically provide documentElement as the first parameter to add namespaces to the root element:

 DOMDocument $doc = [...]; DOMElement $element = [...]; DOMElement $newChild = [...]; Foo::registerNS($doc->documentElement, $newChild->namespaceURI, $newChild->prefix); $element->appendChild($newChild); 
0
source

Source: https://habr.com/ru/post/1384086/


All Articles