Unable to add namespace to an attribute with PHP's SimpleXML

前端 未结 3 1207
悲哀的现实
悲哀的现实 2020-11-30 10:40

I am creating an Atom feed, when I tried below to add xmlns:i as an attribute -

$node->addAttribute(\"xmlns:i\",\"http://www.w3.org/2001/XML         


        
3条回答
  •  星月不相逢
    2020-11-30 11:10

    I found this looking for the same thing, and none of the answers really worked for me. So, I tried a different route. If SimpleXML isn't managing namespace correctly, use DOM instead.

    So, something like this should work:

    $s = new simplexmlelement('');
    $d = dom_import_simplexml($s);
    $d->setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:i", "http://www.w3.org/2001/XMLSchema-instance");
    $s->addChild("bar", "bazy", "http://www.w3.org/2001/XMLSchema-instance");
    $f = $s->addChild("foo", "quux");
    $f->addAttribute("i:corge", "grault", "http://www.w3.org/2001/XMLSchema-instance");
    

    That will result in this:

    
    
       bazy
       quux
    
    

提交回复
热议问题