Project

Profile

Help

How to return an XDM map from XSLT to Python with XSLT and SaxonC 11.1?

Added by Martin Honnen about 2 years ago

After XPath and XQuery I now tried my luck to return an XDM map from XSLT to Python with SaxonC 11.1 but I fail, no error is given but the returned PyXdmValue has head as a PyXdmItem which doesn't seem to be any PyXdmMap as

from saxonc import *

with PySaxonProcessor(license=False) as proc:
    print("Test SaxonC on Python")
    print(proc.version)
    
    xslt30_processor = proc.new_xslt30_processor()
    
    xslt = '''<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="3.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="#all">

  <xsl:output method="adaptive" build-tree="no"/>

  <xsl:template match="/" name="xsl:initial-template">
    <xsl:sequence select="map { 'value' : 1, 'cat': 'test', 'check' : false() }"/>
  </xsl:template>

</xsl:stylesheet>    
    '''
    
    xslt30_executable = xslt30_processor.compile_stylesheet(stylesheet_text = xslt)
    
    xslt30_executable.set_result_as_raw_value(True)
    
    result = xslt30_executable.call_template_returning_value(template_name = '{http://www.w3.org/1999/XSL/Transform}initial-template')
    
    if result is None and xslt30_executable.exception_occurred:
        print(xslt30_executable.error_message)

    print(isinstance(result, PyXdmValue))
    
    print(result.size)
	
    result1 = result.head
	
    print(isinstance(result1, PyXdmItem))
    
    print(isinstance(result1, PyXdmAtomicValue))
    
    print(isinstance(result, PyXdmNode))

    print(isinstance(result, PyXdmFunctionItem))

    print(isinstance(result1, PyXdmMap))
        
    print(result1)

outputs

Test SaxonC on Python
SaxonC-HE 11.1 from Saxonica
True
1
True
False
False
False
False
Traceback (most recent call last):
  File "C:\Users\marti\OneDrive\Documents\xslt\blog-xslt-3-by-example\saxonc-11.1-python\arrays-and-maps\returnXdmMapFromXsltTest1.py", line 48, in <module>
    print(result1)
TypeError: __str__ returned non-string (type NoneType)

Is that not the right way to return a map?


Replies (2)

RE: How to return an XDM map from XSLT to Python with XSLT and SaxonC 11.1? - Added by O'Neil Delpratt about 2 years ago

The head property should automatically return the subclass PyXdmMap, but this is failing.

Also it would be useful to have a method on the PyXdmItem to access the Map object similar to the get_node_value() method.

    (1-2/2)

    Please register to reply