Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I might be approaching my problem from a remedial point of view. I have included a simple example xml file similar to my real data file. I have a simple working perl script to read it. My problem arises because I have a varying number of products under each group. I can use first_child to get the first one, but I don't know how to get each 'product' after that. From the documentation, I decided to determine how many of them there are using within_element.

The documentation also says you must use generate_ns_name(NAME,URI) to define $gs. I can't find an example of calling generate_ns_name or within_element.

My xml file looks like this: ========company.xml======= <comany> <group> <name>dev group 1</name> <product>product 1</product> <product>product 2</product> <product>product 3</product> <product>product 4</product> </group> <group> <name>dev group 2</name> <product>product 1</product> <product>product 2</product> </group> </company>
========company.pl======== #!/bin/perl -w use strict; use XML::Twig; my $field= 'group'; my $twig= new XML::Twig; $twig->parsefile( "company.xml"); # build the twig my $root= $twig->root; # get the root of the twig my @group = $root->children; # get the list foreach my $my_group (@group) # the sorted list { print "\nGroup Name: ",$my_group->first_child("name")->text; print "\nGroup Name: ",$my_group->first_child("product")->text; } ================
Thanks,
Patrick
patrick_breithaupt( at )mentorg.com

Edit kudra, 2001-09-24 Removed cpde tags around text

Replies are listed 'Best First'.
Re: Trying to use XML::Twig
by mirod (Canon) on Sep 21, 2001 at 20:11 UTC

    To find out how many product are in $nameyou can just do my $nb_product= $name->children( 'product'); (that's children in scalar context, in list context you would of course get the list of product children).

    As for the name space question could you please explain a little more what you want to do and what you have tried so far, I am pretty sure there is nothing about name spaces in the XML::Twig documentation.

    This post brought to you courtesy of tonio

      Thanks, I'm back from eating my gruel...
      http://www.standards.ieee.org/resources/spasystem/twig/twig_dev.html
      That is where I saw the
      within_element($gi) Call Expat within_element method. Returns the number of times the given name appears in the context list. If namespace processing is being used and you want to check against a name that may be in a namespace, then use the generate_ns_name method to create the $gi argument. Usefull when using the TwigRoots option.

        Consider this a bad case of cut'n paste. The doc for within_element is from the original XML::Parser::Expat method. In fact this method should only be used during the parsing, if you are using the twig_roots option, to get information about the original document, not the view on it defined by twig_roots... if that makes any sense.

        Basically you should use $elt->ancestors( $gi) in scalar context to getthe number of ancestors of type $gi for the element $elt. Which does not look like what you wanted anyway ;--(

        Thanks for letting me know about this silly documentation bit though, I will fix it in the next release.