Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hi

So, um, why not use those rules? Then work with the resulting hash?

*tip* ?node_id=3989;BIT=XML%3A%3ARules-%3Enew;HIT=xml ... Re: XML::LibXML drives me to drinking

This might be something like what you were attempting

#!/usr/bin/perl -- use strict; use warnings; use XML::Rules; use Data::Dump qw/ dd /; my $rawxml = q{<?xml version="1.0" encoding="UTF-8"?> <root> <summary> <item> <value>1.0</value> </item> </summary> <detail1> <item> <value>2.0</value> </item> </detail1> <detail2> <item> <value>3.0</value> </item> </detail2> <value> 11 </value> </root> }; dd( XML::Rules->new( rules => [], )->parse( $rawxml ) ); dd( XML::Rules->inferRulesFromExample( $rawxml ) ); dd( XML::Rules->new( rules => XML::Rules->inferRulesFromExample( $rawx +ml ), )->parse( $rawxml ) ); my ( $summary, $detail1, $detail2 ) ; my $xr = XML::Rules->new( qw/ stripspaces 8 /, rules => { 'detail1,detail2,item,root,summary' => sub { return; }, 'value' => [ '/root/summary/item' => sub { ( $summary, $detail1, $detail2 ) = (); #reset $summary = $_[1]->{_content}; return; }, '/root/detail1/item' => sub { $detail1 = $_[1]->{_content}; return; }, '/root/detail2/item' => sub { $detail2 = $_[1]->{_content}; warn "$summary $detail1 $detail1\n"; return; }, sub { die "unexpected 'value' at ".join('/','',@{$_[2]}) } +, ], }, ); my $ret = $xr->parse( $rawxml ); dd( $ret ); __END__ $ perl xml-rules-1205065.pl { root => { _content => "\n\n\n\n\n", detail1 => { _content => "\n \n", item => { _content => "\n \n ", value => { _cont +ent => "2.0" } }, }, detail2 => { _content => "\n \n", item => { _content => "\n \n ", value => { _cont +ent => "3.0" } }, }, summary => { _content => "\n \n", item => { _content => "\n \n ", value => { _cont +ent => "1.0" } }, }, value => { _content => " 11 " }, }, } { "detail1,detail2,item,root,summary" => "no content", "value" => "content", } { root => { detail1 => { item => { value => "2.0" } }, detail2 => { item => { value => "3.0" } }, summary => { item => { value => "1.0" } }, value => " 11 ", }, } 1.0 2.0 2.0 unexpected 'value' at /root at xml-rules-1205065.pl line 54.

In reply to Re^3: XML::Rules parsing inside out? by Anonymous Monk
in thread XML::Rules parsing inside out? by bfdi533

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (1)
As of 2024-04-16 18:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found