javax.xml.validation defines interfaces, not implementations. The set of Source classes recognized depends on the implementation you are using, not on the interface. I think your question is about the default implementation of the interface, which in the Oracle JDK is the Sun-produced version of Apache Xerces. If you use the Saxon-EE implementation of the javax.xml.validation interfaces, it will accept the source produced by XdmNode.asSource().

I think that the Sun implementation will probably accept a StreamSource, a SAXSource, or a DOMSource. I suspect that the simplest approach is to serialize the XdmNode to an in-memory byte array, and then wrap this in a StreamSource. Going via SAX is probably possible, but would require diving into more Saxon internals than you may be comfortable with.

Michael Kay

On 24/09/2011 22:48, Hans-Juergen Rennau wrote:
Hello Michael,

probably not Saxon's fault, but I report it because the problem might be of interest to others, and perhaps you recommend a workaround.

When validating a document using javax.xml.validation, one cannot supply an XdmNode as a Source object (obtained by XdmNode.asSource()).
Indeed, the Javadoc of "asSouce" states: "However, there is no guarantee that it will be recognized by other products."

What would you suggest to do if one has schema and/or instance document available as XdmNode and wants to use javax.xml.validation? Transform it to DOM (as I suppose), or serialize, or yet something else? Using S9API, the scenario is very common, so a best practise recommendation would be appreciated.

Thank you,
-- Hans-Jürgen

PS: Example code:
XdmNode schema = ...;
XdmNode doc = ...;
validateXml(doc.asSource(), schema.asSource());

static public void validateXml(Source xmlSource, Source schemaSource) throws X4JException {
try {
   SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
   Schema schema = factory.newSchema(schemaSource);
} catch (SAXException e) {...

Both, "schemaSource" and "xmlSource" in this code are rejected. Error message:
java.lang.IllegalArgumentException: Source parameter of type 'net.sf.saxon.tree.tiny.TinyElementImpl' is not accepted by this validator.

All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.

saxon-help mailing list archived at