java - 无法使用 XSD 架构验证 XML

标签 java xml xsd sax

我是 XML 验证新手。

我的 XSD 是

<xsd:complexType name="RootForm">
    <xsd:sequence>
        <xsd:element name="TRADE" type="RecordForm" minOccurs="0" maxOccurs="unbounded"/>
    </xsd:sequence>

    <xsd:attribute name="ASOF_DATE" use="required">
        <xsd:simpleType>
            <xsd:restriction base="xsd:string">
                <xsd:pattern value="[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}"/>
            </xsd:restriction>
        </xsd:simpleType>
    </xsd:attribute>
    <xsd:attribute name="CREATE_DATE" use="required">
        <xsd:simpleType>
            <xsd:restriction base="xsd:string">
                <xsd:pattern value="[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}"/>
            </xsd:restriction>
        </xsd:simpleType>
    </xsd:attribute>
    <xsd:attribute name="RECORDS" type="xsd:integer" use="required"/>
</xsd:complexType>

我运行的代码是:

SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setNamespaceAware(true);
    spf.setValidating(true);
    InputSource i = new InputSource("X:/workspace/XMLValidation/src/xml/trades.xml");
    InputSource i1 = new InputSource("X:/workspace/XMLValidation/src/xml/transactions.xsd");
    SAXParser saxParser = spf.newSAXParser();
    saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage",  "http://www.w3.org/2001/XMLSchema");
    saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", i1);
    XMLReader xmlReader = saxParser.getXMLReader();
   // xmlReader.setContentHandler(new SimpleErrorHandler());
    xmlReader.setErrorHandler(new SimpleErrorHandler());

    try {
        xmlReader.parse(i);
    } catch (IOException e) {
        e.printStackTrace();
    }

我得到以下异常:

src-resolve.4.2: Error resolving component 'RootForm'. It was detected that 'RootForm' is in namespace 'http://www.w3schools.com', but components from this namespace are not referenceable from schema document 'file:///X:/workspace/XMLValidation/src/xml/transactions.xsd'. If this is the incorrect namespace, perhaps the prefix of 'RootForm' needs to be changed. If this is the correct namespace, then an appropriate 'import' tag should be added to 'file:///X:/workspace/XMLValidation/src/xml/transactions.xsd'.
   Public ID: null
   System ID: file:///X:/workspace/XMLValidation/src/xml/transactions.xsd
   Line number: 6
   Column number: 52
   Message: src-resolve.4.2: Error resolving component 'RootForm'. It was detected that 'RootForm' is in namespace 'http://www.w3schools.com', but components from this namespace are not referenceable from schema document 'file:///X:/workspace/XMLValidation/src/xml/transactions.xsd'. If this is the incorrect namespace, perhaps the prefix of 'RootForm' needs to be changed. If this is the correct namespace, then an appropriate 'import' tag should be added to 'file:///X:/workspace/XMLValidation/src/xml/transactions.xsd'.
cvc-elt.1: Cannot find the declaration of element 'TRANSACTIONS'.

任何人都可以帮我解决我在 xsd 文件中做错的事情吗

谢谢 阿夫尼什

最佳答案

您能分享一下您正在解析的 XML 文件吗? 此外,Trade 元素采用的“RecordForm”类型未在架构中定义。 尝试将其包括在内,然后看看。 应导入您正在使用的命名空间以区分 XML 中的元素。

关于java - 无法使用 XSD 架构验证 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12578262/

相关文章:

xml - XSD - 可以为空的枚举限制值

java - 使用 FXML 将鼠标单击操作事件添加到 JavaFX 中的标签

c# - 从 XML 填充类对象

java - JAXB 检查 xml 对于声明的 xsd 是否有效

namespaces - XSD 导入命名空间

Python:在保留实体的同时解析 XML 文档

java - Warbler 不使用 warble war 打包文件

java - 如何快速查看Java String上的各种字母包含情况?

java - 在 DocumentBuilder 解析方法中使用字符串(使用 XPath 解析 XML 时需要它)

mysql - 将 XML 导入 mySQL,多个节点级别具有相同的属性名称