带两个XSD字符串的XML字符串验证(含include)/ LSResourceResolver如何工作?

问题描述:

我想验证一个XML字符串对两个包含XSD的字符串。一个XSD包含另一个。我收到错误: “无法将名称'ServiceSpecificationSchema:ServiceIdentifier'解析为(n)'类型定义'组件。”带两个XSD字符串的XML字符串验证(含include)/ LSResourceResolver如何工作?

看起来,我的代码无法识别第二个XSD文件。其他人通过使用LSResourceResolver(见这里:How to validate an XML file using Java with an XSD having an include?

解决了这个问题但是在那个例子中,文件存储在本地。有没有一种好方法,这种方法适用于我的XSD字符串?

任何提示,将不胜感激。

我迄今为止代码:

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

终于让我找到一个解决方案。

这个工作对我来说:

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