2017-09-28 18 views
0

我想驗證一個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))); 

回答

0

終於讓我找到一個解決方案。

這個工作對我來說:

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