【问题标题】:Xmlstarlet conditional selectXmlstarlet 条件选择
【发布时间】:2017-01-30 14:15:36
【问题描述】:

当 id=1 时,我试图在我的 xml 中仅选择部分的名称

这是一个 xml 示例:

<Raiz>
    <section name="name1" id="1">
        <entry title="title1"/>
    </section>
    <section name="name2 id="2">
        <entry title="title2"/>
    </section>
    <section name="name3" id="3">
        <entry title="title3"/>
    </section>
</Raiz>

我想在 id=1 时选择节名

我正在尝试

xmlstarlet sel -t -v "/Raiz/section/@name" -i id=1 file.xml

xmlstarlet sel -t -v "/Raiz/section/@name" -i [@id=1] file.xml
xmlstarlet sel -t -v "/Raiz/section/@name" -i "id=1" file.xml
xmlstarlet sel -t -v "/Raiz/section/@name" -i @id=1 file.xml

但输出总是:

name1
name2
name3

我怎样才能只选择name1?

【问题讨论】:

    标签: xml parsing xpath xml-parsing


    【解决方案1】:

    您可以尝试使用 XPath 谓词表达式通过 id 属性值过滤 &lt;section&gt; 元素:

    xmlstarlet sel -t -v "/Raiz/section[@id=1]/@name" file.xml
    

    【讨论】:

      猜你喜欢
      • 2012-09-20
      • 1970-01-01
      • 2012-02-19
      • 2013-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-17
      • 2019-09-11
      相关资源
      最近更新 更多