# Deleting an element in namespace xml based on value

**URL:** <https://forum.ansible.com/t/deleting-an-element-in-namespace-xml-based-on-value/35746>\
**Category:** Ansible Project\
**Created:** [September 16, 2021, 12:38pm UTC](https://forum.ansible.com/t/deleting-an-element-in-namespace-xml-based-on-value/35746 "2021-09-16T12:38:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hasini\_P](https://avatars.discourse-cdn.com/v4/letter/h/b9bd4f/32.png) [@Hasini\_P](https://forum.ansible.com/u/Hasini_P)\
**Post date:** [September 16, 2021, 12:38pm UTC](https://forum.ansible.com/t/deleting-an-element-in-namespace-xml-based-on-value/35746/1 "2021-09-16T12:38:26Z")

</div>

Hi, I am trying to delete an element in a namespace xml based on element value ; without namespace, deletion is working fine (as posted in ansible documentation of xml module)… Can someone help, please

Example 1: xml with no namespace

_Tasty Hot Coffee_  
_\<key\_name\>_  
_cold brew_  
_cappuccino_  
_mochai_  
_\</key\_name\>_  
_10_  
  
_[http://tastyhotcoffee.com](http://tastyhotcoffee.com)_

---------- It worked ------------  
**ansible localhost -m xml -a “path=xml\_example.xml xpath=‘/business/key\_name/type[text()="mocha"]’ state=absent”**  
localhost | CHANGED =\> {  
“actions”: {  
“namespaces”: {},  
“state”: “absent”,  
“xpath”: “/business/key\_name/type[text()="mocha"]”  
},  
“changed”: true  
}  
---------- It worked ------------

Example 1: xml with namespace  
  
_Tasty Hot Coffee_  
_\<key\_name\>_  
_cold brew_  
_cappuccino_  
_mochai_  
_\</key\_name\>_  
_10_  
  
_[http://tastyhotcoffee.com](http://tastyhotcoffee.com)_  
  
  
---------- It did not work------------  
**ansible localhost -m xml -a “path=xml\_example.xml xpath=‘x:/business/x:key\_name/x:type[text()="mochai"]’ namespaces=‘x="[https://coffee.com](https://coffee.com)"’”**  
localhost | FAILED! =\> {  
“changed”: false,  
“msg”: “Syntax error in xpath expression: x:/business/x:key\_name/x:type[text()="mochai"] (Invalid expression)”  
}

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [September 18, 2021, 11:55am UTC](https://forum.ansible.com/t/deleting-an-element-in-namespace-xml-based-on-value/35746/2 "2021-09-18T11:55:27Z")

</div>

As the docs say the namespaces need to be supplied as a dict.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [September 18, 2021, 3:55pm UTC](https://forum.ansible.com/t/deleting-an-element-in-namespace-xml-based-on-value/35746/3 "2021-09-18T15:55:57Z")

</div>

Something like this:

ansible localhost -m xml -a 'path=xml\_example.xml  
xpath=/x:business/x:key\_name/x:type[text()="mochai"] state=absent  
namespaces={"x":"[https://coffee.com](https://coffee.com)"}'

---

<div class="post-metadata">

**Author:** ![Hasini\_P](https://avatars.discourse-cdn.com/v4/letter/h/b9bd4f/32.png) [@Hasini\_P](https://forum.ansible.com/u/Hasini_P)\
**Post date:** [September 20, 2021, 11:10am UTC](https://forum.ansible.com/t/deleting-an-element-in-namespace-xml-based-on-value/35746/4 "2021-09-20T11:10:32Z")

</div>

Thanks Dick, It worked
