如何在 PHP 中的一个元素中写入多个不同的 xml 命名空间
How to write multiple different xml namespace in one element in PHP
我想构建这个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">
这是我的 php 代码
public function build() {
$document = new DOMDocument("1.0", "utf-8");
$root = $document->createElementNS(
"http://schemas.xmlsoap.org/soap/envelope/",
"soapenv:Envelope"
);
$root->setAttributeNS(
"http://schemas.xmlsoap.org/soap/envelope/",
"xmlns",
"sec"
);
$document->appendChild($root);
return $document;
}
我的函数returns这个xml
xml: """
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" soapenv:xmlns="sec"/>
"""
我希望 soapenv 元素有 4 个不同的 xmlns。但是设置属性不起作用。
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();
我想构建这个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">
这是我的 php 代码
public function build() {
$document = new DOMDocument("1.0", "utf-8");
$root = $document->createElementNS(
"http://schemas.xmlsoap.org/soap/envelope/",
"soapenv:Envelope"
);
$root->setAttributeNS(
"http://schemas.xmlsoap.org/soap/envelope/",
"xmlns",
"sec"
);
$document->appendChild($root);
return $document;
}
我的函数returns这个xml
xml: """
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" soapenv:xmlns="sec"/>
"""
我希望 soapenv 元素有 4 个不同的 xmlns。但是设置属性不起作用。
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();