{"id":1296,"date":"2023-12-11T12:33:30","date_gmt":"2023-12-11T17:33:30","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=1296"},"modified":"2023-12-11T12:33:30","modified_gmt":"2023-12-11T17:33:30","slug":"using-external-user-python-functions-in-a-xml-or-xslt-by-using-saxonc-12-4-for-python-parser","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/using-external-user-python-functions-in-a-xml-or-xslt-by-using-saxonc-12-4-for-python-parser\/","title":{"rendered":"Using external User Python Functions in a XML or XSLT by using SaxonC 12.4 for Python parser"},"content":{"rendered":"\n<p>To use external Python functions in an XSLT transformation using the SaxonC 12.4 for Python parser, you&#8217;ll need to leverage Saxon&#8217;s extension functions mechanism. Here&#8217;s a general guide on how you can achieve this:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Install SaxonC 12.4 for Python:<\/h3>\n\n\n\n<p>Make sure you have SaxonC 12.4 installed for Python. You can follow the installation instructions provided by the SaxonC documentation or the installation guide specific to the package you are using.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Define the Python Function:<\/h3>\n\n\n\n<p>Create a Python module containing the function you want to use in your XSLT transformation. Save it as <code>external_functions.py<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># external_functions.py\r\n\r\ndef custom_function(parameter):\r\n    # Your Python logic here\r\n    return f\"Processed: {parameter}\"\r<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Write the XSLT File:<\/h3>\n\n\n\n<p>Create an XSLT file, e.g., <code>transform.xslt<\/code>, and reference the Python function using the extension function mechanism. Here, we&#8217;ll use the <code>saxon:script<\/code> element to call the Python function.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;!-- transform.xslt -->\r\n\r\n&lt;xsl:stylesheet xmlns:xsl=\"http:\/\/www.w3.org\/1999\/XSL\/Transform\"\r\n    xmlns:saxon=\"http:\/\/saxon.sf.net\/\"\r\n    extension-element-prefixes=\"saxon\"\r\n    version=\"2.0\">\r\n\r\n  &lt;xsl:output method=\"text\"\/>\r\n\r\n  &lt;xsl:template match=\"\/\">\r\n    &lt;xsl:value-of select=\"saxon:script('import external_functions; external_functions.custom_function', 'parameter')\"\/>\r\n  &lt;\/xsl:template>\r\n\r\n&lt;\/xsl:stylesheet>\r<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Perform the Transformation in Python:<\/h3>\n\n\n\n<p>Now, in your Python script, use the SaxonC library to perform the transformation. Make sure to import the <code>external_functions<\/code> module:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># transform.py\r\n\r\nfrom saxonc import PySaxonProcessor\r\n\r\n# Create a Saxon Processor\r\nprocessor = PySaxonProcessor(license=False)\r\n\r\n# Load the XSLT stylesheet\r\nxslt_file = \"transform.xslt\"\r\nxslt_compiled = processor.compile_stylesheet_file(xslt_file)\r\n\r\n# Set parameters if needed\r\nparameters = {\"parameter\": \"Input Data\"}\r\nxslt_compiled.set_initial_match_selection_parameters(parameters)\r\n\r\n# Apply the transformation\r\nresult = xslt_compiled.apply_templates_returning_string()\r\n\r\n# Print the result\r\nprint(result)\r<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Run the Python Script:<\/h3>\n\n\n\n<p>Execute your Python script:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>python transform.py<\/code><\/pre>\n\n\n\n<p>This should apply the XSLT transformation and call the external Python function, producing the desired result.<\/p>\n\n\n\n<p>Note: Ensure that the paths to the Python modules and XSLT files are correctly specified in your code. Adjust the code snippets according to your project structure and requirements.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To use external Python functions in an XSLT transformation using the SaxonC 12.4 for Python parser, you&#8217;ll need to leverage Saxon&#8217;s extension functions mechanism. Here&#8217;s a general guide on how you can achieve this: Step 1: Install SaxonC 12.4 for Python: Make sure you have SaxonC 12.4 installed for Python. You can follow the installation [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[203],"tags":[137,431,108,430],"class_list":["post-1296","post","type-post","status-publish","format-standard","hentry","category-python","tag-python","tag-saxonc","tag-xml","tag-xslt"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1296","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=1296"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1296\/revisions"}],"predecessor-version":[{"id":1297,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1296\/revisions\/1297"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=1296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=1296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=1296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}