Analysis scheme and import in the same scheme

Just wondering if there are any ways to parse the xsd and xsd file imported into the original xsd, so I can directly access the elements of the imported xsd. Are there any frameworks that make this possible?

Just an example of what I mean

From the XSD I want to parse:

<xsd:import namespace="..." schemaLocation="anotherFile.xsd">

<xsd:element ref="anElement" />

From XSD imported in the analyzed file

<xsd:element name="anElement">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
 <xsd:enumeration value="THIS" />
<xsd:enumeration value="IS" />
 <xsd:enumeration value="THE" />
<xsd:enumeration value="ELEMENTS" />
<xsd:enumeration value="I" />
<xsd:enumeration value="WANT" />
<xsd:enumeration value=":-)" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>

So, I want to access the elements of the imported xsd when I parse the original xsd using some kind of insert or something :-)

Is this possible somehow?

+3
source share
3 answers

Yes, you just need to implement the LSResourceResolver class that can read the schema addresses you specify:

 /**
 * This function validates a DomResult. T
 *
 * @param domResult
 * @param schemaFile path to the schmea file to validate against.
 * @throws org.xml.sax.SAXException
 * @throws java.io.IOException
 *
 */
protected void validateDomResult(DOMResult domResult, String schemaFile) throws SAXException, IOException, Exception {

    Schema schema = createSchema(schemaFile);
    javax.xml.validation.Validator validator = schema.newValidator();
    ErrorHandler mySchemaErrorHandler = new LoggingErrorHandler();
    validator.setErrorHandler(mySchemaErrorHandler);
    DOMSource domSource = new DOMSource(domResult.getNode());
    validator.validate(domSource);
    if (((LoggingErrorHandler) mySchemaErrorHandler).isError()) {
        throw new Exception("Validation Error");
    }
}

/**
 *
 * @param baseSchemaFilePath
 * @return
 * @throws java.lang.Exception
 *
 */
protected Schema createSchema(String baseSchemaFilePath) throws Exception {

    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    LSResourceResolver resourceResolver = (LSResourceResolver) new LocalSchemaLSResourceResolver();
    factory.setResourceResolver(resourceResolver);
    Schema schema = factory.newSchema(new File(baseSchemaFilePath));

    return schema;
}

LSResourceResolver, xsd :

public class LocalSchemaLSResourceResolver implements LSResourceResolver{

    protected final Log logger = LogFactory.getLog(getClass());

    public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {

        LSInput input = new DOMInputImpl();
        try {
            FileInputStream fis = new FileInputStream(new File("classpath:xsd/" + systemId));

            input.setByteStream(fis);
            return input;
        } catch (FileNotFoundException ex) {
            logger.error("File Not found", ex);
            return null;
        }

    }
}
+2

, JAXB, ( ) Java.

+1

This works for me when the XSD files are either in the local / src / main / resources directory, or packaged in a jar:

final SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
final URL url = this.getClass().getClassLoader().getResource("META-INF/parent.xsd");
final Schema schema = sf.newSchema(url);

parent.xsd included "kid.xsd" - both XSD files are in the same directory

+1
source

Source: https://habr.com/ru/post/1789324/


All Articles