简体   繁体   中英

Java: string xml validation against xsd that include another xsd

Update 1: New Validation method throws new error, see below.

Update 2: i have checked the xml files with xml spy. No errors there.

Hope somebody see where iam wrong.

i'm currently trying to validate xml file against an xsd which includes another xsd. I don' know if my xsds or the java code is faulty. I dont have the xml/xsd files stored, i get them as a base64 string from the server. Hope somebody can help.

I get the fllowing error:

org.xml.sax.SAXParseException; lineNumber: 81; columnNumber: 104; src-resolve: Cannot resolve the name 'ServiceSpecificationSchema:ServiceIdentifier' to a(n) 'type definition' component.

-ServiceSpecificationSchema.xsd

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ServiceSpecificationSchema="http://example.org/ServiceSpecificationSchema.xsd" targetNamespace="http://example.org/ServiceSpecificationSchema.xsd" version="1.0" xml:lang="EN">

    <include schemaLocation="ServiceBaseTypesSchema.xsd"/>
    <element name="serviceSpecification" type="ServiceSpecificationSchema:ServiceSpecification">
        <unique name="serviceDataModelTypeKey">
            <selector xpath=".//xs:*"/>
            <field xpath="@name"/>
        </unique>
        <keyref name="serviceDataModelReturnValueTypeKeyRef" refer="ServiceSpecificationSchema:serviceDataModelTypeKey">
            <selector xpath=".//ServiceSpecificationSchema:returnValueType"/>
            <field xpath="ServiceSpecificationSchema:typeReference"/>
        </keyref>
        <keyref name="serviceDataModelParameterTypeTypeKeyRef" refer="ServiceSpecificationSchema:serviceDataModelTypeKey">
            <selector xpath=".//ServiceSpecificationSchema:parameterType"/>
            <field xpath="ServiceSpecificationSchema:typeReference"/>
        </keyref>
    </element>
    <complexType name="ServiceSpecification">
        <all>
            <element name="id" type="ServiceSpecificationSchema:ServiceIdentifier" minOccurs="1" maxOccurs="1"/>
.....

ServiceBaseTypeSchema:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ServiceSpecificationSchema="http://example.org/ServiceSpecificationSchema.xsd" targetNamespace="http://example/ServiceSpecificationSchema.xsd" version="1.0" xml:lang="EN">
...
    <simpleType name="ServiceIdentifier">
        <restriction base="string"/>
    </simpleType>
...
</schema>

Java Validator

public void validate(String inputXml, String XSD, String XSD2)
        throws SAXException, IOException {
        try {
            byte[] decoded = Base64.decodeBase64(XSD);
            XSD = new String(decoded, "UTF-8");
            byte[] decoded2 = Base64.decodeBase64(XSD2);
            XSD2 = new String(decoded2, "UTF-8");

        SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

        Schema schema = factory.newSchema(new SAXSource[]
        {
        (new SAXSource(new InputSource(new StringReader(XSD)))),
        (new SAXSource(new InputSource(new StringReader(XSD2))))
        });

        Validator validator = schema.newValidator();
        validator.validate(new StreamSource(new StringReader(inputXml)));
        } catch (SAXException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
        } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
        }

}

I found another possible solution, but it still don't work. With the following method, the exeption is thrown, wenn validating the xsds against the xml. The line of the excetion depends on the used xml. In the Example on the bottom, the error is in line 2. -> Error: Cannot find the declaration of element 'ServiceSpecificationSchema:serviceSpecification'

I think: 1. My java code is faulty or 2. all the xml files are.

New Java Validator:

byte[] decoded = Base64.decodeBase64(XSD);
                byte[] decoded2 = Base64.decodeBase64(XSD2);

                SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

                InputStream impFis = new ByteArrayInputStream(decoded2);
                InputStream mainFis = new ByteArrayInputStream(decoded);

                Source main = new StreamSource(mainFis);
                Source imp = new StreamSource(impFis);
                Source[] schemaFiles = new Source[] {imp, main};
                Schema schema = factory.newSchema(schemaFiles);
                Validator validator = schema.newValidator();
                validator.validate(new StreamSource(new StringReader(inputXml))); 

Example XML file:

<?xml version="1.0" encoding="UTF-8"?>
<ServiceSpecificationSchema:serviceSpecification
        xmlns:ServiceSpecificationSchema="http://example.org/ServiceSpecificationSchema.xsd"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://example.org/ServiceSpecificationSchema.xsd">
.....

Finally i found a solution. Thestandard method does not resolve included or imported files from the start. To resolve the included files i used the LSResourceResolver.

This works for me:

@Service
public class ResourceResolverImpl implements LSResourceResolver {
private ILoadFromSRService iLoadFromSRService;

@Autowired
public ResourceResolverImpl(ILoadFromSRService iLoadFromSRService){
    this.iLoadFromSRService = iLoadFromSRService;
}

public LSInput resolveResource(String type,
                               String namespaceURI,
                               String publicId,
                               String systemId,
                               String baseURI) {
    String string =iLoadFromSRService.getServiceBaseTypeSchema();
    string = string.replace("\n", "").replace("\t", "");
    InputStream resourceAsStream = new ByteArrayInputStream( string.getBytes());
    return new LSInputImpl(publicId, systemId, resourceAsStream);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM