慕斯王
这是一个 XML 命名空间前缀,如果没有匹配的 XML 命名空间定义,它是无效的。DOM 有特殊的方法来创建带有命名空间的 XML 节点:// a list of namespaces just to avoid repeating the URIs// the keys do not have to match the prefixes in the XML$namespaces = [ 'xmlns' => 'http://www.w3.org/2000/xmlns/', 'a' => 'urn:example:a', 'i' => 'urn:example:i'];$document = new DOMDocument();// the document element does not seem to have a namespace$identifiant = $document->appendChild( $document->createElement('identifiant'));// add the namespace definition for prefix "a"// namespace definition use a reserved, internal namespace$identifiant->setAttributeNS($namespaces['xmlns'], 'xmlns:a', $namespaces['a']);$identifiant->appendChild( // create an elment node with the namespace $document->createElementNS( $namespaces['a'], 'a:Nom' ))->textContent = 'NOM';$identifiant ->appendChild( $document->createElementNS( $namespaces['a'], 'a:NomJeuneFille' ) ) ->setAttributeNS( $namespaces['i'], 'i:nil', 'true' );$identifiant->appendChild( $document->createElementNS( $namespaces['a'], 'a:Prenom' ))->textContent = 'PRENOM';$document->formatOutput = TRUE;echo $document->saveXML();输出:<?xml version="1.0"?><identifiant xmlns:a="urn:example:a"> <a:Nom>NOM</a:Nom> <a:NomJeuneFille xmlns:i="urn:example:i" i:nil="true"/> <a:Prenom>PRENOM</a:Prenom></identifiant>将根据需要添加命名空间定义。您可以通过在祖先节点上手动定义它来避免在兄弟分支上重复它。我对“a”前缀执行此操作,“i”前缀的定义是自动添加的。