How to transform an XML Document with XSLT into a new XML Document in Python

Cloudmersive
2 min readJun 9, 2020

--

I can think of so many things I’d rather be doing than dealing with the dull coding required to set up XSLT features in Python. That is why I am going to be skipping the details and showing you the shortcut!

Pip install will get us our client:

pip install cloudmersive-convert-api-client

Our transformation will make use of the convert_data_xml_transform_with_xslt_to_xml function from that client we just installed.

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.transform_file = '/path/to/file' # file | Input XSLT file to use to transform the input XML file.try:# Transform XML document file with XSLT into a new XML documentapi_response = api_instance.convert_data_xml_transform_with_xslt_to_xml(input_file, transform_file)pprint(api_response)except ApiException as e:print("Exception when calling ConvertDataApi->convert_data_xml_transform_with_xslt_to_xml: %s\n" % e)

Alright, you officially have your solution. It’s really just that easy. If you want more useful functions in this same vein, the remainder of this library is loaded with great features that are just as simple to plug into your project.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet