Himanshu Bhardiya
Himanshu Bhardiya

Reputation: 1057

remove node from `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?

Upvotes: 0

Views: 478

Answers (1)

sevavietl
sevavietl

Reputation: 3802

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.

Upvotes: 1

Related Questions