具有XML :: LibXML Dom(全局查找和替换XML)的Perl

时间:2018-12-14 05:46:10

标签: perl xml-libxml

我是DOMXML-LibXML的新手。

这是我的示例mathml(XML)文件。我的XML文件名是in.xml,我需要的最终输出XML文件名是out.xml。我想找到<mi>bcde</mi>,并且需要全局修改<mtext>pqsd</mtext>并存储在out.xml中。如何实现这一目标。

<math xmlns="http://www.w3.org/1998/Math/MathML">
    <mfrac>
         <mi>a</mi>
         <mrow>
            <mi>bcde</mi>
         </mrow>
    </mfrac>
    <msqrt>
        <mi>s</mi>
        <mi>e</mi>
        <mi>f</mi>
    </msqrt>
</math> 


#!/usr/bin/perl
use strict;
use warnings 'all';
use XML::LibXML;

my $mediaIdFrom = "MEDIAID_TEST";
my $VodItemIdFrom = "VODITEM_ID_TEST";
my $mediaId="";
my $vodItemId="";

my $filename = 'sample1.xml';
my $out_filename = "sample2.xml";

my $dom = XML::LibXML -> load_xml(location => $filename);

foreach $mediaId ($dom->findnodes('/ScheduleProvider/Episode/Media/@id')) {
    $mediaId->setValue("xx " . $mediaIdFrom . " yy");
}

foreach $vodItemId ($dom->findnodes('/ScheduleProvider/VoidItem/@id')) {
    $vodItemId->setValue($VodItemIdFrom);
}
#### for storing the output separate XML file
$dom->toFile($out_filename);`

1 个答案:

答案 0 :(得分:1)

您的XML具有名称空间,但是您的XPath查询没有名称空间,请参见findnodesman XML::LibXML::Node下的注释。该代码应该可以工作:

#!/usr/bin/perl
use strict;
use warnings;

use XML::LibXML;
use XML::LibXML::XPathContext;

my $dom = XML::LibXML->load_xml(string => <<'END_OF_XML');
<math xmlns="http://www.w3.org/1998/Math/MathML">
    <mfrac>
         <mi>a</mi>
         <mrow>
            <mi>bcde</mi>
         </mrow>
    </mfrac>
    <msqrt>
        <mi>s</mi>
        <mi>e</mi>
        <mi>f</mi>
    </msqrt>
</math>
END_OF_XML

my $xpc = XML::LibXML::XPathContext->new();
$xpc->registerNs('math', 'http://www.w3.org/1998/Math/MathML');

foreach my $node ($xpc->findnodes('/math:math/math:mfrac/math:mrow/math:mi', $dom)) {
    my $newNode = XML::LibXML::Element->new('mtext');
    $newNode->appendText('pqsd');

    $node->replaceNode($newNode);
}

print $dom->toString();

输出:

$ perl dummy.pl
<?xml version="1.0"?>
<math xmlns="http://www.w3.org/1998/Math/MathML">
    <mfrac>
         <mi>a</mi>
         <mrow>
             <mtext>pqsd</mtext>
         </mrow>
    </mfrac>
    <msqrt>
        <mi>s</mi>
        <mi>e</mi>
        <mi>f</mi>
    </msqrt>
</math>

编辑也许我误解了您的问题,您想替换所有出现的<mi>bcde</mi>吗?然后foreach将更改为

foreach my $node ($xpc->findnodes('//math:mi[text()="bcde"]', $dom)) {

EDIT 2 查找多个<mi>xyz</mi>并将其替换,您可以使用text=replacement命令行参数,即

foreach my $argv (@ARGV) {
    next
        unless my($find, $replace) = ($argv =~ /^([^=]+)=(.*)$/);

    foreach my $node ($xpc->findnodes(qq{//math:mi[text()="${find}"]}, $dom)) {
        my $newNode = XML::LibXML::Element->new('mtext');
        $newNode->appendText($replace);

        $node->replaceNode($newNode);
    }
}

您的替换示例为

$ perl dummy.pl bcde=pqsd

编辑3 <mi>xxx</mi>替换xxx具有多个字符的所有mtext

foreach my $node ($xpc->findnodes('//math:mi', $dom)) {
    my $text = $node->textContent();

    # strip surrounding white space from text
    $text =~ s/^\s+//;
    $text =~ s/\s+$//;

    # if text has more than one character then replace "mi" with "mtext"
    if (length($text) > 1) {
        my $newNode = XML::LibXML::Element->new('mtext');
        $newNode->appendText($text);

        $node->replaceNode($newNode);
    }
}