在bash中格式化并组合xpath的输出

时间:2011-04-12 00:31:47

标签: xml bash xpath xml-parsing

我正在尝试使用bash实用程序xpath解析此xml输入:

<?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>

我需要这种格式的输出:

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

我试图以一种简单的方式(仅使用一个xpath命令,或者最多3-4个命令)来解决这个问题,但我所有的努力都是徒劳的。有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:6)

Bash版

#!/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版本(未经测试)......

#!/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++;
}