Spr*_*dzy 7 xml bash xpath parsing
我想知道XPath是否可以返回一个值数组.
我喜欢这样的:
<Cities>
    <City>Paris</City>
    <City>Lyon</City>
    <City>Marseille</City>
</Cities>
而且我想得到一个这种形式的阵列['巴黎','里昂','马赛'].
我正在使用xpath UNIX实用程序.
任何的想法 ?
除了后处理输出:
xpath -q -e '//City/text()' inputfile
这是:
Paris
Lyon
Marseille
以多种方式,包括:
xpath -q -e '//City/text()' inputfile |
    awk 'BEGIN {sq = "\047"; OFS = sq "," sq}
         {a[$1] = NR}
         END {
             printf "[" sq; 
             for (i in a) {
                 printf "%s%s", d, i; d = OFS};
             print sq "]"
         }'
它给出了输出:
['Lyon','Marseille','Paris']
你可以使用我修改过的版本xpath.由于它是一个简单的Perl脚本,因此我对其进行了修改,以便您可以更好地控制输出的格式.使用我的版本,您可以:
xpath -q -b "[" -p "'" -i "," -s "'" -a "]"$'\n' -e '//City/text()'
获得所需的输出.这是用法消息:
Usage:
xpath [options] -e query [-e query...] [filename...]
        If no filenames are given, supply XML on STDIN.
        You must provide at least one query. Each supplementary
        query is done in order, the previous query giving the
        context of the next one.
        Options:
        -q quiet        Only output the resulting PATH
        -b before       use before instead of nothing.
        -p prefix       use prefix instead of nothing.
        -s suffix       use suffix instead of linefeed.
        -i infix        use infix instead of nothing.
        -a after        use after instead of nothing.
你可以在这里下载我的版本.
| 归档时间: | 
 | 
| 查看次数: | 21523 次 | 
| 最近记录: |