2016-08-23 90 views
1

我想創建一個遵循XML架構http://www.oid-info.com/oid.xsd的XML。這個XSD應該在XML文件中以某種方式被引用,以便諸如https://validator.w3.org/的工具可以檢查它以發現數據的語義錯誤,例如,當一個電子郵件地址無效時。如何在XML中引用W3C標記驗證服務的XSD?

首先,我想這樣說:

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<oid-database xmlns="http://www.oid-info.com/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.oid-info.com/oid.xsd"> 
<submitter> 
.... 
</submitter> 
<oid> 
.... 
</oid> 
</oid-database> 

W3C Markup Validation Service說:

沒有DOCTYPE找到了!僅檢查XML語法。 DOCTYPE聲明 未被識別或丟失。這可能意味着正式的 公共標識符包含拼寫錯誤,或者聲明 未使用正確的語法,或者您的XML文檔未使用DOCTYPE聲明。文檔的驗證已被跳過,並且 對XML語法的格式良好性的簡單檢查已被執行 。

好了,所以我試圖用DOCTYPE

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<!DOCTYPE oid-database> 
<oid-database xmlns="http://www.oid-info.com/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.oid-info.com/oid.xsd"> 
<submitter> 
.... 
</submitter> 
<oid> 
.... 
</oid> 
</oid-database> 

現在W3C驗證說:

2號線,23欄:沒有內部或外部的文檔類型聲明 子集;將不經驗證解析

我確實知道DTD是什麼,過去我曾與DTD合作過。我知道這裏缺少一個DTD。但我想檢查一個XSD,而不是針對一個DTD。

我該怎麼做才能使它工作?

回答

2

W3C Validator將檢查格式良好並將驗證對DTD;它不會針對XSD進行驗證。你將不得不使用另一個在線或離線驗證器。

而且,這裏是你的XML文件應該是什麼樣子暗示,它應該對OID XSD被驗證有效:

<?xml version="1.0" encoding="UTF-8" ?> 
<oid-database xmlns="http://oid-info.com" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xsi:schemaLocation="http://oid-info.com 
            http://www.oid-info.com/oid.xsd"> 
    <submitter> 
    .... 
    </submitter> 
    <oid> 
    .... 
    </oid> 
</oid-database> 

參見:How to link XML to XSD using schemaLocation or noNamespaceSchemaLocation?

注意,這些XSD文件似乎有自己的的問題,包括:

[警告] XHTML-light.xsd:8:36:schema_reference.4:無法讀取 模式文檔 'xml.xsd',因爲1)找不到文件; 2) 該文件無法讀取; 3)文檔的根元素不是 。

[錯誤] oid.xsd:91:46:InvalidRegex:模式值 「{\ S *(((([AZ] [A-ZA-Z0-9-] \ S((\ (0 | [1-9] [0-9] )\ s))?|(0 | [1-9] [0-9] ))){2,} \ s) ((0 | [1-9] [0-9] )(。(0 | [1-9] [0-9]))))\ s}?' 不是有效的正則表達式。

+0

非常感謝您提供此信息。您使用了哪種工具來驗證此XML?該工具http://www.freeformatter.com/xml-validator-xsd.html確實需要您手動輸入XSD。我希望它可以直接從XML和下載中自動讀取它進一步引用XSD(如xhtml-light.xsd)。 –

+0

我不使用在線驗證器,但[this one](https://devutilsonline.com/xml/validate)似乎滿足您的要求;我使用了基於Xerces-J的脫機驗證器。 – kjhughes