Project

Profile

Help

Support #6097

closed

How to create string sequence parameter for XSLT?

Added by Martin Honnen 11 months ago. Updated 11 months ago.

Status:
Duplicate
Priority:
Normal
Category:
Python API
Start date:
2023-06-26
Due date:
% Done:

0%

Estimated time:
Found in version:
Platforms:

Description

I am not sure whether I have hit a bug or am simply not finding the wrong way from the Python API to set an XSLT parameter to a sequence of strings; the Python code is e.g.

from saxonche import *

with PySaxonProcessor(license=False) as saxon_proc:
    xslt30_processor = saxon_proc.new_xslt30_processor()

    element_names = ['foo', 'foobar']

    xslt_param = PyXdmValue()

    for name in element_names:
        xslt_param.add_xdm_item(saxon_proc.make_string_value(name))

    xslt30_processor.set_parameter('cdata-tag-names', xslt_param)

    xslt30_processor.transform_to_file(source_file='sample2.xml', stylesheet_file='serialize-wrap-in-cdata1.xsl', output_file='result-sample2.xml')

XSLT is

<?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"
	expand-text="yes"
	version="3.0">

  <xsl:param name="cdata-tag-names" as="xs:string*" static="yes" select="'tag'"/>

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

  <xsl:output method="xml" _cdata-section-elements="{$cdata-tag-names}"/>

  <xsl:template _match="{$cdata-tag-names => string-join(' | ')}">
    <xsl:copy>{serialize(node())}</xsl:copy>
  </xsl:template>

</xsl:stylesheet>

XML is

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <foo>
    some data
    <!-- some data2 -->
    <!-- some data2 -->
    some data
  </foo>
  <bar>
    some data
    <!-- some data2 -->
    <!-- some data2 -->
    some data
  </bar>
  <foobar>
    some data
    <!-- some data2 -->
    <!-- some data2 -->
    some data
  </foobar>
</root>

Error I get:

Traceback (most recent call last):
  File "C:\Users\marti\PycharmProjects\SaxonCHEWrapChildrenInCDATA\main2.py", line 15, in <module>
    xslt30_processor.transform_to_file(source_file='sample2.xml', stylesheet_file='serialize-wrap-in-cdata1.xsl', output_file='result-sample2.xml')
  File "python_saxon\saxonc.pyx", line 1276, in saxonche.PyXslt30Processor.transform_to_file
saxonche.PySaxonApiError: Unknown exception found

Files

main2.py (532 Bytes) main2.py Martin Honnen, 2023-06-26 15:44
sample2.xml (359 Bytes) sample2.xml Martin Honnen, 2023-06-26 15:44
serialize-wrap-in-cdata1.xsl (582 Bytes) serialize-wrap-in-cdata1.xsl Martin Honnen, 2023-06-26 15:44

Related issues

Is duplicate of SaxonC - Bug #6053: PydmNode object gets deleted before reuseClosedO'Neil Delpratt2023-05-26

Actions
Actions #1

Updated by Martin Honnen 11 months ago

I have now also tried to compile the stylesheet and to use an executable but that hits a different error:

from saxonche import *

with PySaxonProcessor(license=False) as saxon_proc:
    xslt30_processor = saxon_proc.new_xslt30_processor()

    element_names = ['foo', 'foobar']

    xslt_param = PyXdmValue()

    for name in element_names:
        xslt_param.add_xdm_item(saxon_proc.make_string_value(name))

    print(xslt_param)

    xslt30_processor.set_parameter('cdata-tag-names', xslt_param)

    xslt30_executable = xslt30_processor.compile_stylesheet(stylesheet_file='serialize-wrap-in-cdata1.xsl')

    xslt30_executable.transform_to_file(source_file='sample2.xml', output_file='result-sample2.xml')

Error

Traceback (most recent call last):
  File "C:\Users\marti\PycharmProjects\SaxonCHEWrapChildrenInCDATA\main3.py", line 17, in <module>
    xslt30_executable = xslt30_processor.compile_stylesheet(stylesheet_file='serialize-wrap-in-cdata1.xsl')
  File "python_saxon\saxonc.pyx", line 1157, in saxonche.PyXslt30Processor.compile_stylesheet
