Hello Monks, This is my first foray into using xml and perl together. I found a thread about swapping xml elements, but unlike that thread, I want to swap them out from an external file. Here is what I have so far: first, an xml file with "head" elements that I want to tag up. So, I run this:
#!/usr/bin/perl -w use XML::Twig; my $file = $ARGV[0]; my $twig= new XML::Twig(TwigRoots => {head => 1}); $twig->parsefile($file); $twig->print;
which scrapes out all and only the head elements. Then I write it all to a file from the command line. That file looks something like this:
<TEI> <head>Note VII</head> <head>Title Page </head> <head>Copyright Page </head> <head>Preface </head> ... </TEI>
Next, I retag the head elements in a text editor. Specifically, I find all roman numerals, and tag them with an xml element. I know I could do this in the script, but in this case, I want to do it by hand for oversight. So, I end up with a list of head elements, tagged as I want them, looking something like this:
<TEI> <head>Note <romanNumeral>VII</romanNumeral></head> <head>Title Page </head> <head>Copyright Page </head> <head>Preface </head> ... </TEI>
Now, I want to go back through the file I extracted the head elements from, and replace the head tags there with the head tags I've extracted and tagged up in the external file. How would I do that? Thanks in advance for your help!

In reply to Swapping xml elements from an external file? by tonyz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.