Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Replacing XML Tag name with another

by mirod (Canon)
on Sep 27, 2011 at 12:20 UTC ( #928076=note: print w/ replies, xml ) Need Help??


in reply to Replacing XML Tag name with another

If you replace 'H1' by 'Heading 1', the resulting data will no longer be well-formed XML, tag names cannot include spaces, so are you sure you want to do this?

Looking at your data, I am also not sure if the names are right. It looks to me like you have nested sections, unless you have quite complex headings, that include a title, text and sub-headings with more text. So maybe something like section1 and section2 would be more appropriate.

So with a proper XML parser (I am partial to XML::Twig), which makes the solution easier to write and more robust:

#!/usr/bin/perl use strict; use warnings; use XML::Twig; XML::Twig->new( twig_handlers => { H1 => sub { $_->set_tag( 'section1' +); }, H2 => sub { $_->set_tag( 'section2' +); }, }, pretty_print => 'indented', ) ->parsefile( $ARGV[0]) ->print;


Comment on Re: Replacing XML Tag name with another
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2014-09-21 00:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (165 votes), past polls