saxonche.PySaxonApiError: NullPointer exception found: java.lang.NullPointerException
	at net.sf.saxon.s9api.XsltCompiler.setParameter(XsltCompiler.java:161)
	at net.sf.saxon.option.cpp.Xslt30Processor.setStaticParametersFromArray(Xslt30Processor.java:449)
	at net.sf.saxon.option.cpp.Xslt30Processor.compileFromFile(Xslt30Processor.java:305)
. Line number: -1
Actions #2

Updated by Martin Honnen 11 months ago

Using SaxonC HE 12.2 from the command line (in Powershell) with a cmd line as ` & 'C:\Program Files\Saxonica\libsaxon-HEC-windows-v12.2\command\Transform.exe' -t -s:.\sample2.xml -xsl:.\serialize-wrap-in-cdata1.xsl ?cdata-tag-names="'foo','foobar'" works fine so the XSLT seems fine; just need to find a way to pass the parameter from Python.

Actions #3

Updated by O'Neil Delpratt 11 months ago

  • Status changed from New to Duplicate

I think this bug is the same as #6053 which has now been fixed. Available in the next maintenance release.

Probably not a practical workaround but you could avoid using the for loop to create the XdmAtomicValue's.

Actions #4

Updated by O'Neil Delpratt 11 months ago

  • Is duplicate of Bug #6053: PydmNode object gets deleted before reuse added
Actions #5

Updated by Martin Honnen 11 months ago

O'Neil Delpratt wrote in #note-3:

I think this bug is the same as #6053 which has now been fixed. Available in the next maintenance release.

Probably not a practical workaround but you could avoid using the for loop to create the XdmAtomicValue's.

So not using a loop and using e.g.

    xslt30_processor = saxon_proc.new_xslt30_processor()

    xslt_param = PyXdmValue()


    xslt_param.add_xdm_item(saxon_proc.make_string_value('foo'))
    xslt_param.add_xdm_item(saxon_proc.make_string_value('foobar'))

    print(xslt_param)

    xslt30_processor.set_parameter('cdata-tag-names', xslt_param)

should avoid the crash? I still get

Traceback (most recent call last):
  File "C:\Users\marti\PycharmProjects\SaxonCHEWrapChildrenInCDATA\main5.py", line 20, in <module>
    xslt30_executable = xslt30_processor.compile_stylesheet(stylesheet_file='serialize-wrap-in-cdata1.xsl')
  File "python_saxon\saxonc.pyx", line 1157, in saxonche.PyXslt30Processor.compile_stylesheet
saxonche.PySaxonApiError: NullPointer exception found: java.lang.NullPointerException
	at net.sf.saxon.s9api.XsltCompiler.setParameter(XsltCompiler.java:161)
	at net.sf.saxon.option.cpp.Xslt30Processor.setStaticParametersFromArray(Xslt30Processor.java:449)
	at net.sf.saxon.option.cpp.Xslt30Processor.compileFromFile(Xslt30Processor.java:305)
. Line number: -1

I can't tell from the description in https://saxonica.plan.io/issues/6053 how "PydmNode object gets deleted before reuse" is a duplicate of this bug throwing while trying to create a sequence of XdmAtomicValues and pass it as a parameter to XSLT.

Bug https://saxonica.plan.io/issues/6053 was fixed weeks ago, any chance there is a Window wheel with the fix in the Saxonica Github build artefacts so I could test whether this parameter passing works? Or did you test it? Then let me know.

Sorry for asking but on the surface the "duplicate" resolution is not clear, the description there sounds quite different and there is no stack trace to tell.

Actions #6

Updated by O'Neil Delpratt 11 months ago

  • Status changed from Duplicate to In Progress

It might be a bit different, but the fix is all in the same area of handling Xdm objects that have more than one item in the sequence. I will create a windows wheel for you to test ASAP.

Actions #7

Updated by O'Neil Delpratt 11 months ago

  • Status changed from In Progress to Resolved
Actions #8

Updated by O'Neil Delpratt 11 months ago

  • Status changed from Resolved to Duplicate

Bug fix applied in the Saxon 12.3 maintenance release.

Please register to edit this issue

Also available in: Atom PDF