Sponsored Content
Top Forums Shell Programming and Scripting xmlstarlet parse field from file Post 302423336 by unclecameron on Thursday 20th of May 2010 07:47:51 PM
Old 05-20-2010
xmlstarlet parse field from file

I have a xmlfile like this:
Code:
<?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>
    <version> build 091119</version>
    <documentLength>101</documentLength>
    <document>
        <administrativeScope>
            <woeId>2430456</woeId>
            <type>Town</type>
            <name><![CDATA[Kailua Kona, HI, US]]></name>
            <centroid>
                <latitude>19.6401</latitude>
                <longitude>-155.996</longitude>
            </centroid>
        </administrativeScope>
        <geographicScope>
            <woeId>12798285</woeId>
            <type>Zip</type>
            <name><![CDATA[96740, Kailua Kona, HI, US]]></name>
            <centroid>
                <latitude>19.7118</latitude>
                <longitude>-155.892</longitude>
            </centroid>
        </geographicScope> 
            <extents>
                <center>
                    <latitude>19.7</latitude>
                    <longitude>-155.9</longitude>
                       </center>
                <southWest>
                    <latitude>19.5288</latitude>
                    <longitude>-156.062</longitude>
                       </southWest>
                     <northEast>
                    <latitude>19.8984</latitude>
                    <longitude>-155.725</longitude>
                       </northEast>
            </extents>
               <placeDetails>
            <place>
                <woeId>12798285</woeId>
                <type>Zip</type>
                     <name><![CDATA[96740, Kailua Kona, HI, US]]></name>
                <centroid>
                    <latitude>19.7</latitude>
                    <longitude>-155.9</longitude>
                </centroid>
            </place>
                       <matchType>1</matchType>
                <weight>1</weight>
                <confidence>10</confidence>
               </placeDetails>
        <referenceList>
            <reference>
                <woeIds>12798285</woeIds>
                <start>-1</start>
                <end>-1</end>
                <isPlaintextMarker>0</isPlaintextMarker>
                <text><![CDATA[]]></text>
                <type>xpath</type>
                <xpath><![CDATA[/div[1]/span[2]]]></xpath>
            </reference>
        </referenceList>
    </document>
</contentlocation>

which is valid xml, according to xmlstarlet val xmfile, I'm trying to run a query like this:
Code:
xmlstarlet sel -t -m "//geographicScope" -v "name" -n xmlfile

but it's returning nothing, what am I doing wrong? I try a similar query on another type of xml file I have on this machine and it returns the right results.

---------- Post updated at 04:47 PM ---------- Previous update was at 02:35 PM ----------

got it working by piping to xml2, definitely not the best way I guess, but this works:
Code:
url=`curl --silent -X POST -ddocumentContent='<div class="geo">GEO: \
                <span class="latitude">'${lat}'</span>, <span class="longitude">'${lon}'</span></div>' \
                -ddocumentType='text/html' -dappid='myidat@yahoo.com' http://wherein.yahooapis.com/v1/document`
address=`echo $url | xml2 | grep contentlocation | grep document | grep geographicScope | grep name | cut -c48-`

had to get something working quick.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to parse a text file with \034 as field and \035 as end of message delimiter?

I need some tips to write a unix korn shell script that will parse an input text file. Input text file has messages that span several lines, each field in the message is delimited by /034 and the end of message is delimited by /035. Input file looks something similar to ... (1 Reply)
Discussion started by: indianya
1 Replies

2. Shell Programming and Scripting

xmlstarlet template parse small xml file

I have a file like: <?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>... (4 Replies)
Discussion started by: unclecameron
4 Replies

3. Shell Programming and Scripting

Using AWK to parse a delimited field

Hi everyone! How can I parse a delimited field using AWK? For example, if I have lastName#firstName or lastName*firstName. I'd like an AWK script that would return lastName and then another that would return firstName? Is this possible? (13 Replies)
Discussion started by: Fatbob
13 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

how to parse with awk (using different fields), then group by a field?

When parsing multiple fields in a file using AWK, how do you group by one of the fields and parse by delimiters? to clarify If a file had tom | 223-2222-4444 , randofield ivan | 123-2422-4444 , random filed ... | and , are the delimiters ... How would you group by the social security... (4 Replies)
Discussion started by: Josef_Stalin
4 Replies

6. Shell Programming and Scripting

awk : Filter a set of data to parse header line and last field of multiple same match.

Hi Experts, I have a data with multiple entry , I want to filter PKG= & the last column "00060110" or "00088150" in the output file: ############################################################################################### PKG= P8SDB :: VGS = vgP8SOra vgP8SDB1 vgP8S001... (5 Replies)
Discussion started by: rveri
5 Replies

7. Shell Programming and Scripting

awk Parse And Create Multiple Files Based on Field Value

Hello: I am working parsing a large input file which will be broken down into multiples based on the second field in the file, in this case: STORE. The idea is to create each file with the corresponding store number, for example: Report_$STORENUM_$DATETIMESTAMP , and obtaining the... (7 Replies)
Discussion started by: ec012
7 Replies

8. Shell Programming and Scripting

awk to parse field and include the text of 1 pipe in field 4

I am trying to parse the input in awk to include the |gc= in $4 but am not able to. The below is close: awk so far: awk '{sub(/\|]+]++/, ""); print }' input.txt Input chr1 955543 955763 AGRN-6|pr=2|gc=75 0 + chr1 957571 957852 AGRN-7|pr=3|gc=61.2 0 + chr1 970621 ... (7 Replies)
Discussion started by: cmccabe
7 Replies

9. Shell Programming and Scripting

Use xmlstarlet inside an if loop

I have a XML file of little huge size. I have to build a logic to get the count of the tag <capacity>. And have an if loop such that all the <capacity> blocks are captured one after the other. sample input file - sample1.xml <subcolumns><capacity><name>45.90</name> <index>0</index>... (1 Reply)
Discussion started by: ramprabhum
1 Replies

10. 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
All times are GMT -4. The time now is 07:18 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy