0

I'm trying to parse this xml input using the bash utility xpath:

<?xml version="1.0" encoding="UTF-8"?>
<feed version="0.3" xmlns="http://purl.org/atom/ns#">
    <entry>
        <title>Title 1</title>
        <author>Author 1</author>
    </entry>
    <entry>
        <title>Title 2</title>
        <author>Author 2</author>
    </entry>
</feed>

I need an output in this format:

1. Title: Title 1
   Author: Author 1
2. Title: Title 2
   Author: Author 2

I've fiddled around a lot trying to achieve this in a simple way (using only a single xpath command, or at most 3-4 commands), but all my efforts have been in vain. Could anyone please help me out with this?

Vicky Chijwani
  • 10,191
  • 6
  • 56
  • 79
  • XPath **selects** nodes from an XML tree instance. **It doesn't serialize**. –  Apr 12 '11 at 22:09

1 Answers1

6

Bash version

#!/bin/bash
count=1
input=input.xml

while [ -n "$title" -o $count = 1 ]
do
    title=`cat $input | xpath //entry[$count]/title 2>/dev/null | sed s/\<title\>//g| sed s/\<\\\\/title\>//g`
    author=`cat $input | xpath //entry[$count]/author 2>/dev/null | sed s/\<author\>//g| sed s/\<\\\\/author\>//g`
    if [ "$title" -a "$author" ]; then
        echo $count $title $author
    fi
    count=$((count+1))
done

Perl version (untested) ...

#!/usr/bin/perl
use XML::XPath;

my $file = 'input.xml';
my $xp = XML::XPath->new(filename => $file);
my $count = 1;
foreach my $entry ($xp->find('//entry')->get_nodelist){
    print $count;
    print 'Title:' . $entry->find('title')->string_value;
    print 'Author: ' . $entry->find('author');
    $count++;
}
qwerty
  • 3,801
  • 2
  • 28
  • 43
  • I'm making a shell script and have no knowledge of Perl. I need to know how to solve the problem using the `xpath` command in bash. – Vicky Chijwani Apr 12 '11 at 05:30
  • I figured out a (similar) solution to the problem. Thanks :) – Vicky Chijwani Apr 12 '11 at 13:17
  • 2
    @vicky if this answer is close to what you figured out - you should consider accepting @qwerty's answer to acknowledge the effort they put in (even if you did not adopt their answer as-is) – nhed Apr 12 '11 at 15:48