Project

Profile

Help

Feature #5848

closed

XQuery that doesn't compile sets exception_occurred but in some cases doesn't set the error_message, it is None

Added by Martin Honnen over 1 year ago. Updated 6 months ago.

Status:
Closed
Priority:
Low
Category:
Python API
Start date:
2023-01-23
Due date:
% Done:

100%

Estimated time:
Found in version:
12.0
Fixed in version:
12.1
Platforms:

Description

The following Python code might seem weird as it passes XSLT code to the XPath and XQuery processor but in the end the bug is about the sequence of running the code first through the XPath processor and then through the XQuery processor somehow SaxonC HE 12.0 doesn't manage to set the error_message of the XQuery processor, it is None.

from saxonche import *

def xpath(saxon_proc, xpath_code, input_xml):
    xpath_processor = saxon_proc.new_xpath_processor()

    xdm_input = saxon_proc.parse_xml(xml_text=input_xml)
    xpath_processor.set_context(xdm_item=xdm_input)

    xdm_result = xpath_processor.evaluate(xpath_code)

    if not(xpath_processor.exception_occurred):
        result_items = []
        for xdm_item in xdm_result:
            result_items.append(str(xdm_item))
        result_python_map = { 'results': result_items }
    else:
        result_python_map = { 'messages': xpath_processor.error_message, 'results': None }

    return result_python_map

def xquery(saxon_proc, xquery_code, input_xml):

    xquery_processor = saxon_proc.new_xquery_processor()

    xquery_processor.set_query_content(xquery_code)

    if xquery_processor.exception_occurred:
        return { 'messages' : xquery_processor.error_message }

    xdm_input = saxon_proc.parse_xml(xml_text=input_xml)
    xquery_processor.set_context(xdm_item=xdm_input)

    serialized_result = xquery_processor.run_query_to_string()

    if not(xquery_processor.exception_occurred):
        result_python_map = { 'results': [serialized_result] }
    else:
        result_python_map = { 'messages': xquery_processor.error_message, 'results': None }

    return result_python_map

with PySaxonProcessor(license=True) as proc:
    print(proc.version)

    xml = '''<?xml version="1.0" encoding="UTF-8"?>
    <root>
      <foo>test</foo>
    </root>'''

    xslt = '''<?xml version="1.0" encoding="utf-8"?>
<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"
  expand-text="yes">

  <xsl:output method="html" indent="yes" html-version="5"/>

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:template match="/" name="xsl:initial-template">
    <xsl:next-match/>
    <xsl:comment>Run with {system-property('xsl:product-name')} {system-property('xsl:product-version')} at {current-dateTime()}</xsl:comment>
  </xsl:template>

</xsl:stylesheet>'''

    result = xpath(proc, xslt, xml)

    print(result['messages'])

    result = xquery(proc, xslt, xml)

    print(result['messages'])

Output for me is

SaxonC-HE 12.0 from Saxonica
Unexpected token "<" at start of expression
None

so the code doing the XQuery execution notices that the code passed in is not XQuery and gives exception_occurred but somehow doesn't manage to populate the error_message.

This happens with the above sequence, if I comment out or take out the lines doing XPath (e.g.

    #result = xpath(proc, xslt, xml)

    #print(result['messages'])

then I get (from the XQuery evaluation only) the proper error message:

SaxonC-HE 12.0 from Saxonica
A processing instruction must not be named 'xml' in any combination of upper and lower case
Static error near {...rsion="1.0" encoding="utf-8...} 
  XPST0003  A processing instruction must not be named 'xml' in any combination of upper and
  lower case

So something in terms of storing/reporting the error message goes wrong if the XPath evaluation code is run before the XQuery evaluation code.

Again the use of XSLT code as the input to XPath or XQuery is weird but after switching to a "production server" for my Python/Flask based SaxonC powered XML workbench finally freed me from all the threading related crashes I am now having a working app but I am occasionally running into some weirdness with error messages being None, the above is an attempt to isolate that problem.

Please register to edit this issue

Also available in: Atom PDF