意外标记(与xmlstarlet

问题描述:

我试图解析与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

我只有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