xmlstarlet:如果子元素丢失,如何删除父元素

问题描述 投票:0回答:1

我有一些像这样的xml:“some.xml”:

<one>
  <two>
    <three> 
    <four>some text</four>
    </three>
  </two>
  <two>
    <three>
        <four>some other text</four>
    </three>
  </two>
  <two>
  </two>
</one>

我可以通过搜索文本来删除“两个”元素中的任何一个:

xmlstarlet ed -d "//one/two[contains(.,'some text')]" some.xml

这将删除前“两个”节点。但我想删除所有不包含任何“三个”元素的“两个”元素。像这样:

<one>
  <two>
    <three> 
    <four>some text</four>
    </three>
  </two>
  <two>
    <three>
        <four>some other text</four>
    </three>
  </two>
</one>
xml bash xmlstarlet
1个回答
0
投票

我想删除所有不包含任何“三个”元素的“两个”元素

如果我理解正确的话,那就是

xmlstarlet ed -d '//two[not(three)]'

<?xml version="1.0"?>
<one>
  <two>
    <three>
      <four>some text</four>
    </three>
  </two>
  <two>
    <three>
      <four>some other text</four>
    </three>
  </two>
</one>
© www.soinside.com 2019 - 2024. All rights reserved.