How to remove Children from Xpath Expression Nodes in Python

Cloudmersive
1 min readMay 10, 2020

Trying to work with XPath expression nodes in Python can be such a colossal drag. But not any more! I will show you how to trivialize this endeavor by use of an API.

Pip install the document and conversion API client from Cloudmersive.

pip install cloudmersive-convert-api-client

And now convert_data_xml_edit_remove_all_child_nodes_with_x_path can be called by way of a newly created API instance:

from __future__ import print_functionimport timeimport cloudmersive_convert_api_clientfrom cloudmersive_convert_api_client.rest import ApiExceptionfrom pprint import pprint# Configure API key authorization: Apikeyconfiguration = cloudmersive_convert_api_client.Configuration()configuration.api_key['Apikey'] = 'YOUR_API_KEY'# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed# configuration.api_key_prefix['Apikey'] = 'Bearer'# create an instance of the API classapi_instance = cloudmersive_convert_api_client.ConvertDataApi(cloudmersive_convert_api_client.ApiClient(configuration))input_file = '/path/to/file' # file | Input XML file to perform the operation on.x_path_expression = 'x_path_expression_example' # str | Valid XML XPath query expressiontry:# Removes, deletes all children of nodes matching XPath expression, but does not remove the nodesapi_response = api_instance.convert_data_xml_edit_remove_all_child_nodes_with_x_path(input_file, x_path_expression)pprint(api_response)except ApiException as e:print("Exception when calling ConvertDataApi->convert_data_xml_edit_remove_all_child_nodes_with_x_path: %s\n" % e)

Now input your file location and XPath expression. And the rest is gravy!

Been staring at failed code all day? Let’s try something else.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.