I have an xml looking like this:
<first id="1">
<second id="1">test</second>
<second>
<third id="4">test</third>
</second>
<second id="3">test</second>
</first>
and I would like to remove the "id" attribute but only in the "first" element. Using XML::TWIG, i did this:
$twig->parsefile('test.xml');
my ($model) = $twig->first_elt('first[@id]');
$model->strip_att('id');
The problem is, it removes the "id" attributes in all the element, not just "first", so the final file looks like this:
<first>
<second>test</second>
<second>
<third>test</third>
</second>
<second>test</second>
</first>
instead of this:
<first>
<second id="1">test</second>
<second>
<third id="4">test</third>
</second>
<second id="3">test</second>
</first>
Any suggestions?
I think that you must use del_att($att) instead of strip_att($att), on documentation you can see this explanation:
strip_att ($att) :
Remove the attribute $att from all descendants of the element (including the element)
del_att ($att):
Delete the attribute for the element.
So in your code:
$twig->parsefile('test.xml');
my ($model) = $twig->first_elt('first[@id]');
$model->del_att('id');
Hope this helps