Conversion from XML including an XSL stylesheet with Java (to .docx or .pdf) converts plain xml-file content

Hi all,

I am struggling trying to convert an .xml file (containing .xsl style information!) to another format like .docx or .pdf. The xml/xsl file combination should use XSL-T instead of converting the raw .xml file.

The code I use is pretty simple, there is an XmlLoadOptions with a Supplier<Stream> providing the xsl-t information (from my understanding). It seems like the xsl loadOptions is being ignored, because the raw .xml content is being converted instead of the result of an xsl-transformation.

Is there any code in the example project in GitLab that I missed or any other JavaDoc that explains this procedure a bit more in detail? Or is using the XmlLoadOptions with its method #setXslFoFactory the wrong approach? Since the internal code seems to be obfuscated, I also am not sure if I am using the correct classes for Stream / MemoryStream.

Heres my code:

import com.groupdocs.conversion.contracts.LoadOptionsProvider;
import com.groupdocs.conversion.options.convert.WordProcessingConvertOptions;
import com.groupdocs.conversion.options.load.XmlLoadOptions;
...
public void convertXml(String xmlSourcePath, String fileSourceName) {
    WordProcessingConvertOptions convertOptions = new WordProcessingConvertOptions();
    String targetPath = xmlSourcePath + FilenameUtils.getBaseName(fileSourceName) + ".docx";
    String xslSourcePath = xmlSourcePath + FilenameUtils.getBaseName(fileSourceName) + ".xsl";

    LoadOptionsProvider loadOptionsProvider = () -> {
        XmlLoadOptions xmlLoadOptions = new XmlLoadOptions();
        xmlLoadOptions.setXslFoFactory(new XmlXslSupplier(xslSourcePath));
        return xmlLoadOptions;
    };

     try (Converter converter = new Converter(xmlSourcePath + fileSourceName, loadOptionsProvider)) {
        converter.convert(targetPath, convertOptions);
    } catch (Exception e) {
    }

My XmlXslSupplier provides a MemoryStream:

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.function.Supplier;

import com.groupdocs.conversion.internal.c.a.ms.System.IO.MemoryStream;
import com.groupdocs.conversion.internal.c.a.ms.System.IO.Stream;

public class XmlXslSupplier implements Supplier<Stream> {

    private final String xslSourcePath;

    public XmlXslSupplier(String xslSourcePath) {
        this.xslSourcePath = xslSourcePath;
    }
    @Override
    public Stream get() {
        MemoryStream memoryStream = new MemoryStream();
        byte[] buffer = new byte[1024];
        int bytesRead;
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xslSourcePath.getBytes());
        while (true) {
            try {
                if ((bytesRead = byteArrayInputStream.read(buffer)) == -1) break;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            memoryStream.write(buffer, 0, bytesRead);
        }
        return memoryStream;
    }
}

GroupDocs conversion version is 22.11.

Any help is very appreciated.

@EDVK

Could you please also provide the source file or the file causing the issue?

@atir.tahir

Sure, here is my example.

File name: books.xml

<?xml version="1.0" encoding="iso-8859-1" ?>

<?xml-stylesheet type="text/xsl" href="books.xsl"?>
<books>
	<book image="webtechnologien">
		<description>
			<title>Web-Technologien</title>
			<author>Sebastian Rieger</author>
			<author>Anatol Badach</author>
			<author>Matthias Schmauch</author>
		</description>
		<isbn>3446221492</isbn>
	</book>
	<book language="english" image="learningjava">
		<description>
			<title>Learning Java</title>
			<author>Niemeyer</author>
			<author>Patrick Knudsen</author>
		</description>
		<isbn>0596008732</isbn>
	</book>
	<book image="entwurfsmuster">
		<description>
			<title>Entwurfsmuster von Kopf bis Fuß</title>
			<author>Eric Freeman</author>
		</description>
		<isbn>9783897214217</isbn>
	</book>
	<book language="englisch" image="javabeans">
		<description>
			<title>Enterprise JavaBeans</title>
			<author>Bill Burke</author>
			<author>Richard Monson-Haefel</author>
		</description>
		<isbn>059600978X</isbn>
	</book>
</books>

File name: books.xsl

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:template match="/">
		<html>
			<head>
				<title>Books</title>
			</head>
			<body>
				<xsl:for-each select="books/book">
					<table width="400" height="220" background="images/frame.jpg">
						<tr>
							<td width="200" height="36"/>
						</tr>
						<tr>
							<td height="155" align="center">
								<img src="images/{@image}.jpg" border="1" bordercolor="black"/>
							</td>
							<td width="167">
								Title: <xsl:value-of select="description/title"/>
								<br/>
								<br/>
								Author:<br/>
								<xsl:for-each select="description/author">
									<xsl:value-of select="."/>
									<br/>
								</xsl:for-each>
								<br/>
								<xsl:if test="@language">
									Language: <xsl:value-of select="@language"/>
									<br/>
								</xsl:if>
								ISBN: <xsl:value-of select="isbn"/>
							</td>
							<td width="17"/>
						</tr>
						<tr>
							<td/>
						</tr>
					</table>
				</xsl:for-each>
			</body>
		</html>
	</xsl:template>
</xsl:stylesheet>

If you put these two into the same folder of a web server, you can view the styled .xml in the browser. The conversion only shows raw .xml content though.

@EDVK
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): CONVERSIONJAVA-2161

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

1 Like