douhuiyan2772 2016-10-06 07:13
浏览 103

从`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条回答 默认 最新

  • drau67562 2016-10-06 09:00
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler