如何使用XML :: LibXML返回单独的结果

时间:2018-08-26 23:16:03

标签: xml perl xml-libxml

我目前正在尝试使用以下Perl代码为特定值过滤XML文档。

use 5.010;
use strict;
use warnings;

use XML::LibXML;

my $filename = 'FF_BLADEII###_UNCP_12_Clips.xml';

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

my $count = 1 ;

foreach my $node($dom->findnodes('//xmeml/sequence/media/video/track')) {

    say 'Track Name:', $node->findvalue('./@MZ.TrackName');

    say 'Clip item',     $count++ ;
    say 'Frame Rate:',   $node->findvalue('./clipitem/rate/timebase'),;
    say 'Timecode In:',  $node->findvalue('./clipitem/start');
    say 'Timecode Out:', $node->findvalue('./clipitem/end');
} 

我当前得到的输出是

Track Name:FF_BLADEII###_UNCP
Clip item1
Frame Rate:252525252525
Timecode In:01125120004800093000138004
Timecode Out:1125120004800093000138004179424
Track Name:CE_FF_BLADEII###_01_FUSED
Clip item2
Frame Rate:252525252525
Timecode In:01125120004800093000138004
Timecode Out:1125120004800093000138004167125

例如,Frame Rate的每个结果都显示在一行上。我知道这种情况的发生是因为每个timebase中有六个track元素,但是我想要的结果将是列出Track Name属性,然后列出其中的每个clipitem元素的值它分开。例如

Track Name:FF_BLADEII###_UNCP
Clip item1
Frame Rate:25
Timecode In:0
Timecode Out:1125
Clip item2
Frame Rate:25
Timecode In:1125
Timecode Out:12000
Clip item3
Frame Rate:25
Timecode In:12000
Timecode Out:48000
Clip item4
Frame Rate:25
Timecode In:48000
Timecode Out:93000
Clip item5
Frame Rate:25
Timecode In:93000
Timecode Out:138004
Clip item6
Frame Rate:25
Timecode In:138004
Timecode Out:179424

Track Name:CE_FF_BLADEII###_01_FUSED
Clip item1
Frame Rate:25
Timecode In:0
Timecode Out:1125
Clip item2
Frame Rate:25
Timecode In:1125
Timecode Out:12000
Clip item3
Frame Rate:25
Timecode In:12000
Timecode Out:48000
Clip item4
Frame Rate:25
Timecode In:48000
Timecode Out:93000
Clip item5
Frame Rate:25
Timecode In:93000
Timecode Out:138004
Clip item6
Frame Rate:25
Timecode In:138004
Timecode Out:167125

