How can I reference an XSD off the CLASSPATH for validating my XML?

1.2k views Asked by At

As part of my XML transform (using XSL), I'm creating an attribute for the schema location so the the result of the transform can be validated. However, as I'm trying to run this as a self-contained test I want to be able to set the appropriate attribute/value for the schema location to be off my CLASSPATH.

After a bit of Googling I've tried various schemaLocation attributes, the latest attempt being:

..
<xsl:attribute name="xsi:external-noNamespaceSchemaLocation">
  <xsl:value-of select="$schemaLocation"/>
</xsl:attribute>
..

Which after the transform results in:

<?xml version="1.0" encoding="UTF-8"?>
<emrException 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:external-noNamespaceSchemaLocation="emrException_1_0.xsd">
    ..
    <!-- more well-formed XML -->
</emrException>

I've double checked that my emrException_1_0.xsd file is clearly in the CLASSPATH when the validator runs. It's a DOM validator and I'm using Xerces 2.9.1/Xalan 2.7.1, the result I get is:

Caused by: org.xml.sax.SAXParseException: 
    cvc-elt.1: Cannot find the declaration of element 'emrException'.

However, when I swap to using a schema location where the XSD file is read off the file system or an external URL, then the validator runs fun.

How can I get the XSD to be read off my CLASSPATH?

1

There are 1 answers

0
John Vint On

One way is to implement your own LSResourceResolver and LSInput, so that the systemId that is passed in can be resolved however you would want.