Project

Profile

Help

Bug #6166

closed

Public fields in a Java instance object cannot be accessed from XSLT

Added by Martin Honnen 9 months ago. Updated 6 months ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Category:
Saxon extensions
Sprint/Milestone:
-
Start date:
2023-08-12
Due date:
% Done:

100%

Estimated time:
Legacy ID:
Applies to branch:
11, 12, trunk
Fix Committed on Branch:
11, 12, trunk
Fixed in Maintenance Release:
Platforms:
Java

Description

The documentation on reflexive extension functions to XSLT/XPath/XQuery in e.g. https://www.saxonica.com/html/documentation12/extensibility/extension-functions-J/reflexive-functions/ always talks about "method, or field of the Java class" but while I get access to an instance method working I don't get a stylesheet compiled which I hope to access a public field.

Is there any sample showing that?

Why does the following code fail?

package org.example;

import net.sf.saxon.s9api.*;

import javax.xml.transform.stream.StreamSource;
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) throws SaxonApiException {
        Processor processor = new Processor(true);

        XsltCompiler xsltCompiler = processor.newXsltCompiler();

        XsltExecutable xsltExecutable = null;

        try {
            xsltExecutable = xsltCompiler.compile(new StreamSource("processPerson2Test1.xsl"));
        }
        catch (SaxonApiException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            return;
        }

        Xslt30Transformer xslt30Transformer = xsltExecutable.load30();

        Map<QName, XdmValue> parameters = new HashMap<>();
        //parameters.put(new QName("person1"), new XdmExternalObject(new Person1("John")));
        parameters.put(new QName("person2"), new XdmExternalObject(new Person2("John")));

        xslt30Transformer.setStylesheetParameters(parameters);

        xslt30Transformer.callTemplate(null, processor.newSerializer(System.out));
    }
}
package org.example;
public class Person2 {
    public Person2(String name) {
        this.name = name;
    }
    public String name;
}

Tested with Saxon 12.3 EE and the 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"
                xmlns:jt="http://saxon.sf.net/java-type"
                xmlns:Person2="java:org.example.Person2"
                exclude-result-prefixes="#all">

    <xsl:param name="person2" as="jt:org.example.Person2" required="true"/>

    <xsl:output method="adaptive" indent="yes"/>

    <xsl:template name="xsl:initial-template">
        <xsl:sequence
                select="Person2:name($person2)"/>
    </xsl:template>

</xsl:stylesheet>

XSLT Compilation fails with a NullPointerException:

Exception in thread "main" java.lang.NullPointerException
	at com.saxonica.expr.JavaExtensionFunctionCall.typeCheck(JavaExtensionFunctionCall.java:205)
	at net.sf.saxon.style.StyleElement.typeCheck(StyleElement.java:1560)
	at net.sf.saxon.style.XSLSequence.validate(XSLSequence.java:94)
	at net.sf.saxon.style.StyleElement.validateSubtree(StyleElement.java:1740)
	at net.sf.saxon.style.StyleElement.validateChildren(StyleElement.java:1773)
	at net.sf.saxon.style.StyleElement.validateSubtree(StyleElement.java:1744)
	at net.sf.saxon.style.XSLTemplate.validateSubtree(XSLTemplate.java:610)
	at net.sf.saxon.style.PrincipalStylesheetModule.preprocess(PrincipalStylesheetModule.java:398)
	at net.sf.saxon.style.Compilation.compilePackage(Compilation.java:290)
	at net.sf.saxon.style.StylesheetModule.loadStylesheet(StylesheetModule.java:254)
	at net.sf.saxon.style.Compilation.compileSingletonPackage(Compilation.java:113)
	at net.sf.saxon.s9api.XsltCompiler.compile(XsltCompiler.java:971)
	at org.example.Main.main(Main.java:18)

Please register to edit this issue

Also available in: Atom PDF