1

I want to download river runoff (.xml) files from a ChartServer online and create a .txt file with the information by running a .sh script. But I'm having difficulty in getting the data out in the correct format.

I have added part of my script in hopes that someone can point me in the right direction on how to get this to work.

url="http://h-web01.nve.no/ChartServer/ShowData.aspx?req=getchart&ver=1.0&time=-10;0&vfmt=xml&chd=ds=htsr,rt=1,da=18,id=700.2.2.1001.0"

xmllint --xpath '//SeriesData/Serie/Point/Value' ${url} | tr '</Value>' '\n' | grep -v '^$' > value_2.2.txt
xmllint --xpath '//SeriesData/Serie/Point/DateTime' ${url} | tr '</DateTime>' '\n' | grep -v '^$' > datetime.txt

if [ -s datetime.txt ]; then
    while true; do
      read month     || break
      read day       || break
      read year      || break
      read hour      || break
      echo ${year} ${month} ${day} >> date_2.2.txt
done < datetime.txt

# Put the date and runoff file together
while read Q <&3 && read y m d <&4; do
    echo ${y} ${m} ${d} ${Q} >> runoff_2.2.txt
done 3<value_${fra}.${til}.txt 4<date_2.2.txt

I want the .txt file to contain:

yyyy month day value

However my code only gives the value for every other "DateTime":

yyyy hh:mm:ss month day value
yyyy hh:mm:ss month day 
yyyy hh:mm:ss month day value
...

Source data

<?xml version="1.0" encoding="utf-8"?>
<SeriesData>
  <Serie>
    <Legend>Glomma med kystområder (700.2.2), Vannføring (m³/s)</Legend>
    <Point>
      <DateTime>03/07/2020 12:00:00</DateTime>
      <Value>673.2365</Value>
    </Point>
    <Point>
      <DateTime>03/08/2020 12:00:00</DateTime>
      <Value>695.2465</Value>
    </Point>
    <Point>
      <DateTime>03/09/2020 12:00:00</DateTime>
      <Value>786.8168</Value>
    </Point>
    <Point>
      <DateTime>03/10/2020 12:00:00</DateTime>
      <Value>766.8459</Value>
    </Point>
    <Point>
      <DateTime>03/11/2020 12:00:00</DateTime>
      <Value>758.2921</Value>
    </Point>
    <!-- ...more data... -->
    <Point>
      <DateTime>03/16/2020 12:00:00</DateTime>
      <Value>702.8088</Value>
    </Point>
    <Statistics/>
  </Serie>
</SeriesData>

1 Answer 1

1

If you don't mind using xmlstarlet instead of xmllint you can use this

url="http://h-web01.nve.no/ChartServer/ShowData.aspx?req=getchart&ver=1.0&time=-10;0&vfmt=xml&chd=ds=htsr,rt=1,da=18,id=700.2.2.1001.0"
curl --silent "$url" |
    xmlstarlet sel -t -m '//SeriesData/Serie/Point' -v 'concat(substring(DateTime,7,4)," ",substring(DateTime,1,2)," ",substring(DateTime,4,2)," ",Value)' -n

Unfortunately the <DateTime/> element isn't a proper XML date so we have to split it by hand rather than using XPath date processing functions.

Output

2020 03 07 673.2365
2020 03 08 695.2465
2020 03 09 786.8168
2020 03 10 766.8459
2020 03 11 758.2921
...
2020 03 16 702.8088
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.