First off, you XML is not well-formed - don't forget to place quotes around the level values as well.

Second off, use XML::Simple instead . . .

use strict; use XML::Simple; local $/; # just so i can slurp __DATA__ my $old_xml = XMLin(<DATA>,forcearray=>1); my $new_xml; foreach my $heading (@{ $old_xml->{'heading'} }) { my $level = delete $heading->{'level'}; $new_xml->{"heading$level"} = $heading; } print XMLout($new_xml); __DATA__ <xml> <heading level="2">Introduction to Arguments</heading> <heading level="3"> <index primary-key="procedures" secondary-key="definition, rest argume +nts in"/> <index primary-key="rest arguments" secondary-key="specifying, in proc +edure definition"/> Specifying Rest Arguments in a Procedure Definition </heading> </xml>
The idea is to turn the XML into an anonymous data structure and mangle that instead - all you have to do is iterate thru the 'heading' hashes and remove the level attribute/key - then you create a new data structure whose keys are the concatenation of 'heading' with the level value.

This works, but i only tested it on the XML that i provided, your milleage may vary. ;)

output: <opt> <heading2>Introduction to Arguments</heading2> <heading3> Specifying Rest Arguments in a Procedure Definition <index primary-key="procedures" secondary-key="definition, rest argume +nts in" /> <index primary-key="rest arguments" secondary-key="spec +ifying, in procedure definition" /> </heading3> </opt>

jeffa

    A flute with no holes is not a flute . . .
a doughnut with no holes is a danish.
                                - Basho,
                                  famous philosopher

In reply to (jeffa) Re: Regex for XML attributes... by jeffa
in thread Regex for XML attributes... by tshabet

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.