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' );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度