'javax.xml.transform.TransformerException: com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: Content is not allowed in prolog

I want to map string code to string value using xsl script in Java. I am getting some exception.

The xsl script:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
               xmlns:xjus="http://www.test"
    >
    <xsl:template name="myName">
        <xsl:param name="value" select="."/>
        <xsl:choose>
            <xsl:when test="$value = 'A'">
                Car
            </xsl:when>
            <xsl:when test="$value = 'B'">
                Pig
            </xsl:when>
            <xsl:otherwise>
                cannot recognize <xsl:value-of select="$value"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
            
</xsl:transform>

The Java code:

I run it using getValueForCode("A");

private String getValueForCode(String code) {
        try {
            TransformerFactory factory = TransformerFactory.newInstance();
            factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
            factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
            factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
            Source xslt = new StreamSource(new InputStreamReader(getClass().getResourceAsStream("/xslt/my-mapping.xsl")));
            Transformer transformer = factory.newTransformer(xslt);
            StringWriter writer = new StringWriter();
            Source text = new StreamSource(new StringReader(registerTypeCode));
            transformer.transform(text, new StreamResult(writer));
            return writer.toString();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new IllegalArgumentException("Could not transform");
        }
    }

The exception:

javax.xml.transform.TransformerException: javax.xml.transform.TransformerException: com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: Content is not allowed in prolog.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source