[英]Find and Replace and attribute in XML file using libxml/xpath in C program
I have a xml file which has different sections and i need to 'rename' certain attributes appearing in this huge file (like find and replace with new name).我有一个 xml 文件,它有不同的部分,我需要“重命名”这个巨大文件中出现的某些属性(比如查找并替换为新名称)。 All over i have used libxml and xpath to modify and satisfy other requirements, but for this one, i am not sure how this can done using libxml and xpath APIs.我一直使用 libxml 和 xpath 来修改和满足其他要求,但是对于这个,我不确定如何使用 libxml 和 xpath API 来完成。 Please help.请帮忙。
eg In the below file i need to find all 'ORANGE' string and replace it with say 'PURPLE' string.例如,在下面的文件中,我需要找到所有“ORANGE”字符串并将其替换为“PURPLE”字符串。 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>
<your_section color="ORANGE" name="blah"> </your_section>
<another_section city="LONDON" blah="ORANGE"> </another_section>
<any_section code="RED" age="ORANGE"> </any_section>
</a>
I know XPath and how complicated it can be... but, maybe the solution to this problem is just a simple string replace?我知道 XPath 以及它有多复杂......但是,也许这个问题的解决方案只是一个简单的字符串替换? But maybe there are scenarios that I am not thinking of that you are not showing here this does not match for.但也许有些场景我没有想到你没有在这里展示这不匹配。
But if you really want to do it through xpath但是如果你真的想通过 xpath
//*[@*='ORANGE']
Selects all elements that contain an attribute with value orange选择包含值为橙色的属性的所有元素
//*[@*='ORANGE']/@*[string()='ORANGE']
Gets you only the attributes只获取属性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.