我有一些XML
文件,其中包含“Squishreport”。
我想在条件下寻找所有节点的一些特殊值。为此,我在Perl
编写了一个代码,主要功能如下:
sub FileCount
{
chdir( $dir ) or die "Couldn't go inside $dir directory, $!";
opendir(my $dh, $dir) or die "$0: $dir: $!\n";
while (my $file = readdir($dh)) {
# We only want files
next unless (-f "$dir/$file");
# Use a regular expression to find files ending in .xml
next unless ($file =~ m/$suff$/);
my $xml = XML::LibXML->load_xml(location => $file);
# Iterate the entries
for my $test ($xml->findnodes('/SquishReport/test/test')) {
my $name_test = $test->findvalue('@name');
# trim the value
s/\A\s*//, s/\s*\z// for my $value_test = $test->textContent;
print "Test Name = $name_test \n";
for my $result ($test->findnodes('/verification/result')) {
my $type_result = $result->findvalue('@type');
# trim the value
s/\A\s*//, s/\s*\z// for my $value = $result->textContent;
print "$type_result = $value \n";
if (($type_result eq "WARNING") || ($type_result eq "FAIL") || ($type_result eq "ERROR") || ($type_result eq "FATAL")){
print "$type_result = $value \n";
print "*********** $name_test is not OK *********** \n\n ";
}
else{
print "*********** $name_test is OK *********** \n\n ";
}
}
print "\n";
}
#}
print "$file\n";
$count = $count + 1;
}
closedir($dh);
if ($count eq 1){
print "There is only one file in '' $dir '' directory.";
}
else{
print "There are $count files in '' $dir '' directory.";
}
}
目标是寻找第一个测试并输入名称。现在,如果PASS
中有type
,那么写下测试名称,然后说好,否则写入不行。
我不确定代码是否是结构/目标的正确代码。
我的问题是内部for
循环不起作用。 XML
文件的说明如下:
<SquishReport>
<test name="abcd">
<test name="EFGH">
<verification .......>
<result type="PASS">
<description>version check test case passes!</description>
<description type="DETAILED"></description>
.
.
.
<test name=IJKLM">
<verification .......>
<result type="PASS">
<description>version check test case passes!</description>
<description type="DETAILED"></description>
**编辑:**
这是XML
文件:
<SquishReport>
<test name="mat_tst_versions">
<prolog time="2015-06-16T13:49:54+02:00"/>
<message type="FATAL" time="2015-06-16T13:50:54+02:00">
<description><![CDATA[Execution error]]></description>
<description type="DETAILED"><![CDATA[squishrunner exited with code 3 when executing tst_start_app]]></description>
</message>
<test name="tst_check_versions">
<prolog time="2015-06-16T13:50:55+02:00"/>
<message line="274" type="LOG" file="C:\test\scripts\utilities.py" time="2015-06-16T13:50:59+02:00">
<description><![CDATA[is 64 bit windows version: True]]></description>
</message>
<message line="124" type="LOG" file="C:\test\mat_tst\tst_check_versions\test.py" time="2015-06-16T13:50:59+02:00">
<description><![CDATA[ path exists!]]></description>
</message>
<message line="129" type="FAIL" file="C:\test\mat_tst\tst_check_versions\test.py" time="2015-06-16T13:50:59+02:00">
<description><![CDATA[expected version: 1.0.0.107]]></description>
</message>
<verification line="132" type="" name="" file="C:\test\mat_tst\tst_check_versions\test.py">
<result type="PASS" time="2015-06-16T13:50:59+02:00">
<description>version check test case passes!</description>
<description type="DETAILED"></description>
</result>
</verification>
<epilog time="2015-06-16T13:50:59+02:00"/>
</test>
<test name="tst_scheduler_1">
<prolog time="2015-06-16T13:51:00+02:00"/>
<message line="62" type="LOG" file="C:\test\mat_tst\tst_scheduler_1\test.py" time="2015-06-16T13:51:00+02:00">
<description><![CDATA[count = 0]]></description>
</message>
<message line="125" type="LOG" file="C:\test\scripts\start_stop.py" time="2015-06-16T13:51:06+02:00">
<description><![CDATA[duration of start of C: 5132ms]]></description>
</message>
<message line="172" type="LOG" file="C:\test\mat_tst\tst_scheduler_1\test.py" time="2015-06-16T13:51:19+02:00">
<description><![CDATA[application name = C]]></description>
</message>
<message line="184" type="LOG" file="C:\test\mat_tst\tst_scheduler_1\test.py" time="2015-06-16T13:51:19+02:00">
<description><![CDATA[start application: C]]></description>
</message>
<message line="213" type="LOG" file="C:\test\mat_tst\tst_scheduler_1\test.py" time="2015-06-16T13:51:19+02:00">
<description><![CDATA[Scheduler Label exists]]></description>
</message>
<message line="219" type="LOG" file="C:\test\mat_tst\tst_scheduler_1\test.py" time="2015-06-16T13:51:19+02:00">
<description><![CDATA[Scheduler Counter exists]]></description>
</message>
<verification line="368" type="" name="" file="C:\test\mat_tst\tst_scheduler_1\test.py">
<result type="FAIL" time="2015-06-16T13:52:01+02:00">
<description>different types elements</description>
<description type="DETAILED"></description>
</result>
</verification>
<verification line="369" type="" name="" file="C:\test\mat_tst\tst_scheduler_1\test.py">
<result type="FAIL" time="2015-06-16T13:52:01+02:00">
<description>name_list= ['Scheduler', 'NCU', 'PLCSimAdv', 'simNCK', 'MCP']</description>
<description type="DETAILED"></description>
</result>
</verification>
<verification line="370" type="" name="" file="C:\test\mat_tst\tst_scheduler_1\test.py">
<result type="FAIL" time="2015-06-16T13:52:01+02:00">
<description>typ_list= ['Scheduler', u'NCU', u'PLCSimAdv']</description>
<description type="DETAILED"></description>
</result>
</verification>
<message line="393" type="LOG" file="C:\test\mat_tst\tst_scheduler_1\test.py" time="2015-06-16T13:52:08+02:00">
<description><![CDATA[:840evoVC.lblClockCounter_Edit = 0]]></description>
</message>
<verification line="411" type="" name="" file="C:\test\mat_tst\tst_scheduler_1\test.py">
<result type="FAIL" time="2015-06-16T13:52:08+02:00">
<description>scheduler counter 1. read, n=0: Scheduler= 0</description>
<description type="DETAILED"></description>
</result>
</verification>
<verification line="414" type="" name="" file="C:\test\mat_tst\tst_scheduler_1\test.py">
<result type="FAIL" time="2015-06-16T13:52:08+02:00">
<description>scheduler counter 2. read, n=0: Scheduler= 0</description>
<description type="DETAILED"></description>
</result>
</verification>
<message line="421" type="LOG" file="C:\test\mat_tst\tst_scheduler_1\test.py" time="2015-06-16T13:52:08+02:00">
<description><![CDATA[scheduler counter 1. read < counter 2. read, n=0: Scheduler= 0]]></description>
</message>
<message line="484" type="ERROR" file="C:\test\mat_tst\tst_scheduler_1\test.py" time="2015-06-16T13:52:29+02:00">
<description><![CDATA[Script Error]]></description>
<description type="DETAILED"><![CDATA[LookupError: Object ':840evoVC.chkSchedule_CheckBox_2' not ready.]]></description>
</message>
<epilog time="2015-06-16T13:52:29+02:00"/>
</test>
<test name="tst_run_plc">
<prolog time="2015-06-16T13:52:30+02:00"/>
<verification line="45" type="" name="" file="C:\test\mat_tst\tst_run_plc\test.py">
<result type="FAIL" time="2015-06-16T13:52:30+02:00">
<description>user path folder already exists!</description>
<description type="DETAILED"></description>
</result>
</verification>
<message line="125" type="LOG" file="C:\test\scripts\start_stop.py" time="2015-06-16T13:52:34+02:00">
<description><![CDATA[duration of start of 840evoVC: 4263ms]]></description>
</message>
<message line="54" type="LOG" file="C:\test\mat_tst_840evovc\tst_run_plc\test.py" time="2015-06-16T13:52:34+02:00">
<description><![CDATA[start ok, duration = 4263000]]></description>
</message>
<message line="64" type="LOG" file="C:\test\mat_tst_840evovc\tst_run_plc\test.py" time="2015-06-16T13:52:34+02:00">
<description><![CDATA[application started: 840evoVC]]></description>
</message>
<message line="78" type="LOG" file="C:\test\mat_tst_840evovc\tst_run_plc\test.py" time="2015-06-16T13:52:39+02:00">
<description><![CDATA[PLC Sim Advanced tmp user files are the expected files]]></description>
</message>
<message line="263" type="LOG" file="C:\test\scripts\start_stop.py" time="2015-06-16T13:52:45+02:00">
<description><![CDATA[all applications are closed]]></description>
</message>
<message line="87" type="LOG" file="C:\test\mat_tst_840evovc\tst_run_plc\test.py" time="2015-06-16T13:52:45+02:00">
<description><![CDATA[close application ok]]></description>
</message>
<epilog time="2015-06-16T13:52:45+02:00"/>
</test>
<test name="tst_app_resources">
<prolog time="2015-06-16T13:52:47+02:00"/>
<message line="65" type="LOG" file="C:\test\mat_tst\tst_app_resources\test.py" time="2015-06-16T13:52:49+02:00">
<description><![CDATA[count:0]]></description>
</message>
<message line="125" type="LOG" file="C:\test\scripts\start_stop.py" time="2015-06-16T13:52:53+02:00">
<description><![CDATA[duration of start: 4176ms]]></description>
</message>
<message line="73" type="LOG" file="C:\test\mat_tst\tst_app_resources\test.py" time="2015-06-16T13:52:53+02:00">
<description><![CDATA[start ok, duration = 4176000]]></description>
</message>
<message line="43" type="LOG" file="C:\test\scripts\win32utilities.py" time="2015-06-16T13:53:00+02:00">
<description><![CDATA[Application 840evovc only exists once]]></description>
</message>
<message line="138" type="ERROR" file="C:\test\mat_tst_\tst_app_resources\test.py" time="2015-06-16T13:53:30+02:00">
<description><![CDATA[Script Error]]></description>
<description type="DETAILED"><![CDATA[LookupError: Object ':chkSchedule_CheckBox_2' not ready.
Called from:
C:\test\mat_tst\tst_app_resources\test.py: 82]]></description>
</message>
<epilog time="2015-06-16T13:53:30+02:00"/>
</test>
<test name="tst_reset">
<prolog time="2015-06-16T13:53:32+02:00"/>
<message line="260" type="LOG" file="C:\test\scripts\start_stop.py" time="2015-06-16T13:56:12+02:00">
<description><![CDATA[Process exited.]]></description>
</message>
<message line="263" type="LOG" file="C:\test\scripts\start_stop.py" time="2015-06-16T13:56:12+02:00">
<description><![CDATA[all applications are closed]]></description>
</message>
<message line="284" type="LOG" file="C:\test\mat_tst\tst_reset\test.py" time="2015-06-16T13:56:12+02:00">
<description><![CDATA[application closed]]></description>
</message>
<verification line="287" type="" name="" file="C:\test\mat_tst\tst_reset\test.py">
<result type="PASS" time="2015-06-16T13:56:12+02:00">
<description>tst_reset_1 ok</description>
<description type="DETAILED"></description>
</result>
</verification>
<epilog time="2015-06-16T13:56:12+02:00"/>
</test>
<epilog time="2015-06-16T13:56:13+02:00"/>
</test>
答案 0 :(得分:2)
这不会有帮助:
if ($count eq 1)
- 应为==
readdir
?glob ( "$dir/*.xml" )
进行过滤
my $xml = XML::LibXML->load_xml(location => $file);
可能会破坏,因为您不更改目录。 (所以位置应该是$dir/$file
- 使用上述glob
避免的问题)然而,问题的核心是:
for my $result ( $test->findnodes('/verification/result') ) {
将其更改为:
for my $result ( $test->findnodes('./verification/result') ) {
你会得到:
Test Name = tst_app_resources
Test Name = tst_reset
*********** tst_reset is OK ***********