use strict; use warnings; use XML::LibXML; ## test script to verify the way xml is parsed## ## $xmlfile is set to data of a format that would be expected to be passed to this if used in a loop ## my $namespace; my $goodnamespace=1; my $namespacedeclared=0; my $xmlfile = '-1ListPerformances ...etc '; if ($xmlfile =~ m/xmlns\=/) { ## if a namespace is declared we want to extract it ## $namespacedeclared=1; ## since we have a namespace, set the namespacedelcare to 1 ## print "There is a namespace declared in the xmlfile. Extracting it.\n"; my @namespacesplit=split(/xmlns\=\"/,$xmlfile); ## split the xmlfile on the xmlns declaration. include the " so that we can discard this. ## my $tempnamespace=$namespacesplit[1]; ## $namespacesplit[0] will contain everything before the declaration. [1] will contain the namespace at the beginning. I like declare a new temp variable to use before feeding into the next split.## my @finalnamespacesplit=split(/">/,$tempnamespace); ## split our return again so that everything after the namespace gets split from the namespace ## $namespace=$finalnamespacesplit[0]; ## finalnamespacesplit[0] should have the name space. I have found that if the namespace is a "space" or a "tab" if it is declared but is nothing then the namespace won't work in processing the xml as it is invalid. Will test for this ## my $lengthtest=$namespace; $lengthtest =~ s/\s+//g; ## remove all spaces ## my $testlength = length ($lengthtest); ## check the length of the namespace once all spaces removed ## if ($testlength) { ## If the length of the string after removing spaces is greater than one, then we likely have a good namespace catches error if the namespace is just a combination of spaces/tabs## $goodnamespace=1; }else{ ## if stringlength is null then there were no characters in the namespace ## $goodnamespace=0; } }else{ print "There is not a namespace delcared in the xmlfile.\n"; ## edit when added to final program ## } if ($namespacedeclared==1 && $goodnamespace==1) { ## if a namespace is declared and if that namespace is good ## my $doc = XML::LibXML->load_xml(string => $xmlfile); print "Setting the namespace. And processing the XML.\n"; ## edit when added to final program ## my $xc = XML::LibXML::XPathContext->new( $doc->documentElement() ); $xc->registerNs( pfx => $namespace ); ## sets the namespace prefix to $namespace ## my @events = $xc->findnodes('//pfx:EventInfo'); foreach my $event (@events) { my $id = $xc->findvalue('pfx:EventId', $event); print "EventId: $id\n"; my @packs = $xc->findnodes('pfx:PreshowPackList/pfx:PreshowPack', $event); if ( not @packs ) { print "No Packs\n"; next; } for my $pack (@packs) { my $pack_id = $xc->findvalue('pfx:PackId', $pack) || "No PackId"; print "PackId: $pack_id\n"; } } }elsif (!$namespace && $namespacedeclared==0) { ## if namespace is null and there was not a namespace declared ## my $doc = XML::LibXML->load_xml(string => $xmlfile); print "No namespace set. Processing XML without.\n"; ## edit when added to final program ## my @events = $doc->findnodes('//EventInfo'); foreach my $event (@events) { my $id = $event->findvalue('./EventId'); print "EventId: $id\n"; my @packs = $event->findnodes('./PreshowPackList/PreshowPack'); if ( not @packs ) { print "No Packs\n"; next; } for my $pack (@packs) { #print $pack; my $pack_id = $pack->findvalue('PackId') || "No PackId"; print "PackId: $pack_id\n"; } } }elsif ($namespacedeclared==1 && $goodnamespace==0) { ## namespace was declared, but was not a valid namespace, just combination of spaces/tabs ## print "There was a namepace set in the XML file but it is a null value. Unable to continue with current code.\n"; ## edit when added to final program ## #return (0); ## Will add back when pushed into sub in main program ## } #return(1); ## will add back when pushed into main program ##