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.

    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型