2012-01-12 72 views
0

我想弄清楚xml中的任何無效元素或屬性。我使用Oxygen XML Editor從xml創建了一個XSD文件。現在我試圖解析&使用XSD驗證xml,但即使我添加了新屬性,我的xml也會解析。以下是代碼。現在,即使我將JUNKATTRIBUTE添加到我的xml中,它也會被解析。 有什麼建議嗎?驗證XSD中的無效屬性對XSD

我的代碼

public static boolean validatehelp(String helpData, helpReport helpReport) { 

    SAXParserFactory spf = SAXParserFactory.newInstance(); 
    SAXParser parser = null; 
    spf.setNamespaceAware(true); 
    spf.setValidating(true); 

    FileReader fileReader = null; 
    try { 
     SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
     fileReader = new FileReader("help_xsd.xsd"); 

     spf.setSchema(sf.newSchema(new SAXSource(new InputSource(fileReader)))); 
     //spf.setSchema(sf.newSchema(new Source[] {new StreamSource("help_xsd.xsd")})); 



     parser = spf.newSAXParser(); 

     MySAXHandler handler = new MySAXHandler(configReport); 

     parser.parse(new InputSource(new StringReader(helpData)), handler); 
     return true; 
    } 

我的XML

<Help date="2020-06-24"> 
<product 
id="en_US_SAN_15.0" 
label="orange_16.0" 
ProductName="orange 16.0 " 
productName="orange 16.0 Pre" 
productVersion="15.0" 
baseUrl="http://help.stage.xyz.com/" 
path="Help/en_US/" 
ionId="orange_product_xyzlr" 
ionCommentingAllowed="yes" 
ionSiteArea="help" 
ionRatingAllowed="yes" 
ionRatingType="thumbs" 
searchOptions="Community|xyz" 
searchDefault="Community" 
searchxyzRefinement="site=orange_V2_all" 
="yes" 
/> 
<package 
id="en_US_SAN_15.0_Using" 
label="orange_16.0" 
path="SAN/orange/15.0/Using" 
description="SAN 15.0" 
contextSensitivity="yes" 
downloadContent="client.orange_V2_Using_en-us.zip" 
downloadContentDefault="yes" 
downloadPdf="orange_V4_help.pdf" 
JUNKATTRIBUTE="JUNK" 
/> 
</Help> 

我的XSD

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> 
    <xs:element name="Help"> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element ref="product"/> 
     <xs:element ref="package"/> 
     </xs:sequence> 
     <xs:attribute name="appId" /> 
     <xs:attribute name="date" type="xs:date"/> 
     <xs:attribute name="locale" type="xs:NCName"/> 
     <xs:attribute name="pubId" /> 
     <xs:attribute name="version" type="xs:decimal"/> 
    </xs:complexType> 
    </xs:element> 
    <xs:element name="product"> 
    <xs:complexType> 
     <xs:attribute name="baseUrl" type="xs:anyURI"/> 
     <xs:attribute name="helpServiceUrl" type="xs:anyURI"/> 
     <xs:attribute name="id" type="xs:NCName"/> 
     <xs:attribute name="ionCommentingAllowed" type="xs:NCName"/> 
     <xs:attribute name="ionId" type="xs:NCName"/> 
     <xs:attribute name="ionRatingAllowed" type="xs:NCName"/> 
     <xs:attribute name="ionRatingType" type="xs:NCName"/> 
     <xs:attribute name="ionSiteArea" type="xs:NCName"/> 
     <xs:attribute name="label" /> 
     <xs:attribute name="multidomain" type="xs:NCName"/> 
     <xs:attribute name="path" /> 
     <xs:attribute name="productName" type="xs:NCName"/> 
     <xs:attribute name="productVersion" type="xs:NCName"/> 
     <xs:attribute name="searchxyzRefinement" /> 
     <xs:attribute name="searchBlueprintRefinement" type="xs:NCName"/> 
     <xs:attribute name="searchCommunityRefinement" type="xs:NCName"/> 
     <xs:attribute name="searchDefault" type="xs:NCName"/> 
     <xs:attribute name="searchOptions" /> 
    </xs:complexType> 
    </xs:element> 
    <xs:element name="package"> 
    <xs:complexType> 
     <xs:attribute name="alias" /> 
     <xs:attribute name="baseUrl" type="xs:anyURI"/> 
     <xs:attribute name="contextSensitivity" type="xs:NCName"/> 
     <xs:attribute name="deprecated" type="xs:NCName"/> 
     <xs:attribute name="description" /> 
     <xs:attribute name="downloadContent" /> 
     <xs:attribute name="downloadContentDefault" type="xs:NCName"/> 
     <xs:attribute name="downloadPdf" type="xs:NCName"/> 
     <xs:attribute name="helpmapPath" type="xs:anyURI"/> 
     <xs:attribute name="id" type="xs:NCName"/> 
     <xs:attribute name="label" /> 
     <xs:attribute name="packageGenerator" type="xs:NCName"/> 
     <xs:attribute name="path" /> 
     <xs:attribute name="urlParams" /> 
    </xs:complexType> 
    </xs:element> 
</xs:schema> 
+0

這聽起來我好像你的期望是不具備XML解析成功,如果它不是XML Schema有效;換句話說,如果XML無效,則parse()應該會引發異常;正確? – 2012-01-12 14:24:24

回答

1

的XML文件是有效的,如果:

  • 是公形成
  • 它符合與其相關聯的任何定義的模式類型。

在你的情況下,xml文檔格式正確,沒有與之相關的模式。所以它是有效的。

如果您想將xml與您定義的類型關聯,您需要使用一個名稱空間。

你的架構聲明:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="myNamespaceHere" xmlns="myNamespaceHere"> 
... 
</xs:schema> 

XML實例:

<Help date="2020-06-24" xmlns="myNamespaceHere"> 
... 
</Help> 

現在,當您進行驗證解析器會知道驗證對XSD。

UPDATE

正如評論所指出的彼得魯杜米特,這種有效性不是來自XSD有效性的觀點是正確的。名稱空間實際上並不是需要進行驗證的,但是在不需要名稱空間的情況下,應該將模式屬性elementFormDefault設置爲「不合格」,以指示模式中的類型可以在沒有名稱空間限定的情況下引用。

但是,XSD解析器通常會忽略沒有名稱空間的模式實例,這就是爲什麼從解析器的角度來看,非名稱空間的xml實例總是返回爲「有效」(只要其格式良好)。

+0

我很同意你的看法,但是如果OP再次嘗試命名空間限定的模式和模式實例,他們會發現解析器現在根據模式驗證XML,這是OP所面臨的最初問題。 – 2012-01-12 14:59:06

+0

感謝您考慮我的第一條評論。我會盡快將其刪除,以保持您的答案清潔。但是,請考慮如果沒有模式的目標命名空間(如您在生成的XSD中看到的那樣),則此上下文中的elementFormDefault值是無關緊要的。所以我傾向於認爲這不是問題,而是一種期望:解析(良構)與驗證。我所知道的大多數XML API,通過驗證轉換的解析始終會成功,因爲驗證錯誤在XML解析(格式良好)之外「被觀察」。 – 2012-01-12 15:49:40

+0

謝謝休。我無法控制xml。現在,我發現了這個問題。我沒有正確捕捉錯誤,並且設置了Validation = false幫助。 – Mady 2012-01-13 11:00:28

0

根據您提供您的XML應該得到驗證,對所提供的架構代碼,但是因爲你沒有包含定義MySAXHandler就很難知道你是否是處理錯誤發生時。

除致命錯誤外,其他驗證錯誤將通過調用DefaultHandler.error()方法報告SAXParseException報告。如果你想處理驗證錯誤,你需要實現該方法。喜歡的東西:

class MyHandler extends DefaultHandler { 
    public void error(SAXParseException exception) throws SAXParseException { 
     throw exception; 
    } 
} 

有了這個處理程序定義(與你的代碼的其餘部分一起)發生時,你應該看到parse拋出SAXParseException驗證異常。在ErrorHandler接口見

的更多信息:http://docs.oracle.com/javase/6/docs/api/org/xml/sax/ErrorHandler.html