gpt4 book ai didi

java - 带 Java validator 的 XSD "minOccurs"未按预期工作

转载 作者:行者123 更新时间:2023-12-01 09:35:19 25 4
gpt4 key购买 nike

我正在努力将 XSD validator 功能添加到我在工作中创建的工具中。该工具实际上会生成格式非常严格的 XML。 XML 的格式在映射文档中定义,并且还有一个 XSD 架构。

我想要的是让用户能够快速验证他们在工具中创建的 XML。我已经添加了进行此验证的所有代码。不幸的是,我遇到了一些意想不到的问题。

具体而言,XML 的一个部分包含大约 20 个标签。这些标记并不总是出现在 XML 文件中,许多标记是可选的。我需要验证来忽略可选标签(如果它们不存在) - 相反,即使我将“Minoccurrs”值设置为 0, validator 也会返回错误。

这是我的 validator 代码:

package misc;

import java.io.File;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.xml.XMLConstants;

import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.xml.sax.SAXException;


public class XMLValidation {


public static JFileChooser uploadFile;


public static boolean validateXMLSchema(String xsdPath, String xmlPath){

try{

SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

Schema schema = factory.newSchema(new File(xsdPath));
Validator validator = schema.newValidator();
validator.validate(new StreamSource(new File(xmlPath)));


}catch(IOException | SAXException e){


JOptionPane.showMessageDialog(null, e.getMessage());

return false;

}

JOptionPane.showMessageDialog(null, "No errors detected!");

return true;

}

public static void uploadFile(){

boolean uploadApproval = false;

while(uploadApproval==false){//While upload approval has not been given..

JFileChooser chooser = new JFileChooser();//Creates a new object of the JFileChooser class.

uploadFile = chooser;//Saves the upload file variable as the chooser response.

FileNameExtensionFilter filter = new FileNameExtensionFilter("XML Files", "xml");
//Sets the allowed file formats for upload.

chooser.setFileFilter(filter);//Activates the created file filter.

chooser.setDialogTitle("Select a Camt54 file to validate");//Sets the title bar text.

//Completes once the user clicks ok.
int returnVal = chooser.showOpenDialog(chooser);//
if(returnVal == JFileChooser.APPROVE_OPTION){
uploadApproval=true;
}else{
System.exit(0);
}

}
}

}

出于安全原因,我无法共享整个 XSD 架构。我试图拿出一部分来说明我的意思:

<xs:complexType name="CntsRecord1">
<xs:sequence>
<xs:element maxOccurs="1" minOccurs="0" name="test1" type="ISODateTime"/>
<xs:element maxOccurs="1" minOccurs="0" name="test2" type="ISODateTime"/>
<xs:element maxOccurs="1" minOccurs="0" name="test3" type="ISODateTime"/>
<xs:element maxOccurs="1" minOccurs="0" name="test4" type="type4"/>
<xs:element maxOccurs="1" minOccurs="0" name="test5" type="Max35Text"/>
<xs:element maxOccurs="1" minOccurs="0" name="test6" type="Max140Text"/>
<xs:element maxOccurs="1" minOccurs="0" name="test7" type="type5"/>
<xs:element maxOccurs="1" minOccurs="0" name="Test" type="Max140Text"/>

</xs:sequence>
</xs:complexType>

如您所见,所有这些值的“minOccurs”均设置为 0,因此它们不应返回错误。

一旦我运行此代码,我将收到一个错误,指出需要元素“Test”。 (假设所有其他人都在场)。

使用 xsd 运行验证并使用最小值 0 是否存在一些困难?

这是我从 validator 返回的错误:

cvc-complex-type.2.4.a:Invalid content was found starting with element 'Test10'. One of {"urn:iso:std:iso:20022:tech:xsd:camt:054:001:04:"Test}' is expected.

这是我的 XML 文件中的示例:

-<Cnts>

<Test1>2016-08-18T09:51:41</Test1>

<Test2>2016-08-18T09:51:41</Test2>

<Test3>2016-08-18T09:51:41</Test3>

<Test4>PAPER</Test4>

<Test5>2016-08-18T09:51:41</Test1>

<Test6>2016-08-18T09:51:41</Test2>

<Test7>2016-08-18T09:51:41</Test3>

<Test10>PAPER</Test4>

任何帮助将不胜感激。

最佳答案

首先,您的 XML 和 XSD 在 test 元素的大小写上不一致,并且您的 XML 格式不正确。

但是,假设这些问题仅仅是您尝试清理敏感数据以创建可以安全发布的示例的结果,您的实际问题可以归结为理解此错误消息:

cvc-complex-type.2.4.a: Invalid content was found starting with element 'Test10'. One of {"urn:iso:std:iso:20022:tech:xsd:camt:054:001:04:"Test}' is expected.

是的,由于 Test 是用 minOccurs="0" 声明的,因此它确实是可选的。该错误并没有说明其他情况。意思是解析器在成功解析 Test7 后遇到了一个元素,并且该元素不是它预期的。它期望Test7后面的元素是Test,因为根据您的XSD,这是唯一可能(不是必须)出现在那里。

关于java - 带 Java validator 的 XSD "minOccurs"未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39024954/

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