I have to invoke a web service with a single parameter, a String in XML format. I’m building this via an XSLT transformation. So far so good.
The problem is with this XSD fragment:
<xs:complexType name='Document'> <xs:sequence> <xs:element name='title' type='xs:string' minOccurs='1'/> <xs:element name='content' type='xs:base64Binary' minOccurs='1'/> </xs:sequence> </xs:complexType>
which translates (for example) into this XML:
<attachment> <title>test title</title> <content> PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Inllc yI/Pg0KPG1zZ3ByYXRpY2E+DQogICAgPHByYXRpY2E+DQogICAgICAgIDxwcm9jZXNzbz 4NCiAgICAgICAgICAgIDxjb2RQcm9jZXNzbz4xPC9jb2RQcm9jZXNzbz4NCiAgICAgICA gICAgIDxjb2RJc3RhbnphUHJvY2Vzc28MzwvY29kSXN0YW56YVByb2Nlc3NvPg0KICAgI CAgICAgICAgPGNvZFN0YXRvPjYwPC9jb2RTdGF0bz4NCiAgICAgICAgPC9wcm9jZXNzbz 4NCiAgICA8L3ByYXRpY2E+DQo8L21zZ3ByYXRpY2E+ </content> </attachment>
Yes, you got it right: I have to insert a file content into the XML document in base 64 binary format.
I thought about inserting a placeholder with XSLT and then processing the XML document to replace it with the actual file content, but I’m wondering if there are any best practices for these occasions, maybe some fancy XSTL trick well beyond my knowledge or some Java tools which may come in handy.
How would you do that?
NOTE: I can’t use SOAP with attachment, and I’m well aware that the aforementioned approach is prone to failure in case of huge attachments, but at the moment our counterpart will not budge.
Probably the best way to do this is to read the file and encode its contents in base64 by the code that instantiates the XSLT transformation. The base64 string can either be passed as a parameter to the transformation, or the transformation could request it via an extension method.
In principle XSLT could be used to do the encoding to base64, however some byte values, such as 0 (for XML 1.0 and XML 1.1), and 29 other codes below 0x20 (for XML 1.0) are forbidden as characters within an XML document and this makes such encoding in XSLT impossible.
( see: http://projects.ischool.washington.edu/tabrooks/545/ContentManagement/PassingParameters.htm )