Reputation: 13
I have a xml file containing thousands of entries, like:
<gml:featureMember>
<Feature>
<featureType>JCSOutput</featureType>
<property name="gml2_coordsys"></property>
<gml:PointProperty>
<gml:Point>
<gml:coordinates>4048313.294966287,5374397.792158723 </gml:coordinates>
</gml:Point>
</gml:PointProperty>
<property name="BEZEICHNUN">Anton-Bosch-Gasse</property>
<property name="WL_NUMMER">68</property>
</Feature>
</gml:featureMember>
<gml:featureMember>
<Feature>
<featureType>JCSOutput</featureType>
<property name="gml2_coordsys"></property>
<gml:PointProperty>
<gml:Point>
<gml:coordinates>4044355.0231338665,5365146.95116724 </gml:coordinates>
</gml:Point>
</gml:PointProperty>
<property name="BEZEICHNUN">Anschützgasse</property>
<property name="WL_NUMMER">67</property>
</Feature>
</gml:featureMember>
The script should search for a name given in a list (for example Anton-Bosch-Gasse) and copy the whole paragraph starting with <gml:featureMember>
to a new file
What would you use for this purpose - awk, sed, perl?
Upvotes: 1
Views: 4295
Reputation: 20280
Here is a solution like choroba's but using the Mojolicious suite. Its module Mojo::DOM traverses the XML using css3 selectors rather than xpath.
Here I find first all of the gml:featureMember
elements, then extracts the first one which has a descendant that matches.
#!/usr/bin/env perl
use strict;
use warnings;
use Mojo::DOM;
use Mojo::Util qw/slurp spurt/;
my $dom = Mojo::DOM->new->xml(1);
# read in from file
# $dom->parse( slurp 'myfile.xml' );
# but for the demo ...
$dom->parse(do{ local $/; <DATA> });
my $found =
$dom->find('gml\:featureMember')
->first(sub{
$_->find('property[name="BEZEICHNUN"]')
->first( qr/\QAnton-Bosch-Gasse/ )
});
spurt "$found", 'output.xml';
__DATA__
<gml:featureMember>
<Feature>
<featureType>JCSOutput</featureType>
<property name="gml2_coordsys"></property>
<gml:PointProperty>
<gml:Point>
<gml:coordinates>4048313.294966287,5374397.792158723 </gml:coordinates>
</gml:Point>
</gml:PointProperty>
<property name="BEZEICHNUN">Anton-Bosch-Gasse</property>
<property name="WL_NUMMER">68</property>
</Feature>
</gml:featureMember>
<gml:featureMember>
<Feature>
<featureType>JCSOutput</featureType>
<property name="gml2_coordsys"></property>
<gml:PointProperty>
<gml:Point>
<gml:coordinates>4044355.0231338665,5365146.95116724 </gml:coordinates>
</gml:Point>
</gml:PointProperty>
<property name="BEZEICHNUN">Anschützgasse</property>
<property name="WL_NUMMER">67</property>
</Feature>
</gml:featureMember>
For this example I grab the XML from the DATA section. You might use the commented code to parse from a file.
You can also be a little more efficient if you are sure that the property is two deep in the structure consistently.
my $found =
$dom->find('gml\:featureMember property[name="BEZEICHNUN"]')
->first( qr/\QAnton-Bosch-Gasse/ )
->parent
->parent;
Upvotes: 1
Reputation: 16161
Using xml_grep
, which comes with XML::Twig, you can write
$ xml_grep --root 'gml:featureMember' \ --cond 'property[string()="Anton-Bosch-Gasse"]' \ to_grep.xml > extract.xml
Upvotes: 3
Reputation: 241928
Sed and awk are not the right tools to parse XML. Reach for Perl:
#!/usr/bin/perl
use warnings;
use strict;
use XML::LibXML;
my $search = 'Anton-Bosch-Gasse';
# Put your real values here!
my $file = '1.xml';
my $uri = 'http://1.2.3';
my $xpc = XML::LibXML::XPathContext->new;
$xpc->registerNs('gml', $uri);
my $xml = XML::LibXML->load_xml(location => $file);
my $r = $xml->find("//property[.='$search']/ancestor::gml:featureMember");
print $_->serialize for @$r;
Or, if you find the above example too verbose, you can use xsh:
my $search = 'Anton-Bosch-Gasse' ;
register-namespace gml http://1.2.3 ; # Insert the real URI.
open 1.xml ; # Insert the real path.
ls //property[.=$search]/ancestor::gml:featureMember ;
Upvotes: 5