xmlstarlet template parse small xml file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting xmlstarlet template parse small xml file
# 1  
Old 05-11-2010
xmlstarlet template parse small xml file

I have a file like:
Code:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<geonames>
<geoname>
<toponymName>Palos Verdes</toponymName>
<name>Palos Verdes</name>
<lat>42.1628912</lat>
<lng>-123.6481235</lng>
<geonameId>5718340</geonameId>
<countryCode>US</countryCode>
<countryName>United States</countryName>
<fcl>P</fcl>
<fcode>PPL</fcode>
<distance>0.1304</distance>
</geoname>
</geonames>

which I was using awk to parse <toponymName> of Palos Verdes, but that's 2 words, which is 2 fields, so I am trying to update my code to use xmlstarlet, so far here's what I have:
Code:
lat="42.163485"
lon="-123.646759"
url="http://ws.geonames.org/findNearbyPlaceName?lat=$lat&lng=$lon"
curl -s "$url" > xmlfile
city=`/usr/bin/xmlstarlet sel -m -v "toponymName" xmlfile`
country_code=`/usr/bin/xmlstarlet sel -m -v "countryCode" xmlfile`
echo "city $city"
echo "country_code: $country_code"

and I get the error:
Code:
error in arguments: no -t or --template options found

I'm not clear what the role of a template is, is this some class I have to call up, does it already exist, or do you have to make one for each style of xml file?
# 2  
Old 05-11-2010
"-m" expects an expression, it needs an argument.

---------- Post updated at 11:52 AM ---------- Previous update was at 11:47 AM ----------

Here's some examples for xmlstarlet. The -t option is mandatory for this, though it doesn't need a file, apparently the 'sel' options and the like are specifying a template themselves.
# 3  
Old 05-11-2010
I moved the -m tag to match my "city" term, still no joy. I'm still trying to understand the context of when you do/don't need a template, and if there already existing "classes" of templates I can call up?
# 4  
Old 05-11-2010
My understanding of this is limited as well, but as I get it so far, the options you're specifying are the template. You can also give it a template stored in a file.
# 5  
Old 05-11-2010
got it!

here's my working code:
Code:
lat="42.163485"
lon="-123.646759"
url="http://ws.geonames.org/findNearbyPlaceName?lat=$lat&lng=$lon"
curl -s "$url" > xmlfile
city=`/usr/bin/xmlstarlet sel -t -m //geoname -v toponymName xmlfile`
country_code=`/usr/bin/xmlstarlet sel -t -m //geoname -v countryCode xmlfile`
echo "city $city"
echo "country_code: $country_code"

what I learned was:

1. sel - that's what you want if you want to query something from your xml file
2. -t - the template is that you specify something you want to match, which is why you have -m next
3. -m - that's the match part, what you want to match, or try to find. In order to match something, you have to know something about the structure of your xml file, to tell xmlstarlet where to go looking. In my case, it had to start looking in the element <geoname>, since that's where the child element I want lives. You can find out your structure by running xmlstarlet el xmlfile, that will tell you the element structure without the gobblygook.
4. -v - this is what you're actually searching for, in my case what's in the child element <toponymName>
5. file - your source xml file

Feel free to correct me if any of this is wrong, just trying to learn and then simplify it for the next guy Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to insert subnode in xml file using xmlstarlet or any other bash command?

I have multiple xml files where i want to update a subnode if the subnode project points to different project or insert a subnode if it doesn't exist using a xmlstarlet or any other command that can be used in a bash script. I have been able to update the subnode project if it doesn't point to... (1 Reply)
Discussion started by: Sekhar419
1 Replies

2. UNIX for Dummies Questions & Answers

XML File Generation - Template Help

Hi, I have hit a bit of a brick wall.:confused: need the following code edited: echo "<?xml version=\"1.0\"?><dailyBalance_ROWSET>" > ${DataDir}/${extract_script}${ApplicationDate}.${Suffix} RunSQL ${extract_script} ${ActionFlag} echo "</dailyBalance_ROWSET>" >>... (2 Replies)
Discussion started by: Xergxes7
2 Replies

3. Shell Programming and Scripting

Creating a larger .xml file from a template(sample file)

Dear All, I have a template xml file like below. ....Some---Header....... <SignalPreference> ... <SignalName>STRING</SignalName> ... </SignalPreference> ......Some formatting text....... <SignalPreference> ......... ... (3 Replies)
Discussion started by: ks_reddy
3 Replies

4. Shell Programming and Scripting

xmlstarlet parse non en_US characters

I'm parsing around 600K xml files, with roughly 1500 lines of text in each, some of the lines include Chinese, Russian, whatever language, with a bash script that uses cat $i | xmlstarlet sel -t -m "//section1/section2/section3/section4/section5" -v "@VALUE" -n > somefile which works, but I... (15 Replies)
Discussion started by: unclecameron
15 Replies

5. Shell Programming and Scripting

xmlstarlet parse field from file

I have a xmlfile like this: <?xml version="1.0" encoding="utf-8"?> <contentlocation xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="http://wherein.yahooapis.com/v1/schema" xml:lang="en"> <processingTime>0.001538</processingTime> ... (1 Reply)
Discussion started by: unclecameron
1 Replies

6. Shell Programming and Scripting

sed parse small xml file

I have a tmp.xml file like: <?xml version="1.0" encoding="UTF-8"?> <Response> <Ip>193.143.121.198</Ip> <Status>OK</Status> <CountryCode>PL</CountryCode> <CountryName>Poland</CountryName> <RegionCode>82</RegionCode> <RegionName>Pomorskie</RegionName> <City>Gdansk</City> ... (9 Replies)
Discussion started by: unclecameron
9 Replies

7. Emergency UNIX and Linux Support

How to parse the following xml file

Hi, I have the following file Example.xml <?xml version="1.0" encoding="iso-8859-1"?> <html><set label="09/07/29" value="1241.90"/> </html> Can any one help me in parsing this xml file I want to retrive the attribute values of the tag set Example I want to... (3 Replies)
Discussion started by: Raji_gadam
3 Replies

8. Shell Programming and Scripting

How can I parse xml file?

How can I parse file containing xml ? I am sure that its best to use perl - but my perl is not very good - can someone help? Example below contents of file containing the xml - I basically want to parse the file and have each field contained in a variable.. ie. I want to store the account... (14 Replies)
Discussion started by: frustrated1
14 Replies

9. Shell Programming and Scripting

Parse XML file

Hi, I need to parse the following XML data enclosed in <a> </a> XML tag using shell script. <X> ..... </X> <a> <b> <c>data1</c> <c>data2</c> </b> <d> <c>data3</c> </d> </a> <XX> ... </XX> (5 Replies)
Discussion started by: viki
5 Replies

10. Shell Programming and Scripting

How to parse a XML file using PERL and XML::DOm

I need to know the way. I have got parsing down some nodes. But I was unable to get the child node perfectly. If you have code please send it. It will be very useful for me. (0 Replies)
Discussion started by: girigopal
0 Replies
Login or Register to Ask a Question