如何删除特定命名空间的整个属性?

时间:2016-06-09 08:03:16

标签: php dom

我有一个像这样的XML(SVG)(例子中只包括部分):

<path id="path3991" sodipodi:nodetypes="ccccc" fill="#A05A2C" fill-opacity="0.5" stroke="#000000" stroke-width="0.3" d="M533.3,1012.4L464,874.9l3.2,0.2l69.1,137.5L533.3,1012.4z"/>

我想删除属于命名空间'sodipodi'的整个属性。

请求的结果是:

<path id="path3991" fill="#A05A2C" fill-opacity="0.5" stroke="#000000" stroke-width="0.3" d="M533.3,1012.4L464,874.9l3.2,0.2l69.1,137.5L533.3,1012.4z"/>

我现在做什么:

$finder = new \DOMXPath($SVG); 
$nodes = $finder->query("//*[namespace::{sodipodi} and not(../namespace::{sodipodi})]");
foreach ($nodes as $node) {
     $node->removeAttributeNS(node->lookupNamespaceURI($ns), $ns);
    }
}

结果是:

<path id="path3991" nodetypes="ccccc" fill="#A05A2C" fill-opacity="0.5" stroke="#000000" stroke-width="0.3" d="M533.3,1012.4L464,874.9l3.2,0.2l69.1,137.5L533.3,1012.4z"/>

因此,我不想删除属性的命名空间,而是删除整个属性。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

正则表达式会替换你的工作吗?

$SVG = '<path id="path3991" sodipodi:nodetypes="ccccc" fill="#A05A2C" fill-opacity="0.5" stroke="#000000" stroke-width="0.3" d="M533.3,1012.4L464,874.9l3.2,0.2l69.1,137.5L533.3,1012.4z"/>';
$SVG = preg_replace('/sodipodi:nodetypes=["\'][^\'"]+[\'"]/', '', $SVG);

$finder = new \DOMXPath($SVG);