gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 05:06:34 24 4
gpt4 key购买 nike

我是 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com