Royeh
Royeh

Reputation: 161

remove XML nodes with attribute in Perl

I am new to Perl and I have lots of XML files with the same following structure:

<reult>
    <test name="test1">
        <meassage type="passed">
            <description><![CDATA[  ===>> passed is here]]></description>
        </meassage>
        <check name="" line="150">
            <result type="text"></result>
            <description type="DETAILED"></description>
        </check>
        <meassage type="error">
            <description><![CDATA[  ===>> error is here]]></description>
        </meassage>
        <meassage type="passed">
            <description><![CDATA[  ===>> passed is here]]></description>
        </meassage>
    </test>
    <test name="test2">
        <meassage type="warning">
            <description><![CDATA[  ===>> warning is here]]></description>
        </meassage>
        <meassage type="fail">
            <description><![CDATA[  ===>> fail is here]]></description>
        </meassage>
        <meassage type="passed">
            <description><![CDATA[  ===>> passed is here]]></description>
        </meassage>
        <check name="" line="100">
            <result type="text"></result>
            <description type="DETAILED"></description>
        </check>
    </test>
</reult>

What I want to do is reading each test using for and then delete every message nodes which is irrelevant is for me. It means, all nodes having not with "fail", "error", and "warning" as attribute must be deleted.

I started to do with deleting all children of test with message, but it will delete what it should stay. Here is the main part of code only for deleting which I could not figure out.

my $cut_name = 'message'; 
my $xml_file = 'test.xml';

my $mainXml = XML::Simple::Tree->new( file => $xml_file,
                        node_key => 'directory',
                        target_key => 'name');

$mainXml->cut_node($cut_name);

After deleting all irrelevant nodes I will save the result as a new XML file. By the way, would you please let me know how I could delete nodes using splice with attribute?

Upvotes: 0

Views: 248

Answers (1)

daxim
daxim

Reputation: 39158

http://p3rl.org/xml_grep

xml_grep \
    --cond 'meassage[@name="error"]' \
    --cond 'meassage[@name="fail"]' \
    --cond 'meassage[@name="warning"]' \
    so50214597.xml

Upvotes: 1

Related Questions