Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

simplexml - How to remove namespace with PHP SimpleXMLElement?

I have an XML structure that needs to look like this to work:

<yq1:ZwsCreateInfoRec xmlns:yq1="urn:test-com:document:test:soap:functions:mc-style">
      <ItPrice>1337</ItPrices>
</yq1:ZwsCreateInfoRec>

To try and achieve this I'm using SimpleXMLElement like this:

$xml = SimpleXMLElement('<yq1:ZwsCreteMaterialFrRef xmlns:yq1="urn:test-com:document:test:soap:functions:mc-style"></yq1:ZwsCreteMaterialFrRef>');
$xml->addChild('ItPrice', '1337');

When I now run $xml->asXML() it shows me this (Note the namespace on the ItPrice node):

<yq1:ZwsCreateInfoRec xmlns:yq1="urn:test-com:document:test:soap:functions:mc-style">
      <yq1:ItPrice>1337</yq1:ItPrices>
</yq1:ZwsCreateInfoRec>

For some reason the WebService doesn't work if I have the namespace on the child nodes like that but if I remove them it works. Is there a way for me to remove this or do I have to loop through this as a string and remove them manually?

Thanks

question from:https://stackoverflow.com/questions/65950150/how-to-remove-namespace-with-php-simplexmlelement

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use 3rd parameter with blank value for addChild method. $xml->addChild('ItPrice', '1337', '');

$xml = new SimpleXMLElement('<yq1:ZwsCreteMaterialFrRef xmlns:yq1="urn:test-com:document:test:soap:functions:mc-style"></yq1:ZwsCreteMaterialFrRef>');
$namespaces = $xml->getDocNamespaces();
$xml->addChild('ItPrice', '1337', '');
echo  $xml->asXml();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...