Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Some questions from beginning user of XML::LibXML and XPath

by Jim (Curate)
on Oct 16, 2012 at 16:27 UTC ( [id://999367]=note: print w/replies, xml ) Need Help??


in reply to Some questions from beginning user of XML::LibXML and XPath

I would have used regular expression pattern matching for this seemingly trivial text substitution (insertion) problem. The formatting of the XML is quite regular and straightforward. Both the string you're matching and the string you're replacing (enhancing) it with are distinct and uncomplicated. You say you "had a hell of a time getting XPath to work." I wouldn't have had the patience to try.

You're explicitly handling both the input text and the output text as binary data rather than as Unicode text? Why?

Here's the operation reduced to a Unicode-conformant one-liner:

C:\Temp>perl -CiO -i.bak -pe "s{(?<=[/\\]ReleaseDLL)(?=[/\\])}{32} if +m{^\s*<(?:Out|Int)Dir>}" fred.vcxproj C:\Temp>diff fred.vcxproj.bak fred.vcxproj 9,10c9,10 < <OutDir>.\../../products/bin/ReleaseDLL\</OutDir> < <IntDir>.\ReleaseDLL\</IntDir> --- > <OutDir>.\../../products/bin/ReleaseDLL32\</OutDir> > <IntDir>.\ReleaseDLL32\</IntDir> 15,16c15,16 < <OutDir>.\../../products/bin/ReleaseDLL\</OutDir> < <IntDir>.\ReleaseDLL\</IntDir> --- > <OutDir>.\../../products/bin/ReleaseDLL32\</OutDir> > <IntDir>.\ReleaseDLL32\</IntDir> C:\Temp>od -h -N 3 fred.vcxproj 0000000000 EF BB BF 0000000003 C:\Temp>

Modify the anchoring regular expression patterns to taste.

Doing it this way avoids the needless and undesirable reordering of the attributes of the <Project> element—and a lot of other XML folderol besides. It also handles the input and output properly as Unicode text rather than as binary data and leaves the existing UTF-8 byte order mark intact.

Modifying this one-liner to support file and folder name globs (wildcards) is left as an exercise for the reader.

UPDATE:  With modern versions of Perl, you can use the special look-behind assertion \K to obviate the separate pattern match used to anchor the substitution (insertion) to just those lines that have <OutDir> and <IntDir> elements on them.

C:\>perl -CiO -i.bak -pe "INIT { @ARGV = <@ARGV> } s{^\s*<(?:Out|Int)D +ir>.+?[/\\]ReleaseDLL\K}{32}" */*.vcxproj C:\>diff Temp\fred.vcxproj.bak Temp\fred.vcxproj 9,10c9,10 < <OutDir>.\../../products/bin/ReleaseDLL\</OutDir> < <IntDir>.\ReleaseDLL\</IntDir> --- > <OutDir>.\../../products/bin/ReleaseDLL32\</OutDir> > <IntDir>.\ReleaseDLL32\</IntDir> 15,16c15,16 < <OutDir>.\../../products/bin/ReleaseDLL\</OutDir> < <IntDir>.\ReleaseDLL\</IntDir> --- > <OutDir>.\../../products/bin/ReleaseDLL32\</OutDir> > <IntDir>.\ReleaseDLL32\</IntDir> C:\>

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://999367]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-04-19 11:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found