Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Re: XML Twig parse

by mirod (Canon)
on Jan 20, 2003 at 21:43 UTC ( [id://228502]=note: print w/replies, xml ) Need Help??


in reply to Re: XML Twig parse
in thread XML Twig parse

I will write more about conditions once I am happy with them ;--( At the moment they are processed by a very poor regex-based parser. I need to write a real XPath parser, probably using Parse::Yapp. Then the doc will be written... it will be the XPath spec! Except that this will be "navigational XPath", and that I will also have to properly identify the subset of XPath that can be used for twig_roots (all we know about an element is its ancestors, and maybe their attributes) or twig_handlers (we know everything that happened before the end tag of the element),so I know when to refuse an XPath expression if it is in the wrong context.

As for now, the doc for the development version is now generated through perldoc and is a little better x-linked, so I will add links to the description of the conditions from all navigation functions.

Replies are listed 'Best First'.
Re: Re: Re: XML Twig parse
by Anonymous Monk on Jan 21, 2003 at 14:21 UTC
    Thanks Mirod

    One last question, what is the syntax to get the children of children?

    I.E.

    The children of rra and or their children cdp_prep or database
    Thanks

      There is no level2_children method, but you might be able to use descendants, or just use a map. Note that you can use a regexp (which will be applied to the element name) as a condition. I am sure there are other ways as well, the condition can be a code ref for example, so you could test exactly what you want in there.

      Anyway, here is some example code:

      #!/usr/bin/perl -lw use strict; use XML::Twig; my $t= XML::Twig->new( pretty_print => 'indented', error_context => 1) ->parse( \*DATA); my $rra= $t->root; { print "results in document order:"; my @results= map { $_, $_->children( qr/cdp_prep|database/) } $rra-> +children; print $_->id foreach (@results); } { print "results by level then document order:"; my @results= $rra->children; push @results, map { $_->children( qr/^(cdp_prep|database)$/) } @res +ults; print $_->id foreach (@results); } { # works only if cdp_prep and database elements can only be found as # grand-children of rra print "results using descendants:"; print $_->id foreach ( $rra->children, $rra->descendants( qr/^(cdp_prep|database)$/) ); } __DATA__ <rra> <child id="child_01"> <cdp_prep id="cdp_prep_01">content</cdp_prep> <database id="database_01">content</database> <other id="other_01">content</other> <cdp_prep id="cdp_prep_02"> <other id="other_02">content</other> </cdp_prep> </child> <child id="child_02"> <other id="other_03">content</other> <database id="database_02">content</database> </child> <child id="child_03"> <other id="other_04"><other id="other_05">content</other></other> </child> </rra>

Log In?
Username:
Password:

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

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

    No recent polls found