I am trying to edit an XML file using xmlStarlet, so I have to specify the element I want to delete in XPath, and I am having problems with the XPath specification. The XML file will contain the following:
我正在尝试使用xmlStarlet编辑XML文件,因此我必须在XPath中指定要删除的元素,并且我遇到了XPath规范的问题。 XML文件将包含以下内容:
<SoundStreamBlock>
<data>base64 characters</data>
</SoundStreamBlock>
<ShowFrame/>
The problem I have is that, after some previous deletions, I am left with 2 of the ShowFrame elements, one after another, that is, 2 adjacent elements:
我遇到的问题是,在一些先前的删除之后,我留下了两个ShowFrame元素,一个接一个,即2个相邻的元素:
<SoundStreamBlock>
<data>base64 characters</data>
</SoundStreamBlock>
<ShowFrame/>
<ShowFrame/>
I want to delete the duplicate ShowFrame element. Using the following XPath specification in xmlStarlet is not working:
我想删除重复的ShowFrame元素。在xmlStarlet中使用以下XPath规范不起作用:
"/swf/Header/tags/ShowFrame/preceding::ShowFrame"
Edit: I have also tried the above with preceding-sibling
编辑:我也尝试了上面的兄弟
This deletes all of the ShowFrame elements except the last one. I only want to delete the single duplicate ShowFrame element, that is, only when there is a ShowFrame element immediately following a ShowFrame element.
这会删除除最后一个之外的所有ShowFrame元素。我只想删除单个重复的ShowFrame元素,也就是说,只有在ShowFrame元素后紧跟ShowFrame元素时才会这样。
What do I need to add to the XPath spec to restrict it for this adjacent element?
我需要添加到XPath规范中以限制它对于这个相邻元素?
Thanks,
-Andres
1 个解决方案
#1
You can try this way :
你可以这样试试:
/swf/Header/tags/ShowFrame/preceding-sibling::*[1][self::ShowFrame]
The last bit of the above xpath gets the first preceding sibling element (/preceding-sibling::*[1]
) and tests if the element name is ShowFrame
([self::ShowFrame]
).
上面的xpath的最后一位获取第一个前面的兄弟元素(/ preceding-sibling :: * [1])并测试元素名称是否为ShowFrame([self :: ShowFrame])。
#1
You can try this way :
你可以这样试试:
/swf/Header/tags/ShowFrame/preceding-sibling::*[1][self::ShowFrame]
The last bit of the above xpath gets the first preceding sibling element (/preceding-sibling::*[1]
) and tests if the element name is ShowFrame
([self::ShowFrame]
).
上面的xpath的最后一位获取第一个前面的兄弟元素(/ preceding-sibling :: * [1])并测试元素名称是否为ShowFrame([self :: ShowFrame])。