【问题标题】:Perl LibXML and multiple namespacesPerl LibXML 和多个命名空间
【发布时间】: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);
    }
}

【问题讨论】:

  • -&gt;new 在失败时不返回 undef(它会引发异常),并且不是系统调用(所以 $! 将毫无意义)。

标签: xml perl xml-parsing namespaces libxml2


【解决方案1】:

在代码的第 10 行,您要求 XML::LibXML 解析 $Check,这意味着您要求 XML::LibXML 解析以下内容:

<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>

这不是一个格式良好的 XML 文档,因为它没有定义 dc

所有这些都是为了构建第二个不必要的 XPC。这可以通过删除大量代码来解决。

my $parser = XML::LibXML->new();
my $doc = $parser->parse_file($filename);
my $xpc = XML::LibXML::XPathContext->new( $doc->documentElement() );
$xpc->registerNs(x  => 'http://checklists.nist.gov/xccdf/1.1');
$xpc->registerNs(dc => 'http://purl.org/dc/elements/1.1');

my $check_content;
for my $rule_node ( $xpc->findnodes('//x:Rule') ) { 
   for my $check_content_node (
         $xpc->findnodes('x:check/x:check-content', $rule_node) ) { 
      $check_content .= $check_content_node->textContent();
   }
}

注意$xpc-&gt;findnodes 的第二个参数。

使用数组没有多大意义,所以我没有。如果有意义的话,您总是可以将$check_content 放入一个数组中。

当然,您也可以选择以下方法:

my $check_content;
for my $rule_node ( $xpc->findnodes('//x:Rule/x:check/x:check-content') ) { 
   $check_content .= $check_content_node->textContent();
}

【讨论】:

  • 不工作。它没有结合 并且我得到了所有 节点的 73 个实例。有 73 个 节点。
  • @ohm,它结合了相同规则的那些。调整为结合所有这些。
  • 它给了我所有的 而没有组合它们中的任何一个。此外,它还给了我 73 个 节点的副本。在它们应该组合之后,我将它们推入一个数组。 { push (@check_content, $check_content) } 然后我清除 $check_content { $check_content = ();顺便说一句,非常感谢您的帮助。你今天让我比 3 天的反复试验更进一步。我非常接近这项工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 2011-01-01
相关资源
最近更新 更多