0

I try to load my local xsd file (import...schemaLocation="gml.xsd" but it seems nothing working because I got always same error :

org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 122; src-resolve: Cannot resolve the name 'gml:AbstractFeature' to a(n) 'element declaration' component.

config.xsd :

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:gml="http://www.opengis.net/gml/3.2" xmlns:pcrs="http://cnig.gouv.fr/pcrs" xmlns:pcrs-i="http://cnig.gouv.fr/pcrs-information" targetNamespace="http://cnig.gouv.fr/pcrs" elementFormDefault="qualified" version="2.0beta2">
    <import namespace="http://cnig.gouv.fr/pcrs-information" schemaLocation="CNIG_PCRS-INFO_v2.0.xsd"/>
    <import namespace="http://www.opengis.net/gml/3.2" schemaLocation="gml.xsd"/>
    <!--XML Schema document created by ShapeChange - http://shapechange.net/-->
    <element name="AffleurantEnveloppePCRS" type="pcrs:AffleurantEnveloppePCRSType" substitutionGroup="gml:AbstractFeature">

java :

    ClassLoader classloader = Thread.currentThread().getContextClassLoader();
    InputStream xsd = classloader.getResourceAsStream("gml/config.xsd");
    InputStream gml = classloader.getResourceAsStream("gml/test.gml");

    public boolean validateXSD() {
            try {
                SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
                Schema schema = factory.newSchema(new StreamSource(xsd));
                Validator validator = schema.newValidator();
                validator.validate(new StreamSource(gml));
                return true;
            } catch (Exception error) {
                System.out.println(error);
                return false;
            }
        }
justin1298
  • 147
  • 2
  • 8
  • Is the file actually present in the *current directory* of the Java program? – Andreas Sep 17 '20 at 22:06
  • @Andreas yes it is in my resource folder – justin1298 Sep 17 '20 at 22:15
  • *"in my resource folder"* Is your resource folder the current directory of the program when you run it? I highly doubt that. The project folder is *generally* the current directory. – Andreas Sep 17 '20 at 22:16
  • @Andreas I have a folder xsd in my resource folder with this xsd file and gml.xsd file – justin1298 Sep 17 '20 at 22:17
  • What is your point? Since the folder where the file is located is not your current directory, a relative path like `gml.xsd` cannot find the file. Or did you expect the file path to be relative to the location of the main XSD file? Does the XML library, which is loading the main XSD file, *know* where it is loading the file from? If not, how could it possibly find another file relative to that location? --- If you need further help, you might need to show how you're loading the main XSD file. – Andreas Sep 17 '20 at 22:23
  • @Andreas I try to validate my gml file with the xsd. My code take the path of this xsd file and `gml.xsd` file are in the same folder so that's why I just put `schemaLocation="gml.xsd"` – justin1298 Sep 17 '20 at 22:26
  • *"My code take the path of this xsd file"* How does it do that? Are we supposed to guess? Let me repeat myself, and highlight it so you might see it: **If you need further help, you might need to *show* how you're loading the main XSD file.** – Andreas Sep 17 '20 at 22:29
  • @Andreas Oh ok ! sorry I just edited my post with java file – justin1298 Sep 17 '20 at 22:33

1 Answers1

3

Since you're supplying a byte stream, the XML library has no idea where the file is located, so it cannot resolved relative paths.

As a fall-back, it likely resolved the relative paths relative to the current directory, but since the files are not there, they are not found.

Specify the XSD as a URI.

public boolean validateXSD() {
    ClassLoader classloader = Thread.currentThread().getContextClassLoader();
    URL xsd = classloader.getResource("gml/config.xsd");
    URL gml = classloader.getResource("gml/test.gml");
    try {
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = factory.newSchema(xsd); // Pass URL to XML library
        Validator validator = schema.newValidator();
        validator.validate(new StreamSource(gml.toString())); // Pass URL as a string
        return true;
    } catch (Exception error) {
        System.out.println(error);
        return false;
    }
}

Advantage: You don't have to remember to close the streams, which you seemed to have forgot.

Andreas
  • 154,647
  • 11
  • 152
  • 247