bash - 意外 token (带有xmlstarlet

标签 bash xpath xml-parsing xmlstarlet

我正在尝试使用xmlstarlet(使用xpath)解析XML文件,但是出现语法错误,并且我不知道如何更正我的代码。

这是我的脚本:

#!/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>


我想在作者的“ titre”标签之间打印文本(“ auteur value”之间的文本)是第二个参数。例如,如果输入./my_script author Knakis,我希望它返回:

Vos bureaux 


谢谢你的帮助 :)

最佳答案

我只有xmllint:

#!/bin/bash

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


输出:

 Vos bureaux

关于bash - 意外 token (带有xmlstarlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25591410/

相关文章:

java - 从 SOAP xml 解码,getSOAPBody() 方法返回 Null

c# - 这在 python 中相当于什么

linux - 比较 2 个文件时进行 Bash shell 脚本检查

bash - 在 Makefile 中提取操作系统名称和版本号

arrays - bash shell 数组输出范围到 csv

xpath - 如何通过被引用的属性查找元素?

java - 从 JavaFX 应用程序读取 XML

bash - 什么时候命令替换会产生比相同命令更多的子shell?

python - 用于 XPATH 查询的 AST

selenium - 机器人框架: passing argument to a keyword that returns an XPath