-1

I'm currently trying to use the following Perl code to filter an XML document for specific values.

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');
} 

The output I'm currently getting is

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

Where for example each result for Frame Rate is displayed on one line. I know this is happening because there are six timebase elements within each track, but my desired result would be to list the Track Name attribute and then values from each clipitem element within it separately. For example

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

Here is the source XML I am using

<?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>
Borodin
  • 126,100
  • 9
  • 70
  • 144
James
  • 11
  • 1
  • 1
    It would help a lot to see the XML data. Missing it out is probably why you've received two downvotes! – Borodin Aug 27 '18 at 00:33

1 Answers1

0

You've already done the right thing by using $node for the tracks (although $track would have been a better name) and there's no need for the ./ at the start of the XPath expressions. Just open another loop for the clipitem elements, like this

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 "";
}

I also suspect that there's no need for the double slash // at the start of the XPath to track. It just makes LibXML search through the entire data instead of checking at the root element, and if xmeml is the root element then there's no need: just one slash is correct.

Borodin
  • 126,100
  • 9
  • 70
  • 144
  • Hi Borodin thank you that answered my question perfectly. And apologies if my question was unclear, I'm new to Stack overflow and PERL but will try to be clearer going forward. It would be nice if the people who had down voted my question had given me a reason at least. I've included the source XML for reference in case it came in useful as a sample for people having the same issue in the future. – James Aug 27 '18 at 08:35
  • @James: *"It would be nice if the people who had down voted my question had given me a reason at least"* That's understandable, and it's why I suggested a reason. But voting is anonymous for a reason, and the vast majority of voters won't give a reason. You need to get used to it. – Borodin Aug 27 '18 at 08:40
  • @James: The language is spelled Perl, as you had in your subject line. It's not an acronym. (I have removed it from your subject line as it only duplicates information in the tags that you assigned). By the way, your question is nothing to do with XSLT! – Borodin Aug 27 '18 at 08:41
  • @James: You should aim to write any question so that we can run the code ourselves and experience the same behaviour. That normally means a (reduced) program, input data, and a thorough description of the unwanted behaviour. – Borodin Aug 27 '18 at 08:43
  • 1
    thanks for your further advice, I’ll make sure I’m precise and provide all code for reproducing the problem in the future. – James Aug 27 '18 at 12:02