Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a xml fragment as such:
<top_level>
   <name>test case</name>
   <junk>just some more tags</junk>
   <sub_level_1>
        <element name="attr1">
              <value format="inches">121</value>
              <modifier>add</modifier>
        </element>
        <element name="attr2">
              <value format="yards">1.5</value>
              <modifier>sub</modifier>
        </element>
   </sub_level_1>
</top_level>
In using XML::DT I can format it such that I get each value in the hash, but I am looking to reformat the hash via XML::DT.
%handler = ( -default => sub{$c}, 
            -type => {name => 'MAP',
                      junk => 'MAP',
                      sub_level_1 => 'SEQ', 
                      element => 'MAP'
                     });

returns
 $VAR = { name => 'test case',
          junk => 'just some more tags',
          sub_level_1 => start_array # submit form rejects bracket
                             {value => '121', modifier => 'add'},
                             {value => '1.5', modifier => 'sub'}
                            end_array}
what I would like to return is: (notice attributes are used as hash index)
                         
 $VAR = { name => 'test case',
          junk => 'just some more tags',
          sub_level_1 => { attr1 => {value => '121', modifier => 'add'},
                           attr2 => {value => '1.5', modifier => 'sub'}}}
Now I know this has to do with the declaring a sub function but I am having difficult with the pointers.
%handler = ( -default => sub{$c}, 
            -type => {name => 'MAP',
                      junk => 'MAP',
                      element => 'MAP'
                     },
            sub_level_1 => sub { split('\n', $c) } 
              # returns list of pointers
              # but need hash indexed by attribute name
              # This is where I need HELP - Please
           );

Thanks Joe

Replies are listed 'Best First'.
Re: XML::DT and hashes/pointers
by mirod (Canon) on Sep 19, 2000 at 21:36 UTC

    I would suggest you sent an email to the author of the module, Jose Joao Dias de Almeida <jj@di.uminho.pt>

    Also note that a new version (0.15) is just out on CPAN

Re: XML::DT and hashes/pointers
by Anonymous Monk on Sep 20, 2000 at 17:26 UTC
    use XML::DT;
    use Data::Dumper;
    my $filename = shift;
    
    %handler=(
      '-type' => {
              top_level => 'MAP',      #
              sub_level_1 => 'SEQ',    # of element  (1)
    #         name => 'STR',           # the default
    #         junk => 'STR',           # the default
              element => 'MAP'   },    # (2)
    
      '-default'  => sub{$c},
      element     => sub{ $c->{name} = $v{name}; $c},           # (3)
      sub_level_1 => sub{ +{ map { ($_->{name}, $_) } @$c } },  # (4)
    );
    print Dumper(dt($filename,%handler));
    
    
    __END__
    ________notes:    
    (1) See also SEQH instead of SEQ
    
    (2) beeing a MAP the attributes and the tagname are deleted => in this case
      we can add a element processing sub  (see (3))
    
    (3) $c is the ref to hash; "name" att is missing but it is available in %v
      In this sub we copied it
    # or if you prefer:
    #    element    => sub{ +{ %v, %$c}},   #add all atts in %v to $c
    
    (4) we have a list of ref to hash and we want a hash of hash
          for all the elements in @$c
             make a pair with the name value, and the complete element
    
    Um abraco
    J.Joao (jj@di.uminho.pt)