You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用xmllint选取XML中指定位置的子元素内容?

用xmllint选取指定位置的XML子元素

先看你提供的原始XML:

<parent>
   <child> test1 </child>
   <child> test2 </child>
</parent>

之前你用这条命令获取所有child元素的文本内容:

xmllint --format --xpath '//parent/child/text()' -

返回结果是:

test1 test2

要单独选取第一个或第二个child的内容,只需要在XPath表达式里给child加上位置索引就行——XPath的索引是从1开始的:

  • 选第一个child的文本,用[1]定位:

    xmllint --format --xpath '//parent/child[1]/text()' -
    

    执行后返回:

    test1
    
  • 选第二个child的文本,用[2]定位:

    xmllint --format --xpath '//parent/child[2]/text()' -
    

    执行后返回:

    test2
    

内容的提问来源于stack exchange,提问作者Sven van den Boogaart

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 02:22:02