doutong7216 2019-01-21 13:03
浏览 47
已采纳

使用PHP删除XLF中的重复条目

I have an XML file and I want to check with PHP if there are any duplicated entries and remove the unnecessary one. Im running through all trans-units, pushing the id into an array and check if the entry already exist in the array. But how can I remove the trans-unit if I find an already existing id?

My XLF and my PHP Code:

    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
        <file source-language="de" target-language="de" datatype="plaintext" original="messages" date="2018-08-24T14:49:31Z" product-name="test">
            <header/>
            <body>
                <trans-unit id="test">
                    <source>123</source>
                    <target/>
                </trans-unit>
                <trans-unit id="test2">
                   <source>123</source>
                   <target/>
                </trans-unit>
                <trans-unit id="test2">
                   <source>123</source>
                   <target/>
                </trans-unit>
                <trans-unit id="test3">
                   <source>123</source>
                   <target/>
                </trans-unit>
                <trans-unit id="test4">
                   <source>123</source>
                   <target/>
                </trans-unit>
            </body>
        </file>
    </xliff>


    function cleanUpXliffFile($file) {
        $transUnitIds = [];
        $xlif = simplexml_load_file($file);
        $xlif->file['source-language'] = "de";
        foreach($xlif->file->body->{'trans-unit'} as $item) {
            $unit = $item->attributes()->id;
            $transUnitId = $unit[0]->__toString();
            if(in_array($transUnitId, $transUnitIds)) {
                //DELETE THE CHILD
            }
            $transUnitIds[] = $transUnitId;
            if (!isset($item->target)) {
                $item->addChild("target");
            }

            if ($item->target->__toString() !== "") {
                $item->source = (string)$item->target;
                $item->target[0] = "";
            }
        }

        $xlif->saveXML($file);
    }
  • 写回答

1条回答 默认 最新

  • duana1986 2019-01-21 13:20
    关注

    A very simple little function that uses DOMDocument rather than simplexml seems to work OK. Obtain a reference to the trans-unit nodes and add the ID to an array if it does not previously exist and use removeChild to remove duplicated node. This does not do the additional fudging with target attribute.

    function cleanXMLFile( $file ){
        $dom=new DOMDocument;
        $dom->load( $file );
    
        $tmp=[];
        $col=$dom->getElementsByTagName( 'trans-unit' );
    
        foreach( $col as $node ){
            if( !array_key_exists( $node->getAttribute('id'), $tmp ) ) $tmp[ $node->getAttribute('id') ]=$node;
            else $node->parentNode->removeChild( $node );
        }
    
        $dom->save( $file );
    }
    
    cleanXMLFile( __DIR__ . '/xlf.xml' );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号