Java XSLT transfomer fails to execute with source XML has invalid schema

  Kiến thức lập trình

I have been banging my head with this for a while. I have a fairly simple XML:

<Result xmlns="http://invalid.schema.org/123">
    <Item>
        <id>123</id>
    </Item>
</Result>

and a straightforward identity transform with a simple change:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   
    <xsl:template match="/">
        <xsl:apply-templates select="*"/>
    </xsl:template>

    <xsl:template match="*">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="id">
        <xsl:copy>
            ***
        </xsl:copy>
    </xsl:template>

   
</xsl:stylesheet>

However when I run a java code to apply such transformation:

 StreamSource xslt = new StreamSource(xsltFile);
TransformerFactory factory = TransformerFactory.newInstance();    
Transformer transformer = factory.newTransformer(xslt);
            
StreamSource xmlSource = new StreamSource(sourceFile);
StreamResult result = new StreamResult(targetFile);
transformer.transform(xmlSource, result);

It simply won’t transform the file.

However I noted that because the xml source has an invalid xmlns schema, if I remove it :

<Result>
    <Item>
        <id>123</id>
    </Item>
</Result>

Then the transformation works.

Does anyone have a clue on why is that? I could not find a way to disable validation/schema on the transformerFactory.

Thanks folks

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT