LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-03-2021, 09:44 AM   #1
littlebigman
Member
 
Registered: Aug 2008
Location: France
Posts: 660

Rep: Reputation: 35
Question [XMLStarlet] Edit+insert elements?


Hello,

In case someone here already knows how to use the XMLStarlet application to edit KML files, I need to…

1. Check if kml/Document/name exists: If it does, edit its name; If it doesn't, insert a new element
2. Add a sibling to all LineString elements.

It looks like XMLStarlet supports it, but I don't know enough about XPath yet to figure it out.

Thank you.

Code:
xml el input.kml

kml
kml/Document
kml/Document/Placemark
kml/Document/Placemark/name
kml/Document/Placemark/ExtendedData
kml/Document/Placemark/ExtendedData/Data
kml/Document/Placemark/ExtendedData/Data/value
kml/Document/Placemark/LineString
kml/Document/Placemark/LineString/coordinates
 
Old 09-03-2021, 12:05 PM   #2
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
I'm not sure what you're asking, it's very broad.
Assuming kml is some sort of subset of XML, then yes, xmlstarlet can.
Here's a good overview (no https!) I have perused.

Quote:
Originally Posted by littlebigman View Post
Hello,

In case someone here already knows how to use the XMLStarlet application to edit KML files, I need to…

1. Check if kml/Document/name exists: If it does, edit its name; If it doesn't, insert a new element
2. Add a sibling to all LineString elements.

It looks like XMLStarlet supports it, but I don't know enough about XPath yet to figure it out.

Thank you.

Code:
xml el input.kml

kml
kml/Document
kml/Document/Placemark
kml/Document/Placemark/name
kml/Document/Placemark/ExtendedData
kml/Document/Placemark/ExtendedData/Data
kml/Document/Placemark/ExtendedData/Data/value
kml/Document/Placemark/LineString
kml/Document/Placemark/LineString/coordinates
 
Old 09-04-2021, 07:11 AM   #3
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,688

Rep: Reputation: Disabled
The first task seems obvious from the output you provided.
Code:
#!/bin/sh
file='input.kml'
cmd=xml
name='Route Map'
# xmlstarlet is picky about namespaces
grep -q 'xmlns=' "$file" && ns=_: || ns=

if $cmd el "$file" | grep -qx 'kml/Document/name'
then $cmd ed -u //${ns}Document/${ns}name -v "$name" "$file"
else $cmd ed -s //${ns}Document -t elem -n name -v "$name" "$file"
fi
But I don't get what you're trying to do in the second task as it is too vaguely formulated. I mean xml ed -i //LineString and xml ed -a //LineString would insert an element just above or just below every LineString, respectively. Is that what you want?

Last edited by shruggy; 09-04-2021 at 09:46 AM.
 
1 members found this post helpful.
Old 09-17-2021, 10:43 AM   #4
littlebigman
Member
 
Registered: Aug 2008
Location: France
Posts: 660

Original Poster
Rep: Reputation: 35
Thank you.
 
  


Reply

Tags
xml



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Use XMLStarlet to parse XML in your the Linux terminal LXer Syndicated Linux News 0 07-13-2021 03:24 PM
Calculate using XMLStarlet martijndutch Linux - General 1 09-27-2018 01:29 AM
update xml file with xmlstarlet masonje Linux - General 1 05-28-2014 11:52 PM
In Javascript How to replace elements in one object with elements from another object pizzipie Linux - Software 1 05-08-2014 02:28 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration