Java – how do I validate my XML by referencing XSD from classpath?
As part of my XML transformation (using XSL), I am creating an attribute for the schema location so that the result of the transformation can be verified However, when I try to run it as a stand-alone test, I want to be able to set the corresponding property / value of the mode location to close my classpath
After a Google search, I tried various schemalocation attributes. The latest attempt is:
.. <xsl:attribute name="xsi:external-noNamespaceSchemaLocation"> <xsl:value-of select="$schemaLocation"/> </xsl:attribute> ..
The results after transformation are as follows:
<?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 have carefully checked my emrexception when the validator runs_ 1_ The 0.xsd file is obviously located in classpath It is a DOM validator. I use Xerces 2.9 1 / Xalan 2.7. 1. My result is:
Caused by: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'emrException'.
However, when I exchange schema locations that read XSD files from the file system or external URLs, the validator runs interesting
How do I read XSD from classpath?
Solution
One method is to implement its own lsresourceresolver and lsinput, so that the incoming systemid can be resolved