Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: perl substitute

by sundialsvc4 (Abbot)
on Nov 04, 2013 at 16:48 UTC ( #1061152=note: print w/replies, xml ) Need Help??

in reply to perl substitute

Having started with (very good!) XML::LibXML, now you need to finish with it.   You are, in fact, much closer to your goal than you dream.

Having parsed the file successfully, now you can use XPath expressions to “query” the file for the nodes that you want to, in this case, rename.   Then, by iterating through each returned list of nodes, you can change the content of the node including its name.   That is, if you want to do this using Perl . . .

You might also be interested to know that a technology known as XSLT can do this sort of thing without programming ... neither in Perl nor anything else.   (Well, except for the fact that “XSL” is very-much a programming tool unto itself, as any sort of tool meant to solve that particular problem must be.)   If you want a crystalline example of just how powerful this technology is, and how available it is, surf here:

(No, I am not going to spell-out for you all of the Magickal Mysteries that you will uncover here when you diligently use the “View Source” option of your browser . . .   Keep Digging.)

What you see here is an XHTML document that is directing your web browser to, on-the-fly(!) transform an external XML document (containing “information about all of the elements”) into the HTML representation that you now see being displayed.   (A small amount of JavaScript drives the interaction with the finished page, but not the construction of the page from its XML sources.   (I repeat... JavaScript did not do this!)

It appears to me that what you ultimately want to do to this file is to re-name a few of the nodes, wherever they might be found.   XSLT can do that, without Perl or anything else.

This being said, XML::LibXML gives Perl access to the same underlying functionality that you see your web-browser (or most XSLT processors) using.   (So, your web browser is not making Perl “look bad.”   “The full power of LibXML” is at your/Perl’s fingertips, too.)   In all cases, regardless of tool, you/they approach the problem in fundamentally the same way:   parse the XML document to a set of nodes, transform the nodes, then emit the node-structure as XML.   At no point do you/they attempt to treat the XML document as text.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1061152]
and the universe expands...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2018-06-22 17:36 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (124 votes). Check out past polls.