【问题标题】:Unexpected token ( with xmlstarlet意外的令牌(带有 xmlstarlet
【发布时间】:2014-10-24 19:56:39
【问题描述】:

我正在尝试使用 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>

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

Vos bureaux 

感谢您的帮助:)

【问题讨论】:

  • [ ] 中的元素使用空格:if [ $1 == "author" ]; then
  • 它应该做什么?我做到了,但没有任何改变

标签: bash xpath xml-parsing xmlstarlet


【解决方案1】:

我只有 xmllint:

#!/bin/bash

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

输出:

 Vos bureaux

【讨论】:

  • 谢谢,这就是我要找的:D
猜你喜欢
  • 2017-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-02
相关资源
最近更新 更多