Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 针对xsd的Xml验证将错误的Xml视为有效_Java_Xml_Xsd - Fatal编程技术网

Java 针对xsd的Xml验证将错误的Xml视为有效

Java 针对xsd的Xml验证将错误的Xml视为有效,java,xml,xsd,Java,Xml,Xsd,我有java代码,可以根据xsd验证我的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_SCHEM

我有java代码,可以根据xsd验证我的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是:在根目录和rob/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>


显然,您正在使用
LokalniAifoStavType
中的
xs:extension
进行扩展?如果元素指定其类型为
LokalniAifoType
,那么它的任何扩展类型都可以出现在XML中。

您显然是在使用
LokalniAifoStavType
中的
xs:extension
进行扩展?如果元素指定其类型为LokalniAifoType,然后它的任何扩展类型都可以出现在XML中。

请向我们展示
reg:LokalniAifoType
ns2:LokalniAifoStavType
类型的定义。您可以在xsd中找到它,但好的,我将它添加到这个问题中,请向我们展示
reg:LokalniAifoType
的定义,以及
ns2:LokalniAifoStavType
types。您可以在xsd中找到它,但我可以将其添加到此问题中。请向我们展示
reg:LokalniAifoType
ns2:LokalniAifoStavType
类型的定义。您可以在xsd中找到它,但我可以将其添加到此问题中,但为什么?在xsd中定义为父级。我不想在这里接受扩展的child这就像Java继承——“可替换性”——child可以在使用父级的任何上下文中使用。这并不能回答这个问题。要评论或要求作者澄清,请在他们的帖子下方留下评论。@MuertoExcobito-我这里没有批评任何东西;只是解释一下为什么会这样。要完全理解它,xsd OP需要阅读xsd扩展等。我不能键入其他地方的内容页面中包含的详细信息作为答案。希望你能理解。我已经编辑了我的答案,以澄清问题。但为什么?在xsd中定义为父级。我不想在这里接受扩展的child这就像Java继承——“可替换性”——child可以在使用父级的任何上下文中使用。这并不能回答这个问题。要评论或要求作者澄清,请在他们的帖子下方留下评论。@MuertoExcobito-我这里没有批评任何东西;只是解释一下为什么会这样。要完全理解它,xsd OP需要阅读xsd扩展等。我不能键入其他地方的内容页面中包含的详细信息作为答案。希望你能理解。我已经编辑了我的答案,以澄清问题。但为什么?在xsd中定义为父级。我不想在这里接受扩展的child这就像Java继承——“可替换性”——child可以在使用父级的任何上下文中使用。这并不能回答这个问题。要评论或要求作者澄清,请在他们的帖子下方留下评论。@MuertoExcobito-我这里没有批评任何东西;只是解释一下为什么会这样。要完全理解它,xsd OP需要阅读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>
<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>