2009-09-30 22 views
5

我試圖在MSDN上修改JScript示例,以便根據某些模式驗證XML。德爾福:無效的XML通過MSXML驗證

作爲第一個認證,我使用了示例中使用的sl-valid.xml,sl-notValid.xml和sl.xsd文件。

我的代碼去如下:

procedure BasicValidation(FileName: string); 
var 
    XML: IXMLDOMDocument2; 
begin 
    // Load XML and resolve externals 
    XML := ComsDOMDocument.Create; 
    XML.async := False; 
    XML.validateOnParse := True; 
    XML.resolveExternals := True; 
    XML.setProperty('SelectionLanguage', 'XPath'); 
    XML.setProperty('SelectionNamespaces', 'xmlns:x=''urn:book'''); 
    XML.load(FileName); 
    if XML.parseError.errorCode <> 0 then 
    ShowMessage('Error parsing. Reason: ' + XML.parseError.reason) 
    else 
    ShowMessage('XML validation OK.'); 
end; 

當我嘗試了SL-notValid.xml文件,我仍然得到 'XML驗證OK'。有沒有人見過這個?上述代碼與JScript考試http://msdn.microsoft.com/en-us/library/ms764717%28VS.85%29.aspx的基本區別是什麼?

回答

8

試試這個

procedure BasicValidation(FileName: string); 
var 
    XML: IXMLDOMDocument2; 
begin 
    XML := CoDOMDocument40.Create; 
    XML.async := False; 
    XML.validateOnParse := True; 
    XML.resolveExternals := True; 
    XML.setProperty('SelectionLanguage', 'XPath'); 
    XML.setProperty('SelectionNamespaces', 'xmlns:x=''urn:book'''); 
    XML.load(FileName); 
    if XML.parseError.errorCode <> 0 then 
    ShowMessage('Error parsing. Reason: ' + XML.parseError.reason) 
    else 
    ShowMessage('XML validation OK.'); 
end; 

解釋,你必須顯式調用的,對於XSD架構驗證支持版本(MSXML> = 4)的構造函數。

再見。

+0

非常感謝,RRUZ! – conciliator 2009-10-01 06:42:12