2010-06-16 80 views
4

我正在嘗試一些XML模式示例,並且必須使用示例XML文件來驗證它們。模式是一個本地文件(someFile.xsd)。我正在使用eclipse,並且希望在XML文件中包含一個引用來指向這個本地xsd文件,以便eclipse可以向我建議這些元素。帶有本地XML模式副本的XML文件

我很難想出包含本地文件的語法。有什麼建議麼 ?

回答

1

您是否在使用xsi:schemaLocation屬性?

例:

<?xml version="1.0" encoding="UTF-8"?> 
<root xmlns="http://foo/target/Namespace" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:someNamespace="someFile"  
     xsi:schemaLocation=" 
     someFile someFile.xsd" > 
... 
</root> 

我相信someFile.xsd必須在classpath

0

請問像這樣的工作?

 
<?xml version="1.0"?> 
<note 
xmlns="http://www.w3schools.com" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.w3schools.com note.xsd"> 
    <to>Tove</to> 
    <from>Jani</from> 
    <heading>Reminder</heading> 
    <body>Don't forget me this weekend!</body> 
</note> 

http://www.w3schools.com/Schema/schema_howto.asp

0

複製您可以將自己的ResourceResolverLSInput實施設置爲SchemaFactory,這樣的LSInput.getCharacterStream() 通話將提供來自本地路徑的模式。它試圖提供一個comprehensive example here

的方法基本上是由一個正確執行取之於

getSchemaAsStream(input.getSystemId(), input.getBaseURI(), localPath))); 

下面的代碼的結束叫的。此時,您必須使用自己的查找機制來查找本地路徑上的模式文件。

public void validate(InputStream xmlStream, InputStream schemaStream, String baseUri, String localPath) 
       throws SAXException, IOException { 
    Source xmlFile = new StreamSource(xmlStream); 
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
    factory.setResourceResolver((type, namespaceURI, publicId, systemId, baseURI) -> { 
     LSInput input = new DOMInputImpl(); 
     input.setPublicId(publicId); 
     input.setSystemId(systemId); 
     input.setBaseURI(baseUri); 
     input.setCharacterStream(new InputStreamReader(
         getSchemaAsStream(input.getSystemId(), input.getBaseURI(), localPath))); 
     return input; 
    });