2014-08-31 64 views
1

我試圖解析與xmlstarlet一個XML文件(使用XPath),但我得到一個語法錯誤,我不知道怎麼糾正我的代碼意外標記(與xmlstarlet

這是我的腳本:

#!/bin/bash 

if [ $1=="author" ]; then 
    xmlstarlet sel -t -v //topic/auteur[text()=$2]/../titre < ~/.jvc/topics.xml 
fi 

這是XML文件:

<liste_topics> 
    <topic> 
    <icone>topic_marque_on</icone> 
    <auteur>Knakis</auteur> 
    <titre>Vos bureaux</titre> 
    <auteur_en_rouge>0</auteur_en_rouge> 
    <nb_reponses>1036</nb_reponses> 
    <lien_topic>jv://forums/1-38-7790682-1-0-1-0-0.xml</lien_topic> 
    <date_heure>29/08/2014 - 12h56</date_heure> 
    </topic> 
    <topic> 
    <icone>topic_marque_on</icone> 
    <auteur>ShadowwF</auteur> 
    <titre>[PROJET] Le wiki du forum v2</titre> 
    <auteur_en_rouge>0</auteur_en_rouge> 
    <nb_reponses>198</nb_reponses> 
    <lien_topic>jv://forums/1-38-7796926-1-0-1-0-0.xml</lien_topic> 
    <date_heure>17/08/2014 - 15h16</date_heure> 
    </topic> 
    <topic> 
    <icone>topic_marque_off</icone> 
    <auteur>Google_Bot</auteur> 
    <titre>[À lire] Répondre aux trolls = kick</titre>  
    <nb_reponses>0</nb_reponses> 
    <lien_topic>jv://forums/1-38-7818336-1-0-1-0-0.xml</lien_topic> 
    <date_heure>14/08/2014 - 03h33</date_heure> 
    </topic> 
</liste_topics> 

我要打印的「效價」標籤之間的文本,其作者(「導演值」之間的文本)是第二個參數。例如。 ,如果我輸入./my_script author Knakis,我想它返回:

Vos bureaux 

感謝您的幫助:)

+1

的元素用空格在'[]'中:'if [$ 1 ==「author」]; then'。 – fedorqui 2014-08-31 11:24:18

+0

它應該做什麼?我做到了,但沒有任何改變 – nounoursheureux 2014-08-31 11:32:07

回答

2

我只有xmllint:

#!/bin/bash 

if [ "$1" == "author" ]; then 
    xmllint --xpath "//liste_topics/topic[auteur=\"$2\"]/titre/text()" file.xml 
fi 

輸出:

Vos bureaux 
+0

謝謝,那就是我一直在尋找的東西:D – nounoursheureux 2014-08-31 11:43:22