从`SimpleXMLElement Object`中删除节点

XMl like below,

<text font-family="Helvetica" font-size="25" style="stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(0,0,0); fill-rule: nonzero; opacity: 1;" >
    <tspan x="-100" y="7.87" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(0,0,0); fill-rule: ; opacity: 1;">t</tspan>
    <tspan x="-93" y="7.87" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(0,0,0); fill-rule: ; opacity: 1;">e</tspan>
    <tspan x="-79" y="7.87" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(0,0,0); fill-rule: ; opacity: 1;">s</tspan>
    <tspan x="-66" y="7.87" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(0,0,0); fill-rule: ; opacity: 1;">t</tspan>
</text>

What i want to do is, keep first tspan and append all other tspan value on first tspan and remove all other.

He is the desired output,

<text font-family="Helvetica" font-size="25" style="stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(0,0,0); fill-rule: nonzero; opacity: 1;" >
    <tspan x="-100" y="7.87" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(0,0,0); fill-rule: ; opacity: 1;">test</tspan>
</text>

For this what i did,

$previousValue = null;
$first = $text->tspan[0];
foreach($text->tspan as $k2=>$span){
                if(is_object($span)){
                    $style = $span->attributes()->style;
                    if($previousValue) {
                        if(strcmp($style,$previousValue) === 0){
                            $first.=$span;
                            //$dom=dom_import_simplexml($span); $dom->parentNode->removeChild($dom);
                        }
                    }
                    $previousValue = $style;
                }
            }
            $text->tspan[0] = $first;

That will generate first node perfect, but not removing other nodes proper. I tried this even,

$dom=dom_import_simplexml($span); $dom->parentNode->removeChild($dom);

But it just removing 1 node and break the loop then. Dont know what happening there. Am i doing any mistake there?

1个回答



使用 SimpleXMLElement XPath ,您可以执行以下操作:</ p >

  $ xml = new SimpleXMLElement($ xmlString); 

$ texts = $ xml-&gt; xpath('// text / tspan / ..');
\ nforeach($ texts as $ text){
$ tspans = $ text-&gt; xpath('// tspan');;

$ currentTspan = array_shift($ tspans);

foreach( $ tspans as $ tspan){
if($ currentTspan ['style'] - &gt; asXML()!= $ tspan ['style'] - &gt; asXML()){
$ currentTspan = $ tspan; \ n继续;
}

$ currentTspan [0]。= $ tspan [0];

unset($ tspan [0]);
}
}
</ code> </ pre>

这是工作演示。 </ p>
\ n

我用 array_shift()在这里起作用。 它只是返回数组的第一个元素并将其删除。</ p>
</ div>

展开原文

原文

Using SimpleXMLElement and XPath you can do this like following:

$xml = new SimpleXMLElement($xmlString);

$texts = $xml->xpath('//text/tspan/..');

foreach ($texts as $text) {
    $tspans = $text->xpath('//tspan');;

    $currentTspan = array_shift($tspans);


    foreach ($tspans as $tspan) {
        if ($currentTspan['style']->asXML() != $tspan['style']->asXML()) {
            $currentTspan = $tspan;
            continue;
        }

        $currentTspan[0] .= $tspan[0];

        unset($tspan[0]);
    }
}

Here is working demo.

I have used array_shift() function here for the simplicity. All it does is returning the first element of an array and removes it.

duanhuo7441
duanhuo7441 这是最新的代码sandbox.onlinephpfunctions.com/code/ ...
接近 4 年之前 回复
doukang7501
doukang7501 谢谢你的回复,但仍然没有根据我的xml工作。 你看到了吗? 它也有g标签。 我知道我们已经离开了,小东西会让它发挥作用。 但我在xmlobject上是全新的
接近 4 年之前 回复
doukuilian8365
doukuilian8365 这是我的完整代码示例sandbox.onlinephpfunctions.com/code/ ...
接近 4 年之前 回复
dongtan1009
dongtan1009 我在xmlobject的foreach中使用这个cide,好像是tspans = $ xml-> xpath('// tspan'); 不在那里工作。 我刚刚在这里添加了部分xml。 你可以在这里看到我的完整xml。 printhubpro.co.uk/images/export_edited.svg
接近 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问