Java:针对包含另一个xsd的xsd的字符串xml验证

Java:针对包含另一个xsd的xsd的字符串xml验证,java,validation,xsd,include,Java,Validation,Xsd,Include,更新1:新的验证方法抛出新错误,请参见下文 更新2:我已经用XMLSpy检查了xml文件。没有错误 希望有人知道我错在哪里 我目前正在尝试根据包含另一个xsd的xsd验证xml文件。我不知道我的XSD或java代码是否有错误。我没有存储xml/xsd文件,而是将它们作为base64字符串从服务器获取。希望有人能帮忙 我得到了流动误差: org.xml.sax.SAXParseException; lineNumber: 81; columnNumber: 104; src-resolve: Ca

更新1:新的验证方法抛出新错误,请参见下文

更新2:我已经用XMLSpy检查了xml文件。没有错误

希望有人知道我错在哪里

我目前正在尝试根据包含另一个xsd的xsd验证xml文件。我不知道我的XSD或java代码是否有错误。我没有存储xml/xsd文件,而是将它们作为base64字符串从服务器获取。希望有人能帮忙

我得到了流动误差:

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"/>
.....
我找到了另一个可能的解决方案,但仍然不起作用。使用下面的方法,抛出exeption,根据xml验证XSD。例外行取决于使用的xml。在底部的示例中,错误在第2行。->错误:找不到元素“ServiceSpecificationSchema:serviceSpecification”的声明。

我认为:1。我的java代码有错误或2。所有的xml文件都是

新Java验证程序:

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))); 
示例XML文件:

<?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">
.....

.....

我终于找到了解决办法。标准方法不会从一开始解析包含或导入的文件。为了解析包含的文件,我使用了LSResourceResolver

这对我很有用:

@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);
    }
}

最后我找到了解决办法。标准方法不会从一开始解析包含或导入的文件。为了解析包含的文件,我使用了LSResourceResolver

这对我很有用:

@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);
    }
}
@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);
    }
}