【发布时间】:2014-01-25 08:06:54
【问题描述】:
我有一个问题,我肯定需要一些帮助。首先,要温柔。我是 perl 和 LibXML 的新手。我一直在解析文档并将元素放入一个数组中,然后将其写入电子表格列。在测试过程中发现一些节点有多个同名子节点。我需要将每个子节点中的文本组合到数组的一个元素中。 xml的格式为:
<Group id="V-3021"
xmlns="http://checklists.nist.gov/xccdf/1.1"
xmlns:dc="http://purl.org/dc/elements/1.1">
<title>blah blah blah</title>
<description>blah blah blah</description>
<Rule id="SV-41507r1_rule" severity="medium" weight="10.0">
<version>blah blah blah</version>
<title>blah blah blah</title>
<description>blah blah blah</description>
<reference>
<dc:title>blah blah blah</dc:title>
<dc:publisher>blah blah blahO</dc:publisher>
<dc:type>blah blah blah</dc:type>
<dc:subject>blah blah blah</dc:subject>
<dc:identifier>blah blah blah</dc:identifier>
</reference>
<fixtext fixref="F-3046r3_fix">blah blah blah</fixtext>
<check system="C-39986r2_chk">
<check-content-ref name="M" href="VMS_XCCDF_Benchmark_Network - Firewall - Cisco.xml"/>
<check-content>This is the text I want</check-content>
</check>
</Rule>
</Group>
但偶尔是这样的:
<Group id="V-3021"
xmlns="http://checklists.nist.gov/xccdf/1.1"
xmlns:dc="http://purl.org/dc/elements/1.1">
<title>blah blah blah</title>
<description>blah blah blah</description>
<Rule id="SV-41507r1_rule" severity="medium" weight="10.0">
<version>blah blah blah</version>
<title>blah blah blah</title>
<description>blah blah blah</description>
<reference>
<dc:title>blah blah blah</dc:title>
<dc:publisher>blah blah blahO</dc:publisher>
<dc:type>blah blah blah</dc:type>
<dc:subject>blah blah blah</dc:subject>
<dc:identifier>blah blah blah</dc:identifier>
</reference>
<fixtext fixref="F-3046r3_fix">blah blah blah</fixtext>
<check system="C-39986r2_chk">
<check-content-ref name="M" href="VMS_XCCDF_Benchmark_Network - Firewall - Cisco.xml"/>
<check-content>This is the text I want</check-content>
<check-content>This is more text that I wantto grab and add to the end of the above text
</check-content>
</check>
</Rule>
</Group>
我可以从“检查内容”中提取所有文本,但如果有多个文本,它会丢弃电子表格中的数据行。我需要能够说出类似的话:如果有 2 个或更多加入数据,则将数据推送到数组中。如果没有,只需将数据推送到数组中。现在这是问题所在。我试图将所有内容拉到“规则”下方,然后解析每个部分( to )并从 XML 的每个部分中提取“检查内容”。通过这样做,我应该能够在将数据推送到数组之前将两个“检查内容”部分连接在一起。问题是在“引用”节点(dc:) 下声明了一个命名空间。我试过注册这个命名空间没有运气。我实际上根本不关心那部分数据,但是当我尝试拉这部分(到)时,我收到一条错误消息,指出“:1:命名空间错误:未定义标题上的命名空间前缀 dc s>ECAT -1、ECAT-2、ECSC-1
my $parser = XML::LibXML->new() or die $!;
my $doc1 = $parser->parse_file($filename1);
my $xc1 = XML::LibXML::XPathContext->new($doc1->documentElement() );
$xc1->registerNs(x => 'http://checklists.nist.gov/xccdf/1.1');
$xc1->registerNs(dc => 'http://purl.org/dc/elements/1.1');
for $Check ( $xc1->findnodes('//x:Rule') ) {
my $doc2 = $parser->parse_string($Check); # Associate the NS with $Check
my $xc2 = XML::LibXML::XPathContext->new($doc2->documentElement());
$xc2->registerNs(x => 'http://checklists.nist.gov/xccdf/1.1');
foreach $Check_Content ( $xc2->findvalue('check-content') ) {
push (@Check_Content1, $Check_Content);
}
$result_string = $Check_Content1[0] . $Check_Content1[1];
push (@Check_Content, $result_string);
}
}
【问题讨论】:
-
->new在失败时不返回 undef(它会引发异常),并且不是系统调用(所以$!将毫无意义)。
标签: xml perl xml-parsing namespaces libxml2