2015-06-17 54 views
1

我有一个验证我的XML对XSD的Java代码:XML验证接受错误的XML作为有效

public static void main(String[] args) throws Exception { 
    Source xmlFile = new StreamSource(new File("src/main/resources/file.xml")); 

    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
    Schema schema = schemaFactory.newSchema(new Source[] { 
      new StreamSource(new File("src/main/resources/RegTypy.xsd")), 
      new StreamSource(new File("src/main/resources/XopInclude.xsd")), 
      new StreamSource(new File("src/main/resources/RobTypy.xsd")), 
      new StreamSource(new File("src/main/resources/RobDotazyData.xsd")), }); 
    Validator validator = schema.newValidator(); 
    try { 
     validator.validate(xmlFile); 
     System.out.println(xmlFile.getSystemId() + " is valid"); 
    } catch (SAXException e) { 
     System.out.println(xmlFile.getSystemId() + " is NOT valid"); 
     System.out.println("Reason: " + e.getLocalizedMessage()); 
    } 
} 

XSD下载是here:在根目录下,并在抢劫

现在/ XSD目录我的XML看起来像:

<data:RobCtiAifoData xmlns:ns2="urn:cz:isvs:rob:schemas:RobTypy:v1" xmlns:data="urn:cz:isvs:rob:schemas:RobDotazyData:v1" xmlns:reg="urn:cz:isvs:reg:schemas:RegTypy:v1"> 
    <data:Aifo stav="spravny" xsi:type="ns2:LokalniAifoStavType" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">2</data:Aifo> 
    <data:VyuzitiPoskytnuti>vyuziti</data:VyuzitiPoskytnuti> 
</data:RobCtiAifoData> 
从XSD

重要的部分是:

<xs:complexType name="RobCtiAifoDataType"> 
    <xs:annotation> 
     <xs:documentation xml:lang="cs">Čtení referenčních údajů podle AIFO.</xs:documentation> 
    </xs:annotation> 
    <xs:sequence> 
     <xs:element name="Aifo" type="reg:LokalniAifoType" /> 
     <!-- omezení - jenom využití nebo poskytnutí --> 
     <xs:element name="VyuzitiPoskytnuti" type="rob:TypVyuzitiPoskytnutiType" /> 
    </xs:sequence> 
    <xs:attribute name="znepristupniLog" type="xs:boolean" /> 
</xs:complexType> 

正如你所看到的,Aifo应该有reg:LokalniAifoType类型,但是在xml中它被覆盖到ns2:LokalniAifoStavType,它具有额外的参数stav。问题是为什么这种类型在xml中被接受,为什么它是有效的xml。

UPDATE:

<xs:complexType name="LokalniAifoType"> 
    <xs:annotation> 
     <xs:documentation xml:lang="cs">Lokální identifikátor AIFO. Klíč typu integer.</xs:documentation> 
    </xs:annotation> 
    <xs:simpleContent> 
     <xs:extension base="xs:int"> 
      <xs:attribute name="prevodAifoStatus" type="PrevodAifoStatusType"> 
       <xs:annotation> 
        <xs:documentation xml:lang="cs">Informace o výsledku převodu v ORG, pokud se nepodařilo přeložit.</xs:documentation> 
       </xs:annotation> 
      </xs:attribute> 
      <xs:attribute name="stavOvereniAifo" type="xs:boolean"> 
       <xs:annotation> 
        <xs:documentation xml:lang="cs">Existence AIFO se má ověřit v ROB.</xs:documentation> 
       </xs:annotation> 
      </xs:attribute> 
     </xs:extension> 
    </xs:simpleContent> 
</xs:complexType> 


<xs:complexType name="LokalniAifoStavType"> 
    <xs:annotation> 
     <xs:documentation xml:lang="cs">Agendový identifikátor fyzické osoby včetně stavu a času 
      poslední změny. 
     </xs:documentation> 
    </xs:annotation> 
    <xs:simpleContent> 
     <xs:extension base="reg:LokalniAifoType"> 
      <xs:attribute name="stav" type="reg:StavType" /> 
      <xs:attribute name="zmenaCas" type="ZmenaCasType" /> 
     </xs:extension> 
    </xs:simpleContent> 
</xs:complexType> 
+0

请向我们展示'reg:LokalniAifoType'和'ns2:LokalniAifoStavType'类型的定义。 – potame

+0

你可以在xsd中找到它,但是我也可以将其添加到这个问题 – hudi

回答

0

您在使用LokalniAifoStavType显然xs:extension延伸?如果一个元素指定了它的类型LokalniAifoType,那么它的任何扩展类型都可以出现在XML中。

+0

但是为什么?在xsd中定义了父项。我不想在这里接受扩展孩子 – hudi

+0

这就像Java继承 - “可替代性” - 孩子可以在使用父母的任何环境中使用。 –

+0

这不提供问题的答案。要批评或要求作者澄清,请在其帖子下方留言。 – MuertoExcobito