Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^3: How to return two and more values by parsing XML with XML::Rules?

by Anonymous Monk
on Nov 06, 2012 at 11:21 UTC ( [id://1002453]=note: print w/replies, xml ) Need Help??


in reply to Re^2: How to return two and more values by parsing XML with XML::Rules?
in thread How to return two and more values by parsing XML with XML::Rules?

I was referring to the yaml, and FWIW, ysh doesn't like that YAML, which I assume is supposed to be

This is what I came up with, which took waaay too long, the rules are hard to remember

#!/usr/bin/perl -- use strict; use warnings; use XML::Rules; use Data::Dump qw/ dd /; my $ta = XML::Rules->new( qw/ stripspaces 8 /, rules => { 'Outpatient_Services' => 'no content', 'Outpatient_Service' => 'as array no content', #~ 'Outpatient_Clinic' => 'content by AM_Key', 'Outpatient_Clinic' => sub { #~ $rule->( $tag_name, \%attrs, \@context, \@parent_data, $parser) #~ my ($tagname, $attrHash, $contexArray, $parentDataArray, $parser) = + @_; my $amk = $_[1]->{AM_Key} ; return unless $amk; { $amk => 1 }; }, #~ _default => sub { $_[0] => $_[1]->{_content} }, _default => 'content', 'Outpatient_Clinic_Special' => undef, }, ); my $ref = $ta->parsefile( 'pm1002448.xml' ); dd $ref; use YAML(); print YAML::Dump( $ref); __END__ { Outpatient_Services => { Outpatient_Service => [ { AM01 => 1 }, { AM01 => 1 }, { AM02 => 1 }, {}, { AM04 => 1 }, {}, ], }, } --- Outpatient_Services: Outpatient_Service: - AM01: 1 - AM01: 1 - AM02: 1 - {} - AM04: 1 - {}

Replies are listed 'Best First'.
Re^4: How to return two and more values by parsing XML with XML::Rules?
by vagabonding electron (Curate) on Nov 06, 2012 at 11:36 UTC
    Thank you very much!
    I noticed now ( but somewhat late :-) ) that your point was yaml output. I have corrected the output presentation in the original post and in the reply to your post.
    My main problem however ist that I need to represent the LK-Keys at the same hierarchy level as the AM-Keys. That is, I do need the Outpatient_Clinic_Special infos.
    Thank you again for your time I will try to build on your code.

      My main problem however ist that I need to represent the LK-Keys at the same hierarchy level as the AM-Keys.

      Your updated output doesn't show that. Only AM04 is found in your sample data.

      Anyway, see Re: How to return two and more values by parsing XML with XML::Rules?, save the key callback

      my $amlk = sub { print $_->xpath, "\n"; push @amk, $_->trimmed_text; };

      Then register some twig_handlers

      'AM_Key' => $amlk, 'LK_Key' => $amlk,
        Your updated output doesn't show that.
        I am getting old ...
        I used your code with XML::Twig for LK_Keys Re^2: How to return two and more values by parsing XML with XML::Rules? but your recent proposal with the callback is of course much better and also shows that I have to learn much much more ...
        It is furthermore still the question for me what module should I use as a main option by parsing the data related xml-files?
        Thank you very much again!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-03-19 10:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found