【问题标题】:Remove attribute & matched node at once一次删除属性和匹配的节点
【发布时间】:2021-11-23 12:39:42
【问题描述】:

我尝试从 xml 中删除 attributechild node 一次,例如:

<xml>
  <node attribute="name">
    <child>name</child>
  </node>
</xml>

达到预期的结果

<xml>
  <node>
  </node>
</xml>

取决于子价值

有可能吗?

【问题讨论】:

    标签: xml xpath command-line xml-parsing xmlstarlet


    【解决方案1】:

    你可能可以;尝试使用

    xml ed -d '//node[child["name"]]/@attribute'  -d  '//node/child["name"]' file.xml
    

    第一部分删除(-dnode 节点的attribute 属性,该节点本身具有child 子节点和name 文本值;第二个-d 删除“子”节点本身。

    【讨论】:

    • 匹配无效。不仅“名称”包含,而且所有“子”节点都被删除了。
    【解决方案2】:

    受 Jack 的启发,这是可行的解决方案:

    xml ed -d  '//node[child="name"]/@attribute' -d  '//node/child[contains(text(),"name")]' file.xml
    

    【讨论】:

      猜你喜欢
      • 2018-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-11
      相关资源
      最近更新 更多