Project

Profile

Help

base URI for stylesheet compilation from string?

Added by Martin Honnen 15 days ago

For the method compileFromString https://www.saxonica.com/saxon-c/doc12/html/classXslt30Processor.html#a84560f8f3afadaab61eeba9ac7e242f6

XsltExecutable * Xslt30Processor::compileFromString	(	
const char *	stylesheet,
const char *	encoding = nullptr )

how am I supposed to set the base URI to resolve xsl:imports/xsl:includes?


Replies (2)

RE: base URI for stylesheet compilation from string? - Added by Martin Honnen 15 days ago

Based on https://saxonica.plan.io/projects/saxonmirrorhe/repository/he/revisions/he_mirror_saxon_12_5/entry/src/main/java/net/sf/saxon/option/cpp/Xslt30Processor.java#L553, it looks as if the cwd (current working directory) is appended with / to form the base URI. Need to test what that does on Windows, though.

RE: base URI for stylesheet compilation from string? - Added by Martin Honnen 15 days ago

Have so far tested only with Python, but there it works surprisingly well:

from saxonche import *

xslt = '''
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0" expand-text="yes">

  <xsl:import href="import-test1.xsl"/>
  
  <xsl:mode on-no-match="shallow-copy"/>
  
  <xsl:template match="/">
    <xsl:copy>
      <xsl:apply-templates/>
      <xsl:comment>static base URI: {static-base-uri()}</xsl:comment>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
'''

xml = '''
<root>
  <foo>foo 1</foo>
  <foo>foo 2</foo>
  <bar>bar 1</bar>
  <bar>bar 2</bar>
</root>
'''

with PySaxonProcessor() as saxon_proc:
    print(saxon_proc.version)

    xslt30Processor = saxon_proc.new_xslt30_processor()

    xslt30Processor.set_cwd(".")

    xsltExecutable = xslt30Processor.compile_stylesheet(stylesheet_text=xslt)

    xdmNode = saxon_proc.parse_xml(xml_text=xml)

    xsltExecutable.set_initial_match_selection(xdm_value=xdmNode)

    xsltExecutable.set_global_context_item(xdm_item=xdmNode)

    result = xsltExecutable.apply_templates_returning_string()

    print(result)

Import is found as static base URI is correctly set:

SaxonC-HE 12.5 from Saxonica
<?xml version="1.0" encoding="UTF-8"?><root>
  <foo>foo 1</foo>
  <foo>foo 2</foo>
  <baz>bar 1</baz>
  <baz>bar 2</baz>
</root><!--static base URI: file:///C:/Users/marti/PycharmProjects/SaxonC12CompileXsltFromStringTest1/-->

Now do I trust this to work with the C++ API as well if it works with the Python API or do I need to write a test for C++?

    (1-2/2)

    Please register to reply