Project

Profile

Help

Problem with xsl:import and Java

Added by Al Niessner 5 months ago

Recently leapt Saxon-HE from 9.9 to 12.3 and have new warning messages that I simply do not understand. I have two xsl files that are both really large where file A does an import of file B. With 9.9.1-7 it processes without difficulty. With 12.3, receive the following warning:

WARNING: Supplied DOM uses namespaces, but is not created as namespace-aware

Tried random things to no avail but did reduce the problem to a small example. Using the CD example at W3 Schools (inserted at the bottom) it produces the same results:

    Source isoSchematron = new StreamSource(
        LabelValidator.class.getResourceAsStream("/simple_test.xsl"));
    isoTransformer = isoFactory.newTransformer(isoSchematron);
    transformerFactory = TransformerFactory.newInstance();

produces the previously stated warning. Is there something missing from the XSL, is the call for the transformation different, is this a bug in saxon?

There are no other functions that would allow the inclusion of namespaces or how to make the import namespace aware that I could find. Google searches have not been very helpful but that may be because I do not understand what keywords would net meaningful results.

As always, any and all help is appreciated.

simple_test.xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:import href="cdcatalog_ex3.xsl"/>

<xsl:template match="/">
  <xsl:apply-imports/>
</xsl:template>

</xsl:stylesheet> 

cdcatalog_ex3.xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
    <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <tr>
        <td><xsl:value-of select="catalog/cd/title"/></td>
        <td><xsl:value-of select="catalog/cd/artist"/></td>
      </tr>
    </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>

Replies (5)

Please register to reply

RE: Problem with xsl:import and Java - Added by Michael Kay 5 months ago

What the error message means is that you have supplied an input to Saxon (it might be a source document, a stylesheet, or a schema) in the form of a DOMSource, and calling getNodeName() on the outermost element of the DOM has returned a name that contains a colon. That indicates that the DOM was probably created using a DocumentFactory that wasn't set to be namespace-aware. It's a warning, because processing a non-namespace-aware DOM will sometimes succeed, but it's pretty unreliable.

This is nothing to do with your XML, XSLT, or XSD code. Somewhere in your Java code (or third-party Java code?) you are building a DOM without namespace-awareness switched on, and you should change that.

As I mentioned, processing a non-namespace-aware DOM sometimes works - it depends what you are doing. We introduced the warning because it doesn't always work, and because people don't read the documentation so they don't realise they are doing anything wrong. For an example of the kind of thing that can go wrong, see https://saxonica.plan.io/issues/5859 and the other issues that it references.

Note also that the check we make won't catch all cases where the DOM isn't namespace-aware. Unfortunately there's no way of asking whether the DOM is namespace-aware; we just look at one particular clue.

Generally you should avoid using DOM with Saxon, even if it is namespace-aware. Using Saxon's native tree model is 5 to 10 times faster.

A final point: your question suggests that this is something to do with xsl:import. But you haven't given us any information that links this to xsl:import.

RE: Problem with xsl:import and Java - Added by Al Niessner 5 months ago

Sorry, I missed some code.

// Use saxon for schematron (i.e. the XSLT generation).
System.setProperty("javax.xml.transform.TransformerFactory",
    "net.sf.saxon.TransformerFactoryImpl");
TransformerFactory isoFactory = TransformerFactory.newInstance();
// Load the isoSchematron stylesheet that will be used to transform each
// schematron file
Source isoSchematron = new StreamSource(
    LabelValidator.class.getResourceAsStream("/schematron/iso_svrl_for_xslt2.xsl"));
isoTransformer = isoFactory.newTransformer(isoSchematron);
transformerFactory = TransformerFactory.newInstance();

As best I can tell, I am using the saxon transformer library. While I looked at how to instantiate or configure it to be namespace aware, I was unable to find it. Tried google, docs, etc. Not saying it is not there. Just saying I could not find the needle in the very, very large haystack.

RE: Problem with xsl:import and Java - Added by Michael Kay 5 months ago

Somewhere your Java code is building a DOM. Search for uses of DocumentFactory.

RE: Problem with xsl:import and Java - Added by Al Niessner 5 months ago

Thanks to all for the help. While typing a response that this is all of the code in the unit test I found the document factory in the URI resolver. Updated it and now it works without warning. Again, thanks.

    (1-5/5)

    Please register to reply