Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Hi,
I am using XML::XPath module for parsing XML document.In XML document if i have empty value.I could not get empty value.I got only non null value.
<Cell><Data ss:Type="Number">1</Data></Cell> <Cell><Data ss:Type="String">1</Data></Cell> <Cell><Data ss:Type="String">1</Data></Cell> <Cell><Data ss:Type="String"></Data></Cell> <Cell><Data ss:Type="String"></Data></Cell> <Cell><Data ss:Type="String"></Data></Cell> <Cell><Data ss:Type="String">695</Data></Cell> <Cell><Data ss:Type="String"></Data></Cell>
Consider this is source.I need to get all null and non null values
#use warnings; use strict; use XML::XPath; use XML::XPath::XMLParser; my $start='<?xml version="1.0" encoding="utf-8"?> <MultifamilyProperty xmlns:xsi="http://www.w3.org/2001/XMLSchema-insta +nce" xmlns:xsd="http://www.w3.org/2001/XMLSchema">'; print "Enter the Input File name\n"; my $input=<STDIN>; chomp($input); print "Enter the Output File name\n"; my $output=<STDIN>; #my $output="Check"; chomp($output); my $output_file="$output".".xml"; unlink("$output_file"); open(fh,"<$input"); my @xmlsource= <fh>; close fh; #dummy xml my $xmlsource ="@xmlsource"; my ($addrees_Sheet,$amenties_sheet,$unit_mix); if($xmlsource=~m/<Worksheet\s*ss:Name="(?!Amenities|UnitMix)([^\"]*?)\ +"/is) { $addrees_Sheet=$1; print "address_sheet:$addrees_Sheet\n"; } if($xmlsource=~m/<Worksheet\s*ss:Name="(Amen[^\"]*?)\"/is) { $amenties_sheet=$1; print "amen_sheet:$amenties_sheet\n"; } if($xmlsource=~m/<Worksheet\s*ss:Name="(UnitMi[^\"]*?)\"/is) { $unit_mix=$1; print "unixmxi_Sheet:$unit_mix\n"; } # print "Enter the addrees_Sheet name\n"; # my $addrees_Sheet=<STDIN>; # chomp($addrees_Sheet); # my $amenties_sheet="Amenities_1"; # my $unit_mix="UnitMix_1"; open(FH,'>:utf8',"$output_file"); print FH $start."\n"; print FH "\t\t".'<Address>'."\n"; my $add_att='@ss:Name="'.$addrees_Sheet."\""; my $ame_att='@ss:Name="'.$amenties_sheet."\""; my $uni_att='@ss:Name="'.$unit_mix."\""; my $add_path='/Workbook/Worksheet['.$add_att.']/Table/Row[position() > + 1]/Cell/Data/node()'; my $add1_path='/Workbook/Worksheet['.$add_att.']/Table/Row[2]/Cell[3]/ +Data/node()'; my $ame_path='/Workbook/Worksheet['.$ame_att.']/Table/Row[position() > + 1]/Cell[3]/Data/node()'; my $uni_path='/Workbook/Worksheet['.$uni_att.']/Table/Row[position() > + 1]/Cell/Data/node()'; my $xp = XML::XPath->new(filename =>$input); my $nodeset4 = $xp->find("$add_path"); my ($count,$count1); foreach my $node ($nodeset4->get_nodelist) { my $value=XML::XPath::XMLParser::as_string($node); $count++; if($count==4) { print FH "\t\t\t".'<Street>'.$value.'</Street>'."\n"; } if($count==5) { print FH "\t\t\t".'<City>'.$value.'</City>'."\n"; } if($count==6) { print FH "\t\t\t".'<State>'.$value.'</State>'."\n"; } if($count==7) { print FH "\t\t\t".'<Zip>'.$value.'</Zip>'."\n"; } } print FH "\t\t".'</Address>'."\n"; print FH "\t\t".'<Amenities>'."\n"; #my $nodeset1 = $xp->find('/Workbook/Worksheet[@ss:Name="$amenties_she +et"]/Table/Row[position() > 1]/Cell[3]/Data/node()'); my $nodeset1 = $xp->find("$ame_path"); foreach my $node ($nodeset1->get_nodelist) { my $value=XML::XPath::XMLParser::as_string($node); print FH "\t\t\t".'<Amenity>'.$value.'</Amenity>'."\n"; + } print FH "\t\t".'</Amenities>'."\n"; #my $nodeset2 = $xp->find('/Workbook/Worksheet[@ss:Name="$addrees_Shee +t"]/Table/Row[2]/Cell[3]/Data/node()'); my $nodeset2 = $xp->find("$add1_path"); foreach my $node ($nodeset2->get_nodelist) { my $value=XML::XPath::XMLParser::as_string($node); print FH "\t\t".'<BuildingName>'.$value.'</BuildingName>'."\n"; + } print FH "\t\t".'<Units>'."\n"; my ($max_r,$min_r,$max_sq,$min_sq,$bed,$bath,$units); #my $nodeset3 = $xp->find('/Workbook/Worksheet[@ss:Name="$unit_mix"]/T +able/Row[position() > 1]/Cell/Data/node()'); my $nodeset3 = $xp->find("$uni_path"); my $node_count=1; foreach my $node ($nodeset3->get_nodelist) { print "node_count:$node_count\n"; #<>; $node_count++; my $value=XML::XPath::XMLParser::as_string($node); $count1++; # my $value1=$node->getNodeValue; # print "value:$value1\n"; $units='<NumUnits xsi:nil="true" />'; $max_r='<MaxMonthlyRent xsi:nil="true" />'; $max_sq='<MaxUnitSizeSqFt xsi:nil="true" />'; if($count1==3) { $bed='<NumBeds>'.$value.'</NumBeds>'; } if($count1==4) { $bath='<NumBaths>'.$value.'</NumBaths>'; } if($count1==5) { $min_sq='<MinUnitSizeSqFt>'.$value.'</MinUnitSizeSqFt>'; } if($count1==6) { $min_r='<MinMonthlyRent>'.$value.'</MinMonthlyRent>'; print FH "\t\t\t".'<Unit>'."\n"; print FH "\t\t\t\t".$min_r."\n"; print FH "\t\t\t\t".$max_r."\n"; print FH "\t\t\t\t".$min_sq."\n"; print FH "\t\t\t\t".$max_sq."\n"; print FH "\t\t\t\t".$bed."\n"; print FH "\t\t\t\t".$bath."\n"; print FH "\t\t\t\t".$units."\n"; print FH "\t\t\t".'</Unit>'."\n"; undef $count1; } } print FH "\t\t".'</Units>'."\n"; print FH '</MultifamilyProperty>'; close FH;
Regards,
kumar
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to get empty tag value in XML::XPath
by roboticus (Chancellor) on Oct 12, 2011 at 10:06 UTC | |
by Anonymous Monk on Oct 12, 2011 at 10:27 UTC | |
by roboticus (Chancellor) on Oct 12, 2011 at 12:02 UTC | |
|
Re: How to get empty tag value in XML::XPath
by choroba (Cardinal) on Oct 12, 2011 at 10:36 UTC |