in reply to XML dont include parent node

Hello there

Remember for future questions to be as precise as you can, so that others can help you effectively (consider to read Understanding-and-Using-PerlMonks).

I humbly think that xpath are not intended to do match as in your case (child1, child2, child3..). I also think another design for your data will be better, if you can choice: all tag are 'child' and each one have a numerical 'id'.

In Perl there are many way to get the work done (and speaking about xml they are many * many.. see the poll), so I present a XML::Twig solution. Handlers are subs that are called during parsing, here you can use a normal Perl regex to filter unwanted results (i putted an 'ufo' in the xml data..).
use warnings; use strict; use XML::Twig; my $xml=<<'XML'; <Root> <Parent> <child1>Child 1</child1> <child2>Child 2</child2> <child3>Child 3</child3> <ufo> Ufo there!</ufo> </Parent> </Root> XML my $twig= new XML::Twig( pretty_print => 'indented', twig_handlers => { '/Root/Parent/*' => \&fie +ld }, ); $twig->parse( $xml); sub field { my( $twig, $field)= @_; return unless $field->gi() =~ /^child/i; $field->print; #OR print $field->text(); } #OUTPUT # # <child1>Child 1</child1> # <child2>Child 2</child2> # <child3>Child 3</child3>


Hth
L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: XML dont include parent node
by Anonymous Monk on Dec 11, 2013 at 09:50 UTC

    I humbly think that xpath are not intended to do match as in your case (child1, child2, child3..)

    Hmm, but they seem to do exactly that, which kinda means they are intended to do it

    $ xmllint --xpath " //Parent " foot.xml <Parent> <child1/> <child2/> <child3/> </Parent> $ xmllint --xpath " //Parent/* " foot.xml <child1/><child2/><child3/> $ xmllint --xpath " /Root/*/*[starts-with(name(), 'child')] " foot.x +ml <child1/><child2/><child3/>

    ... design for your data ...

    I think too often the person asking how-something-xml doesn't have a choice in the design :)

      i suspected to be wrong there. thanks. Maybe that syntax (strats-with(name)..) is not available in XML::Twig, or, more probably i'm not able to get rid of:
      my @all = $twig->get_xpath ('/Root/Parent/child*'); #gives:error in xpath expression... my @all = $twig->get_xpath ('/Root/*/*[starts-with(name(), "child")] +'); #also gives error in xpath expression.. #someresults with the findnodes method..
      XML::Twig docs says these methods are similar to the XML::LibXML method. Being probably 'similar' the key word.

      Albeit, if someone is able...welcome!

      L*
      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.