develooper Front page | perl.perl6.users | Postings from July 2021

XML module question?

Thread Next
From:
William Michels via perl6-users
Date:
July 16, 2021 04:03
Subject:
XML module question?
Message ID:
CAA99HCwKPZQUwF8ebksLh5N58=ZpFdu5BgeDOC0VKJ=wv_9-ag@mail.gmail.com
Hi,

I'm trying to put an answer together for SO, and I keep running into the
same difficulty. Given a CSV file, how can one produce an XML file with
latitude and longitude values properly quoted? Every time I get proper
quoting, "lat" and "lon" are scrambled (i.e. sometimes "lat" first,
sometimes "lat" second). Conversely, if I manage to restrict "lat/lon" to
the correct order, I lose the equals sign and proper (value) quotation. The
input file is as below:

11-06-2014 - 10:49:06PM lat = 41.858657; lon = -91.345142
11-06-2014 - 10:49:49PM lat = 42.864653; lon = -92.349914
11-06-2014 - 10:50:35PM lat = 43.874808; lon = -93.350364
11-06-2014 - 10:51:21PM lat = 44.885047; lon = -94.350058
11-06-2014 - 10:52:08PM lat = 45.895078; lon = -95.349920
11-06-2014 - 10:53:30PM lat = 46.905178; lon = -96.349837
11-06-2014 - 10:54:50PM lat = 47.910303; lon = -97.350606

While the desired output gives lat, lon, and moves the date/time to the end
of the line:

<wpt lat="41.858657" lon="-91.345142">
<time>11-06-2014 - 10:49:06PM</time>
</wpt>
<wpt lat="42.864653" lon="-92.349914">
<time>11-06-2014 - 10:49:49PM</time>
</wpt>
<wpt lat="43.874808" lon="-93.350364">
<time>11-06-2014 - 10:50:35PM</time>
</wpt>
<wpt lat="44.885047" lon="-94.350058">
<time>11-06-2014 - 10:51:21PM</time>
</wpt>
<wpt lat="45.895078" lon="-95.349920">
<time>11-06-2014 - 10:52:08PM</time>
</wpt>
<wpt lat="46.905178" lon="-96.349837">
<time>11-06-2014 - 10:53:30PM</time>
</wpt>
<wpt lat="47.910303" lon="-97.350606">
<time>11-06-2014 - 10:54:50PM</time>
</wpt>


So far, I can correctly swap the date/time to the end:

$ raku -MXML -e 'my @reordered = do for lines()
{(.words[3..*],.words[0..2])}; .put for @reordered;'  lat_lon.txt
lat = 41.858657; lon = -91.345142 11-06-2014 - 10:49:06PM
lat = 42.864653; lon = -92.349914 11-06-2014 - 10:49:49PM
lat = 43.874808; lon = -93.350364 11-06-2014 - 10:50:35PM
lat = 44.885047; lon = -94.350058 11-06-2014 - 10:51:21PM
lat = 45.895078; lon = -95.349920 11-06-2014 - 10:52:08PM
lat = 46.905178; lon = -96.349837 11-06-2014 - 10:53:30PM
lat = 47.910303; lon = -97.350606 11-06-2014 - 10:54:50PM

But using the "make-xml()" function from the XML module, I end up
scrambling "lat/lon":

$ raku -MXML -e 'my @xml = do for lines() {make-xml("wpt",
:lat(.words[5].subst(";")), :lon(.words[8]), \("time",.words[0..2]))}; .put
for @xml;'  lat_lon.txt
<wpt lon="-91.345142" lat="41.858657"><time>11-06-2014 -
10:49:06PM</time></wpt>
<wpt lon="-92.349914" lat="42.864653"><time>11-06-2014 -
10:49:49PM</time></wpt>
<wpt lat="43.874808" lon="-93.350364"><time>11-06-2014 -
10:50:35PM</time></wpt>
<wpt lat="44.885047" lon="-94.350058"><time>11-06-2014 -
10:51:21PM</time></wpt>
<wpt lat="45.895078" lon="-95.349920"><time>11-06-2014 -
10:52:08PM</time></wpt>
<wpt lon="-96.349837" lat="46.905178"><time>11-06-2014 -
10:53:30PM</time></wpt>
<wpt lon="-97.350606" lat="47.910303"><time>11-06-2014 -
10:54:50PM</time></wpt>

Any help appreciated,

Thanks, Bill.

REFERENCE:  https://unix.stackexchange.com/q/166675/227738

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About