簡體   English   中英

如何使用 PHP SimpleXMLElement 刪除命名空間?

[英]How to remove namespace with PHP SimpleXMLElement?

我有一個 XML 結構,需要看起來像這樣才能工作:

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

為了嘗試實現這一點,我正在使用 SimpleXMLElement,如下所示:

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

當我現在運行$xml->asXML()時,它會顯示這個(注意 ItPrice 節點上的命名空間):

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

出於某種原因,如果我在子節點上有這樣的命名空間,那么 WebService 不起作用,但如果我刪除它們,它就起作用了。 有沒有辦法讓我刪除它,或者我必須將它作為字符串循環並手動刪除它們?

謝謝

您可以將第三個參數與空白值一起用於 addChild 方法。 $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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM