Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: XML Twig parse

by boo_radley (Parson)
on Jan 20, 2003 at 20:41 UTC ( [id://228487]=note: print w/replies, xml ) Need Help??


in reply to XML Twig parse

What I want to do is search for a specidifc tag, say maybe <ds> or the <v> tag.
you can specify search criteria in the call to the children method :
my @ds= $root->children("ds");# get only ds children
my @ds= $root->children('ds[@name="foo"]');# get only ds children with a name attribute of foo
and so on.

update Damn module authors jumping in :-) Hey mirod, you should expound on conditions in the docs a bit more, a lot of people get tripped up on them.

Replies are listed 'Best First'.
Re: Re: XML Twig parse
by mirod (Canon) on Jan 20, 2003 at 21:43 UTC

    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.

      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://228487]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-19 02:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found