Project

Profile

Help

Is there any way to set a static base URI for https://www.saxonica.com/saxon-c/doc/html/saxonc.html#PyXslt30Processor-compile_stylesheet when passing in the XSLT code as a string?

Added by Martin Honnen 8 months ago

The various APIs to XSLT 3 from Python in Saxon-C 1.2.1 allow you to pass in the XSLT code as a Python string, e.g. https://www.saxonica.com/saxon-c/doc/html/saxonc.html#PyXslt30Processor-compile_stylesheet has stylesheet_text as a possible keyword argument.

However, when doing that, I find no way to set a static base URI so that for instance relative URIs in calls to the doc or collection or uri-collection function can be resolved. The Java API I think for the compile method takes a Java Source where you can provide a base URI, the .NET API has a BaseUri property on the XsltCompiler.

Is there any similar way in the Python API?


Replies (5)

Please register to reply

RE: Is there any way to set a static base URI for https://www.saxonica.com/saxon-c/doc/html/saxonc.html#PyXslt30Processor-compile_stylesheet when passing in the XSLT code as a string? - Added by O'Neil Delpratt 8 months ago

Unfortunately currently not possible. The bug issue #4395 is still open to add the base URI method in the C++/PHP/Python APIs. I have added the method on the Saxon/C Java side, but this change requires a maintenance release.

RE: Is there any way to set a static base URI for https://www.saxonica.com/saxon-c/doc/html/saxonc.html#PyXslt30Processor-compile_stylesheet when passing in the XSLT code as a string? - Added by Martin Honnen 8 months ago

But that bug is about the base output URI used for resolving relative URIs in xsl:result-document. This question is about the static base URI that would be used to resolve relative URIs in calls to doc(), collection() and uri-collection().

If I am not mistaken on the .NET and Java side there are two different methods/setters for these two distinct type of URIs.

RE: Is there any way to set a static base URI for https://www.saxonica.com/saxon-c/doc/html/saxonc.html#PyXslt30Processor-compile_stylesheet when passing in the XSLT code as a string? - Added by O'Neil Delpratt 8 months ago

Ah yes you are right. This method is missing. I will create bug issue to keep track of this issue.

As a workaround you should be able to pass the base URI via the set_cwd method on the PyXsltProcessor or PyXslt30Processor before you call the compile_stylesheet function.

Bug issue added: #4411

RE: Is there any way to set a static base URI for https://www.saxonica.com/saxon-c/doc/html/saxonc.html#PyXslt30Processor-compile_stylesheet when passing in the XSLT code as a string? - Added by Martin Honnen 8 months ago

I can't seem to get the workaround to use set_cwd working either:

Here is a simple example:

import saxonc
import os

with saxonc.PySaxonProcessor() as proc:
    print(proc.version)
    
    xslt30_processor = proc.new_xslt30_processor()
    
    xslt30_processor.set_cwd(os.getcwd())
    
    xslt30_code = '''<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        exclude-result-prefixes="#all"
        version="3.0">
        
  <xsl:param name="pattern" as="xs:string">?select=sample-*.xml</xsl:param>
  
  <xsl:output method="text" item-separator="&#10;"/>

  <xsl:template match="/" name="xsl:initial-template">
     <xsl:value-of select="$pattern"/>
     <xsl:value-of select="static-base-uri()"/>
     <xsl:value-of select="doc-available('sample-10.xml')"/>
     <!-- <xsl:variable name="input-uris" select="uri-collection($pattern)"/>
     <xsl:value-of select="$input-uris"/> -->
  </xsl:template>
  
</xsl:stylesheet> 
    '''
    
    xslt30_processor.compile_stylesheet(stylesheet_text = xslt30_code)
        
    result = xslt30_processor.call_template_returning_string()
    
    print(result)

This runs without error but only outputs

Saxon/C 1.2.1 running with Saxon-HE 9.9.1.5C from Saxonica
?select=sample-*.xml

true

meaning no static base URI has been set by the set_cwd call.

If remove the comment to try to use uri-collection I get an error:

Saxon/C 1.2.1 running with Saxon-HE 9.9.1.5C from Saxonica
Error evaluating (fn:uri-collection(...)) in xsl:variable/@select on line 15 column 73
  XTDE1162: Relative URI passed to document() function (); but no base URI is available
at template xsl:initial-template on line 11
     invoked by unknown caller (null)
None

Complete code that produces that error:

import saxonc
import os

with saxonc.PySaxonProcessor() as proc:
    print(proc.version)
    
    xslt30_processor = proc.new_xslt30_processor()
    
    xslt30_processor.set_cwd(os.getcwd())
    
    xslt30_code = '''<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        exclude-result-prefixes="#all"
        version="3.0">
        
  <xsl:param name="pattern" as="xs:string">?select=sample-*.xml</xsl:param>
  
  <xsl:output method="text" item-separator="&#10;"/>

  <xsl:template match="/" name="xsl:initial-template">
     <xsl:value-of select="$pattern"/>
     <xsl:value-of select="static-base-uri()"/>
     <xsl:value-of select="doc-available('sample-10.xml')"/>
     <xsl:variable name="input-uris" select="uri-collection($pattern)"/>
     <xsl:value-of select="$input-uris"/>
  </xsl:template>
  
</xsl:stylesheet> 
    '''
    
    xslt30_processor.compile_stylesheet(stylesheet_text = xslt30_code)
        
    result = xslt30_processor.call_template_returning_string()
    
    print(result)

Is there anything missing in that attempt to use set_cwd?

RE: Is there any way to set a static base URI for https://www.saxonica.com/saxon-c/doc/html/saxonc.html#PyXslt30Processor-compile_stylesheet when passing in the XSLT code as a string? - Added by O'Neil Delpratt 8 months ago

Sorry,

Looking back at the code on the java I see the problem with the workaround. The code has been commented out. A fix for this and a new set base URI function will be available in the next maintenance release, which I will be getting out soon.

    (1-5/5)

    Please register to reply