bobafifi has asked for the wisdom of the Perl Monks concerning the following question:

The MusicXML I'm importing to Sibelius was created using PDFtoMusic Pro which did a great job in capturing the music/lyrics but missed on the chord symbols, often putting them in other lyrics fields. I've been able to strip the chords in Sibelius using the Advanced Filter, but I'd rather comment out all the XML files for the lyric tags except lyric number="1" in one go using Perl. The problem is the closing tags for lyric number="2", lyric number="3" and lyric number="4" are all the same, i.e. </lyric>.

Change this:
<lyric number="1" justify="center" default-y="-78.58"> <syllabic>single</syllabic> <text font-family="Times New Roman" font-size="10" + font-weight="normal" font-style="normal" xml:lang="en-gb">love</text +> <extend/> </lyric> <lyric number="3" justify="left" default-y="-162.40"> <syllabic>single</syllabic> <text font-family="Krinitsky" font-size="13" font- +weight="normal" font-style="normal" xml:lang="en-gb">ƒ5</text> </lyric> <lyric number="4" justify="right" default-y="-180.32"> <syllabic>single</syllabic> <text font-family="Krinitsky" font-size="13" font- +weight="normal" font-style="normal" xml:lang="en-gb">MI</text> <extend/> </lyric>

to this:
<lyric number="1" justify="center" default-y="-78.58"> <syllabic>single</syllabic> <text font-family="Times New Roman" font-size="10" + font-weight="normal" font-style="normal" xml:lang="en-gb">love</text +> <extend/> </lyric> <!--<lyric number="3" justify="left" default-y="-162.4 +0"> <syllabic>single</syllabic> <text font-family="Krinitsky" font-size="13" font- +weight="normal" font-style="normal" xml:lang="en-gb">ƒ5</text> </lyric>--> <!--<lyric number="4" justify="right" default-y="-180. +32"> <syllabic>single</syllabic> <text font-family="Krinitsky" font-size="13" font- +weight="normal" font-style="normal" xml:lang="en-gb">MI</text> <extend/> </lyric>-->


Thanks!

Replies are listed 'Best First'.
Re: MusicXML - find/replace
by toolic (Bishop) on Jun 25, 2015 at 13:31 UTC
    A common approach to modifying an XML file is to use a parser, such as XML::Twig. There are other modules on CPAN. All parsers require an investment in time, but there are many examples out there on the internet.

    UPDATE: Fixed broken link.

      Thanks! Link was 404 - found it here http://search.cpan.org/~mirod/XML-Twig-3.49/Twig.pm
Re: MusicXML - find/replace
by choroba (Cardinal) on Jun 26, 2015 at 09:44 UTC
    When working with XML, I prefer XML::LibXML. It's a bit verbose, so I usually switch to its wrapper called XML::XSH2:
    open music.xml ; for //lyric[@number != 1] { insert comment xsh:serialize(.) before . ; delete . ; } save :b ;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