简体   繁体   English

在 XML 中查找和删除节点,使用 libxml2 和 xpath

[英]Find and Delete a Node in XML, using libxml2 and xpath

I have a big XML file which has multiple nodes like below;我有一个大的 XML 文件,它有多个节点,如下所示;

<my_section enum="ORANGE" name="blah"> </my_section> <my_section enum="APPLE" name="blah blah" > </my_section> <my_section enum="ORANGE" name="blah"> </my_section> <my_section enum="APPLE" name="blah blah" > </my_section>

etc.. ETC..

I need to look for a specific 'enum' type like APPLE and delete that my_section node for APPLE.我需要寻找一个特定的“枚举”类型,如 APPLE,并为 APPLE 删除 my_section 节点。

I tried using Xpath, but i am not sure what should be the eval string here.我尝试使用 Xpath,但我不确定这里的 eval 字符串应该是什么。 If I just give my_section, then all the sections will come.如果我只给 my_section,那么所有的部分都会来。 I need to match on the enum though.我需要匹配枚举。 Please help.请帮忙。

<a>
    <my_section enum="ORANGE" name="blah"> </my_section> 
    <my_section enum="APPLE" name="blah blah"> </my_section>
    <my_section enum="ORANGE1" name="blah1"> </my_section> 
</a>

Using XPATH, you may be able filter out the nodes needed.使用 XPATH,您可以过滤掉所需的节点。 I have added a root element just to test.我添加了一个根元素只是为了测试。 Change as needed.根据需要进行更改。

/a/my_section except /a/my_section[@enum = "APPLE"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM