N
NiallBCarter
Hey folks
I have what I think is a simple task but one that I am struggling
with.
I have this KML (Slightly abridged)
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.0" xmlns:gml="http://
www.opengeospatial.org/standards/gml">
<Document>
<open>0</open>
<name>Gazetteer for Scotland</name>
<Placemark>
<name> Troon </name>
<description>A resort town on the coast of Kyle in Ayrshire, Troon
lies at the north end of Ayr Bay on a headland that extends into the
</description>
<Point id="t476">
<coordinates>-4.6562,55.54207,0</coordinates>
</Point>
<Link>http://www.geo.ed.ac.uk/scotgaz/towns/townfirst476.html
</Link>
</Placemark>
<Placemark>
<name> Niall Home </name>
<description>Not a lovely little home in the western isles
</description>
<Point id="t576">
<coordinates>-5.3454,53.46532,0</coordinates>
</Point>
<Link>http://www.geo.ed.ac.uk/scotgaz/towns/townfirst576.html
</Link>
</Placemark>
<ExtendedData xmlns:GforS="http://www.geo.ed.ac.uk/scotgaz/">
<GforS:Copyright>
<p>All Images and Text are Copyright (c) The Gazetteer for
Scotland 1995-2008 </p>
</GforS:Copyright>
</ExtendedData>
</Document>
</kml>
This KML is generated by a perl script lying on the web and so I am
able to 'get' this KML from the web and parse it into a DOM using the
script below:
#!/usr/bin/perl
use XML:
OM;
use Data:
umper;
use LWP::Simple;
use XML:
OM::XPath;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->env_proxy;
#Gets the contents of the URLs
my $sg_get = $ua->get('http://www.geo.ed.ac.uk/scotgaz/cgi_bin/mid/
module2/scotgaz.pl?xmin=232095&ymin=630744&xmax=232695&ymax=631344');
#Creates instance of new XML:
OM:
arser and uses to make new DOM
object
my $parser = new XML:
OM:
arser;
# Parses the contents of the 'got' URL
my $sg_dom = $parser->parse ($sg_get->content);
#Prints the contents of a DOM:
arsed document to the screen
#print Dumper($sg_dom);
#Saves the content of the DOM to a file
#$sg_dom->printToFile ("out.kml");
$sg_dom->dispose;
What I would actuallylike to do is use an XSL to pull out only the
Placemarks (including the placemark tags).
The reason for this is that I actually have four of these KML files
and I want to rip out all the Placemarks from each KML and then using
DOM (i know how to) put all the placemarks into one single KML and
serve this out to the user.
So how far have I got?
Well, the XSL file I am using is:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl
utput method="xml" indent="yes" version="1.0" omit-xml-
declaration ="no"/>
<xsl:template match="/Document/Placemark">
<xsl:for-each select="kml/document/Placemark">
<Placemark>
<name><xsl:for-each select="kml/document/Placemark/name"></name>
</Placemark>
</xsl:for-each>
);
As you can see it is only getting the name from each Placemark and it
doesn't work! When using with teh perl script:
#!/usr/local/bin/perl
use XML::XSLT;
use strict;
use warnings;
my $xsl="xsl.xsl";
my $xml="out.kml";
# Create an instance of XSLT
my $xslt = eval { XML::XSLT->new($xsl, warnings => 1, debug => 0) };
print $xslt->to_dom;
# Free the memory
$xslt->dispose();
It says that it cannot create an instance of the XSL! For the moment I
have written my KML to a file to ease things up a little.
So can any of you people help? In the past I have found you all really
helpful and so I hope you can help me with this one! Sorry for the
long post but I wanted to try to get all the info down!
Cheers,
Niall
I have what I think is a simple task but one that I am struggling
with.
I have this KML (Slightly abridged)
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.0" xmlns:gml="http://
www.opengeospatial.org/standards/gml">
<Document>
<open>0</open>
<name>Gazetteer for Scotland</name>
<Placemark>
<name> Troon </name>
<description>A resort town on the coast of Kyle in Ayrshire, Troon
lies at the north end of Ayr Bay on a headland that extends into the
</description>
<Point id="t476">
<coordinates>-4.6562,55.54207,0</coordinates>
</Point>
<Link>http://www.geo.ed.ac.uk/scotgaz/towns/townfirst476.html
</Link>
</Placemark>
<Placemark>
<name> Niall Home </name>
<description>Not a lovely little home in the western isles
</description>
<Point id="t576">
<coordinates>-5.3454,53.46532,0</coordinates>
</Point>
<Link>http://www.geo.ed.ac.uk/scotgaz/towns/townfirst576.html
</Link>
</Placemark>
<ExtendedData xmlns:GforS="http://www.geo.ed.ac.uk/scotgaz/">
<GforS:Copyright>
<p>All Images and Text are Copyright (c) The Gazetteer for
Scotland 1995-2008 </p>
</GforS:Copyright>
</ExtendedData>
</Document>
</kml>
This KML is generated by a perl script lying on the web and so I am
able to 'get' this KML from the web and parse it into a DOM using the
script below:
#!/usr/bin/perl
use XML:
use Data:
use LWP::Simple;
use XML:
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->env_proxy;
#Gets the contents of the URLs
my $sg_get = $ua->get('http://www.geo.ed.ac.uk/scotgaz/cgi_bin/mid/
module2/scotgaz.pl?xmin=232095&ymin=630744&xmax=232695&ymax=631344');
#Creates instance of new XML:
object
my $parser = new XML:
# Parses the contents of the 'got' URL
my $sg_dom = $parser->parse ($sg_get->content);
#Prints the contents of a DOM:
#print Dumper($sg_dom);
#Saves the content of the DOM to a file
#$sg_dom->printToFile ("out.kml");
$sg_dom->dispose;
What I would actuallylike to do is use an XSL to pull out only the
Placemarks (including the placemark tags).
The reason for this is that I actually have four of these KML files
and I want to rip out all the Placemarks from each KML and then using
DOM (i know how to) put all the placemarks into one single KML and
serve this out to the user.
So how far have I got?
Well, the XSL file I am using is:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl
declaration ="no"/>
<xsl:template match="/Document/Placemark">
<xsl:for-each select="kml/document/Placemark">
<Placemark>
<name><xsl:for-each select="kml/document/Placemark/name"></name>
</Placemark>
</xsl:for-each>
);
As you can see it is only getting the name from each Placemark and it
doesn't work! When using with teh perl script:
#!/usr/local/bin/perl
use XML::XSLT;
use strict;
use warnings;
my $xsl="xsl.xsl";
my $xml="out.kml";
# Create an instance of XSLT
my $xslt = eval { XML::XSLT->new($xsl, warnings => 1, debug => 0) };
print $xslt->to_dom;
# Free the memory
$xslt->dispose();
It says that it cannot create an instance of the XSL! For the moment I
have written my KML to a file to ease things up a little.
So can any of you people help? In the past I have found you all really
helpful and so I hope you can help me with this one! Sorry for the
long post but I wanted to try to get all the info down!
Cheers,
Niall