为什么SimpleXMLElement为所有肥皂孩子添加前缀?

问题描述 投票:2回答:1

我是第一次构建SOAP消息并使用PHP进行播放。

要求是qazxsw poi版本qazxsw poi with Style / Encoding:Document / Literal pattern。

使用SOAP

1.1

预期产量:

SimpleXMLElement

实际产量:

    $soapEnv = '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://www.w3.org/2003/05/soap-envelope"></soap:Envelope>';
    $soapEnvElement = new \SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?>' . $soapEnv);
    $soapEnvElement->addChild('soap:Header');
    $bodyElement = $soapEnvElement->addChild('soap:Body');
    $bodyElement->addChild('node');
    echo $soapEnvElement->asXML();

为什么会这样?

是否可以禁用此行为?

php xml soap simplexml
1个回答
0
投票

@ splash58的评论确实回答了你的问题。

链接的问题有一个答案,可以完美地解释发生了什么。

对于SimpleXML,c需要一个命名空间。如果指定一个,它将获取xmlns属性,因为之前未声明您指定的内容。如果没有为c指定命名空间,它将从父节点继承命名空间。这里唯一的选择是ns1。 (这发生在d。)

为了防止父命名空间的出现并省略空的xmlns,你需要在父节点上使用像<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://www.w3.org/2003/05/soap-envelope"> <soap:Header/> <soap:Body> <node/> </soap:Body> </soap:Envelope> 这样的命名空间。然后<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://www.w3.org/2003/05/soap-envelope"> <soap:Header/> <soap:Body> <soap:node/> </soap:Body> </soap:Envelope> 给你没有ns。

证明:

xmlns="http://example.com"

哪个输出:

$it->addChild('c', 'no ns', 'http://example.com')
© www.soinside.com 2019 - 2024. All rights reserved.