Project

Profile

Help

Java send byte[] to base64Binary in XSL transformation

Added by Sanker S about 9 years ago

I'm trying to send a byte array from java code as a parameter to xslt. I declared the type as base64Binary in XSL and set the byte array as parameter. I get an error that " Cannot convert value class net.sf.saxon.value.SequenceExtent of type xs:base64Binary to class net.sf.saxon.value.Base64BinaryValue". What am I doing wrong?

XSL:-

<xsl:param name="attachmentBytes" as="xs:base64Binary" /> Java code:-

byte[] bytes = "SAMPLE".getBytes(); transformer.setParameter("attachmentBytes", Base64.encodeBase64String(bytes)); transformer.transform(new DOMSource(sourceDocument), new StreamResult(targetStream));

Error Cannot convert value class net.sf.saxon.value.SequenceExtent of type xs:base64Binary to class net.sf.saxon.value.Base64BinaryValue


Replies (5)

Please register to reply

RE: Java send byte[] to base64Binary in XSL transformation - Added by Michael Kay about 9 years ago

Which Saxon release are you using?

What is the full class name of "Base64"?

RE: Java send byte[] to base64Binary in XSL transformation - Added by Sanker S about 9 years ago

Saxon release: saxon-ee-9.5.1.2 Base64 full class name: org.apache.commons.codec.binary.Base64 (apache commons codec 1.10)

RE: Java send byte[] to base64Binary in XSL transformation - Added by Michael Kay about 9 years ago

I don't understand how you can get this particular error message, but I wouldn't expect your code to work.

The method org.apache.commons.codec.binary.Base64.encodeBase64Binary() returns a String, so you are passing a String to a parameter whose expected type is xs:base64Binary.

Try:

setParameter("attachmentBytes", new net.sf.saxon.value.Base64BinaryValue(bytes))

RE: Java send byte[] to base64Binary in XSL transformation - Added by Sanker S about 9 years ago

Thank you Michael.

In fact, I had tried it and the error message is same. transformer.setParameter("attachmentBytes", new net.sf.saxon.value.Base64BinaryValue(bytes)); "Error Cannot convert value class net.sf.saxon.value.SequenceExtent of type xs:base64Binary to class net.sf.saxon.value.Base64BinaryValue"

When a byte array is passed in parameter as such, it complains that the supplied parameter is a byte (error message below). "Error on line 0 XPTY0004: Required item type of value of variable $attachmentBytes is xs:base64Binary; supplied value has item type xs:byte"

RE: Java send byte[] to base64Binary in XSL transformation - Added by Michael Kay almost 9 years ago

I'm unable to reproduce the problem. The following JUnit test completes successfully:

public void testBase64BinaryParam() {
        try {
            String xsl = "\n" +
                " \n" +
                " \n" +
                "   \n" +
                "  \n" +
                "";
            TransformerFactory tfactory = TransformerFactory.newInstance();
            Transformer transformer =
                tfactory.newTransformer(new StreamSource(new StringReader(xsl)));
            byte[] bytes = "SAMPLE".getBytes();
            transformer.setParameter("in", new net.sf.saxon.value.Base64BinaryValue(bytes));
            transformer.setOutputProperty("omit-xml-declaration", "yes");
            transformer.transform(new StreamSource(new StringReader(xsl)),
                new StreamResult(System.out));
        } catch (Exception err) {
            fail();
        }
    }

The output of the above is

U0FNUExF

(Response edited to remove description of spurious bug).

    (1-5/5)

    Please register to reply