change soap prefixes in php

前端 未结 2 392
小鲜肉
小鲜肉 2020-12-11 16:16

i\'m rewriting a soap web service from .net to php. by default, php is giving me tags that look like this:



        
2条回答
  •  离开以前
    2020-12-11 17:17

    If you don't want to use Regular expressions, I've done this quick class implementation which uses DomXPath and DomDocument to cleanup the XML and append the namespace attribute at the node level.

    loadXML($request);
    
            // Create a XPath object
            $path = new DOMXPath($dom);
    
            // Search the nodes to fix
            $nodesToFix = $path->query('//SOAP-ENV:Envelope/SOAP-ENV:Body/*', null, true);
    
            // Remove unwanted namespaces
            $this->fixNamespace($nodesToFix, 'ns1', 'http://tempuri.org/');
    
            // Save the modified SOAP request
            $request = $dom->saveXML();
    
            return parent::__doRequest($request, $location, $action, $version);
        }
    
        public function fixNamespace(DOMNodeList $nodes, $namespace, $value) {
            // Remove namespace from envelope
            $nodes->item(0)
                    ->ownerDocument
                    ->firstChild
                    ->removeAttributeNS($value, $namespace);
    
            //iterate through the node list and remove namespace
    
            foreach ($nodes as $node) {
    
                $nodeName = str_replace($namespace . ':', '', $node->nodeName);
                $newNode = $node->ownerDocument->createElement($nodeName);
    
                // Append namespace at the node level
                $newNode->setAttribute('xmlns', $value);
    
                // And replace former node
                $node->parentNode->replaceChild($newNode, $node);
            }
        }
    }
    

提交回复
热议问题