gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:05:40 25 4
gpt4 key购买 nike

我想构建这个 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;
}
我的函数返回这个 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();

关于php - 如何在 PHP 中的一个元素中编写多个不同的 xml 命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64735305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com