如何在PHP中的一个元素中编写多个不同的xml名称空间

如何在PHP中的一个元素中编写多个不同的xml名称空间,php,xml,namespaces,domdocument,xml-namespaces,Php,Xml,Namespaces,Domdocument,Xml Namespaces,我想构建这个xml <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sec="http://xml.amadeus.com/2010/06/Security_v1" xmlns:link="http://wsdl.amadeus.com/2010/06/ws/Link_v1" xmlns:ses="htt

我想构建这个xml

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:sec="http://xml.amadeus.com/2010/06/Security_v1"
xmlns:link="http://wsdl.amadeus.com/2010/06/ws/Link_v1"
xmlns:ses="http://xml.amadeus.com/2010/06/Session_v3" 
xmlns:ns="http://www.iata.org/IATA/2015/00/2018.1/AirShoppingRQ">
我的函数返回这个xml

  xml: """
    <?xml version="1.0" encoding="utf-8"?>
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" soapenv:xmlns="sec"/>
    """
xml:“
"""

我希望soapenv元素有4个不同的XMLN。但是设置属性不起作用。

xmlns
引用XML中的保留命名空间。因此,您可以使用此命名空间显式地将定义添加为属性:

// a dictionary for the namespace URIs - to keep the source readable
$xmlns = [
  //  the reserved xmlns namespace     
  'xmlns' => 'http://www.w3.org/2000/xmlns/',
  // soap
  'soap' => 'http://schemas.xmlsoap.org/soap/envelope/',
  // others
  'security' => 'http://xml.amadeus.com/2010/06/Security_v1',
  'link' => 'http://wsdl.amadeus.com/2010/06/ws/Link_v1',
  'session' => 'http://xml.amadeus.com/2010/06/Session_v3',
  'air' => 'http://www.iata.org/IATA/2015/00/2018.1/AirShoppingRQ'
];

$document = new DOMDocument();
// adding an element with a namespace adds the definition implicitly
$document->appendChild(
  $envelope = $document->createElementNS($xmlns['soap'], 'soapenv:Envelope')
);
// or add the definition explicitly as attributes
$envelope->setAttributeNS($xmlns['xmlns'], 'xmlns:sec', $xmlns['security']);
$envelope->setAttributeNS($xmlns['xmlns'], 'xmlns:link', $xmlns['link']);
$envelope->setAttributeNS($xmlns['xmlns'], 'xmlns:ses', $xmlns['session']);
$envelope->setAttributeNS($xmlns['xmlns'], 'xmlns:ns', $xmlns['air']);

$document->formatOutput = TRUE;
echo $document->saveXML();
// a dictionary for the namespace URIs - to keep the source readable
$xmlns = [
  //  the reserved xmlns namespace     
  'xmlns' => 'http://www.w3.org/2000/xmlns/',
  // soap
  'soap' => 'http://schemas.xmlsoap.org/soap/envelope/',
  // others
  'security' => 'http://xml.amadeus.com/2010/06/Security_v1',
  'link' => 'http://wsdl.amadeus.com/2010/06/ws/Link_v1',
  'session' => 'http://xml.amadeus.com/2010/06/Session_v3',
  'air' => 'http://www.iata.org/IATA/2015/00/2018.1/AirShoppingRQ'
];

$document = new DOMDocument();
// adding an element with a namespace adds the definition implicitly
$document->appendChild(
  $envelope = $document->createElementNS($xmlns['soap'], 'soapenv:Envelope')
);
// or add the definition explicitly as attributes
$envelope->setAttributeNS($xmlns['xmlns'], 'xmlns:sec', $xmlns['security']);
$envelope->setAttributeNS($xmlns['xmlns'], 'xmlns:link', $xmlns['link']);
$envelope->setAttributeNS($xmlns['xmlns'], 'xmlns:ses', $xmlns['session']);
$envelope->setAttributeNS($xmlns['xmlns'], 'xmlns:ns', $xmlns['air']);

$document->formatOutput = TRUE;
echo $document->saveXML();