0

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 the supplied value is byte. What am I doing wrong?

XSL:-

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

Java code:-

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

Error code: XPTY0004: Required item type of value of variable is xs:base64Binary; supplied value has item type xs:byte

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
user1573133
  • 904
  • 1
  • 9
  • 23
  • 1
    Do you need to supply a String instead? See http://stackoverflow.com/questions/4838464/convert-byte-to-base64-string-for-data-uri – BretC May 18 '15 at 16:01
  • I changed it to base 64 encoded String using Base64.encodeBase64String(bytes); ends up in "Cannot convert value class net.sf.saxon.value.SequenceExtent of type xs:base64Binary to class net.sf.saxon.value.Base64BinaryValue" – user1573133 May 18 '15 at 18:28
  • Added the tag "saxon": type conversions depend on which XSLT processor you are using, and your comment shows that you are using saxon. – Michael Kay May 25 '15 at 18:31
  • Please note, you also raised the same question at https://saxonica.plan.io/boards/3/topics/6128. It's generally bad form to raise the same question on more than one forum unless one of them gives you no answer. – Michael Kay May 25 '15 at 18:34

2 Answers2

1

XSLT is waiting for a base64binary but you're giving a byte, if you dont mind the type, just change:

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

by

<xsl:param name="attachmentBytes" as="xs:byte" />

If you want to mantain the base64binary check this answer that shows how to convert from byte[] to base64binary, basically:

StringBuilder sb = new StringBuilder();
sb.append("data:image/png;base64,");
sb.append(StringUtils.newStringUtf8(Base64.encodeBase64(imageByteArray, false)));
contourChart = sb.toString();
Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • In fact, I was sending a byte array returned using String.getBytes(). i tried the snippet above. Since this is an xsl transformation, i removed the "data:image/png;base64," and tried encoding it. However it ends up in "Cannot convert value class net.sf.saxon.value.SequenceExtent of type xs:base64Binary to class net.sf.saxon.value.Base64BinaryValue". – user1573133 May 18 '15 at 18:30
0

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

public void testBase64BinaryParam() {
        try {
            String xsl = "<xsl:stylesheet version=\"2.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\n" +
                " <xsl:param name='in' as='xs:base64Binary' xmlns:xs='http://www.w3.org/2001/XMLSchema'/>\n" +
                " <xsl:template match='/'>\n" +
                "   <out><xsl:value-of select='string($in)'/></out>\n" +
                " </xsl:template> \n" +
                "</xsl:stylesheet>";
            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

<out>U0FNUExF</out>
Michael Kay
  • 156,231
  • 11
  • 92
  • 164