【问题标题】:XSD Validation Error in JAVAJAVA 中的 XSD 验证错误
【发布时间】:2012-01-13 00:21:09
【问题描述】:

我正在尝试使用以下代码从给定 XSD 文件中验证我的 XML 文件,

        Source xmlFile = new StreamSource(fXmlFile);
        SchemaFactory schemaFactory = SchemaFactory
        .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = schemaFactory.newSchema(new File("presentation.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 文件上传到这里给你看:http://orhancanceylan.com/stack/presentation.xsd

但是当我运行我的代码时,我得到了这个错误:

org.xml.sax.SAXParseException: s4s-att-not-allowed: Attribute 'maxOccurs' cannot appear in element 'element'.

什么问题,我应该怎么解决?

【问题讨论】:

    标签: java xml validation xsd


    【解决方案1】:

    您的架构中有错误,全局表示元素不能有约束 (maxOccurs="1" minOccurs="1"),您需要删除它们(全局的最小值/最大值没有意义元素,这些约束在其他元素的上下文中是有意义的)。 XSD Primer 中的更多信息:http://www.w3.org/TR/2004/REC-xmlschema-0-20041028/#Globals

    【讨论】:

    • +1 - 这个限制的原因是因为任何全局元素都是实例文档中的潜在根元素
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多