这是我正在使用的源XML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xmeml>
<xmeml version="4">
    <sequence id="sequence-2" TL.SQAudioVisibleBase="9" TL.SQVideoVisibleBase="0" TL.SQVisibleBaseTime="0" TL.SQAVDividerPosition="0.5" TL.SQHideShyTracks="0" TL.SQHeaderWidth="184" Monitor.ProgramZoomOut="499578347520000" Monitor.ProgramZoomIn="474176747520000" TL.SQTimePerPixel="11.015524079320114" MZ.EditLine="487629434880000" MZ.Sequence.PreviewFrameSizeHeight="1080" MZ.Sequence.PreviewFrameSizeWidth="1920" MZ.Sequence.PreviewRenderingClassID="1480868673" MZ.Sequence.PreviewRenderingPresetCodec="107" MZ.Sequence.EditingModeGUID="44cf6a27-e791-427a-a544-97439b366abf" MZ.Sequence.VideoTimeDisplayFormat="109" MZ.WorkOutPoint="1823062671360000" MZ.WorkInPoint="0" MZ.ZeroPoint="883975680000000" explodedTracks="true">
        <rate>
            <timebase>25</timebase>
            <ntsc>FALSE</ntsc>
        </rate>
        <name>FF_BLADEII###_UNCP</name>
        <media>
            <video>
                <track TL.SQTrackShy="0" TL.SQTrackExpandedHeight="50" TL.SQTrackExpanded="1" MZ.TrackTargeted="1" MZ.TrackName="FF_BLADEII###_UNCP">
                    <clipitem id="clipitem-73" frameBlend="FALSE">
                        <masterclipid>masterclip-3</masterclipid>
                        <name>FF_BLADEII###_UNCP</name>
                        <enabled>TRUE</enabled>
                        <duration>181474</duration>
                        <rate>
                            <timebase>25</timebase>
                            <ntsc>FALSE</ntsc>
                        </rate>
                        <start>0</start>
                        <end>1125</end>
                        <in>0</in>
                        <out>1125</out>
                    </clipitem>
                    <clipitem id="clipitem-74" frameBlend="FALSE">
                        <masterclipid>masterclip-3</masterclipid>
                        <name>FF_BLADEII###_UNCP</name>
                        <enabled>TRUE</enabled>
                        <duration>181474</duration>
                        <rate>
                            <timebase>25</timebase>
                            <ntsc>FALSE</ntsc>
                        </rate>
                        <start>1125</start>
                        <end>12000</end>
                        <in>1175</in>
                        <out>12050</out>
                    </clipitem>
                    <clipitem id="clipitem-75" frameBlend="FALSE">
                        <masterclipid>masterclip-3</masterclipid>
                        <name>FF_BLADEII###_UNCP</name>
                        <enabled>TRUE</enabled>
                        <duration>181474</duration>
                        <rate>
                            <timebase>25</timebase>
                            <ntsc>FALSE</ntsc>
                        </rate>
                        <start>12000</start>
                        <end>48000</end>
                        <in>12550</in>
                        <out>48550</out>
                    </clipitem>
                    <clipitem id="clipitem-76" frameBlend="FALSE">
                        <masterclipid>masterclip-3</masterclipid>
                        <name>FF_BLADEII###_UNCP</name>
                        <enabled>TRUE</enabled>
                        <duration>181474</duration>
                        <rate>
                            <timebase>25</timebase>
                            <ntsc>FALSE</ntsc>
                        </rate>
                        <start>48000</start>
                        <end>93000</end>
                        <in>48550</in>
                        <out>93550</out>
                    </clipitem>
                    <clipitem id="clipitem-77" frameBlend="FALSE">
                        <masterclipid>masterclip-3</masterclipid>
                        <name>FF_BLADEII###_UNCP</name>
                        <enabled>TRUE</enabled>
                        <duration>181474</duration>
                        <rate>
                            <timebase>25</timebase>
                            <ntsc>FALSE</ntsc>
                        </rate>
                        <start>93000</start>
                        <end>138004</end>
                        <in>95050</in>
                        <out>140054</out>

                    </clipitem>
                    <clipitem id="clipitem-78" frameBlend="FALSE">
                        <masterclipid>masterclip-3</masterclipid>
                        <name>FF_BLADEII###_UNCP</name>
                        <enabled>TRUE</enabled>
                        <duration>181474</duration>
                        <rate>
                            <timebase>25</timebase>
                            <ntsc>FALSE</ntsc>
                        </rate>
                        <start>138004</start>
                        <end>179424</end>
                        <in>140054</in>
                        <out>181474</out>
                    </clipitem>
                </track>
                <track TL.SQTrackShy="0" TL.SQTrackExpandedHeight="25" TL.SQTrackExpanded="0" MZ.TrackTargeted="0" MZ.TrackName="CE_FF_BLADEII###_01_FUSED">
                    <clipitem id="clipitem-79" frameBlend="FALSE">
                        <masterclipid>masterclip-4</masterclipid>
                        <name>CE_FF_BLADEII###_01</name>
                        <enabled>TRUE</enabled>
                        <duration>169175</duration>
                        <rate>
                            <timebase>25</timebase>
                            <ntsc>FALSE</ntsc>
                        </rate>
                        <start>0</start>
                        <end>1125</end>
                        <in>0</in>
                        <out>1125</out>
                    </clipitem>
                    <clipitem id="clipitem-80" frameBlend="FALSE">
                        <masterclipid>masterclip-4</masterclipid>
                        <name>CE_FF_BLADEII###_01</name>
                        <enabled>TRUE</enabled>
                        <duration>169175</duration>
                        <rate>
                            <timebase>25</timebase>
                            <ntsc>FALSE</ntsc>
                        </rate>
                        <start>1125</start>
                        <end>12000</end>
                        <in>1175</in>
                        <out>12050</out>
                    </clipitem>
                    <clipitem id="clipitem-81" frameBlend="FALSE">
                        <masterclipid>masterclip-4</masterclipid>
                        <name>CE_FF_BLADEII###_01</name>
                        <enabled>TRUE</enabled>
                        <duration>169175</duration>
                        <rate>
                            <timebase>25</timebase>
                            <ntsc>FALSE</ntsc>
                        </rate>
                        <start>12000</start>
                        <end>48000</end>
                        <in>12550</in>
                        <out>48550</out>
                    </clipitem>
                    <clipitem id="clipitem-82" frameBlend="FALSE">
                        <masterclipid>masterclip-4</masterclipid>
                        <name>CE_FF_BLADEII###_01</name>
                        <enabled>TRUE</enabled>
                        <duration>169175</duration>
                        <rate>
                            <timebase>25</timebase>
                            <ntsc>FALSE</ntsc>
                        </rate>
                        <start>48000</start>
                        <end>93000</end>
                        <in>48550</in>
                        <out>93550</out>
                    </clipitem>
                    <clipitem id="clipitem-83" frameBlend="FALSE">
                        <masterclipid>masterclip-4</masterclipid>
                        <name>CE_FF_BLADEII###_01</name>
                        <enabled>TRUE</enabled>
                        <duration>169175</duration>
                        <rate>
                            <timebase>25</timebase>
                            <ntsc>FALSE</ntsc>
                        </rate>
                        <start>93000</start>
                        <end>138004</end>
                        <in>95050</in>
                        <out>140054</out>
                    </clipitem>
                    <clipitem id="clipitem-84" frameBlend="FALSE">
                        <masterclipid>masterclip-4</masterclipid>
                        <name>CE_FF_BLADEII###_01</name>
                        <enabled>TRUE</enabled>
                        <duration>169175</duration>
                        <rate>
                            <timebase>25</timebase>
                            <ntsc>FALSE</ntsc>
                        </rate>
                        <start>138004</start>
                        <end>167125</end>
                        <in>140054</in>
                        <out>169175</out>
                    </clipitem>
                </track>
            </video>
        </media>
    </sequence>
</xmeml>

1 个答案:

答案 0 :(得分:0)

通过使用$node作为曲目,您已经做了正确的事情(尽管$track会是一个更好的名字),并且在开始时不需要./ XPath表达式。只需为clipitem元素打开另一个循环,就像这样

for my $track ( $dom->findnodes('//xmeml/sequence/media/video/track') ) {

    say 'Track Name: ',   $track->findvalue('@MZ.TrackName');

    my $count;

    for my $clip ( $track->findnodes('clipitem') ) {

        say 'Clip item', ++$count;

        say 'Frame Rate:   ', $clip->findvalue('rate/timebase');
        say 'Timecode In:  ', $clip->findvalue('start');
        say 'Timecode Out: ', $clip->findvalue('end');
    }

    say "";
}

我还怀疑,在//的XPath开头不需要双斜杠track。它只是使LibXML搜索整个数据,而不是检查根元素,如果xmeml是根元素,则没有必要:只需要一个斜杠即可。