Search code examples
javaxmlxsltsaxonxml-generation

How to make saxon transformer backward compitable?


I have a file documentA4.xsl below:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:fo="http://www.w3.org/1999/XSL/Format"
  xmlns:fox="http://xmlgraphics.apache.org/fop/extensions"
  xmlns:cmn="*/uri/xmlns/*/1/4"
  xmlns:docgen="*/documentGeneration/1/2">
...
</xsl:stylesheet>

And docGen.xml

<?xml version="1.0" encoding="UTF-8"?>
<domainCommand xmlns="*/uri/xmlns/*/1/4">
    
    <command>
        <docgen:generateDocument xmlns:docgen="*/documentGeneration/1/3" version="1.3.0">
            <docgen:documentId>850e8400-e29b-11d4-a716-446655440479</docgen:documentId>
            <docgen:templateDomain>naturalization</docgen:templateDomain>
                        <docgen:templateName>declarationOfRegisteredPartnership.xsl</docgen:templateName>                       
            <docgen:emitTemplateApplied>true</docgen:emitTemplateApplied>
            <docgen:attachmentDomain>naturalization</docgen:attachmentDomain>
            <docgen:attachmentClass>signatureSheets</docgen:attachmentClass>
    </command>
</domainCommand>

When I use Saxon library(Saxon-HE:10.5) to transform XSLT:

var processor = new Processor(false);
var compiler = processor.newXsltCompiler();
var xsl = compiler.compile(new StreamSource(new File(pathTemplate.toUri())));
var out = processor.newSerializer();
out.setOutputProperty(Serializer.Property.METHOD, "xml");
out.setOutputProperty(Serializer.Property.INDENT, "yes");
out.setOutputFile(new File(fileNameFormattedOutput));
Xslt30Transformer transformer = xsl.load30();
transformer.transform(new StreamSource(new File(fileNameDomainCommandPath)), out);

And my formatted.xml has something wrong:

<?xml version="1.0" encoding="UTF-8"?>850e8400-e29b-11d4-a716-446655440479naturalizationdeclarationOfRegisteredPartnership.xsltruenaturalizationsignatureSheets
<fo:root xmlns:cmn="*/uri/xmlns/*/1/4"
  xmlns:docgen="*/documentGeneration/1/2"
  xmlns:fo="http://www.w3.org/1999/XSL/Format"
  xmlns:fox="http://xmlgraphics.apache.org/fop/extensions"
  xml:lang="de">

If I change "xmlns:docgen="*/documentGeneration/1/2" to "*/documentGeneration/1/3" in documentA4.xsl file the transformation work well

Is there a configuration for the Saxon library to not care about the version of xmlns:docgen="*/documentGeneration/X/X"?


Solution

  • Using versioned namespaces in generally considered a bad idea, for this very reason. XSLT is designed on the assumption that you know statically what namespace the source elements will be in. If this isn't the case, you need to using paths like *:xxxx in place of docgen:xxxx.