1
votes

How can one validate an xml file against the multischema xsd which is in the form of a string? I have my XSD files retrieved from the database only at runtime and I don't want to create any files on the filesystem. So I have to validate XML against XSD strings.

Thanks

update: My actual problem is the import statements and include statements which are going to be file links. I want to know how to deal with import and include statements pointing to a string in the database. I mean I cannot at any point create a file. It is supposed to be a string.

1
Whichever you think can do this. Java,python...any language.user1900588
Most languages can do it. The answer will depend on which languages you useJohn Saunders
@JohnSaunders I couldn't find anything on this. Plenty of programs can be found on using string xmls as input, but nothing on import and include statements. Can you point out any example or something on this. Again, I want to know how to resolve import and include statements as strings. Most example only deal with xml string inputs.user1900588

1 Answers

0
votes

Hi I made this small example.

I think it could be optimized but it gives you a global idea for a solution. :-)

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.SchemaFactory;

import org.xml.sax.SAXException;

public class xsd {


    /**
     * @param args
     */
    public static void main(String[] args) {

        String xsd = "Your XSD";
        String xsd2 = "2nd XSD";

        InputStream is = new ByteArrayInputStream(xsd.getBytes());
        InputStream is2 = new ByteArrayInputStream(xsd2.getBytes());

        SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);

        Source source = new StreamSource(is);
        Source source2 = new StreamSource(is2);

        try {
            sf.newSchema(new Source[]{source,source2});
        } catch (SAXException e) {
            e.printStackTrace();
        }
    }
}