gpt4 book ai didi

java - Jing Relaxng 字符串长度验证

转载 作者:行者123 更新时间:2023-11-30 04:02:11 26 4
gpt4 key购买 nike

我正在使用 Jing API 进行 RelaxNG 和 xml 验证。我无法验证我指定的松弛字符串的长度。我用java编写了以下独立代码。在relaxng文件中,我已将minLength验证指定为3,并且在代码中我传递空字符串,但jing api没有抛出任何错误。字符串的验证没有发生。我正在使用Jing API。

我的代码:

 public static void main(String[] args) throws JAXBException, IOException, SAXException, IncorrectSchemaException, TransformerConfigurationException, TransformerException, TransformerFactoryConfigurationError {
OBJ006 obj006 = new OBJ006();
obj006.setMyInput("");
JAXBContext jc = JAXBContext.newInstance(OBJ006.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
JAXBElement<OBJ006> rootElement =
new JAXBElement<OBJ006>(new QName("obj006"),
OBJ006.class,
obj006);
StringWriter xmlFile = new StringWriter();
marshaller.marshal(rootElement, xmlFile);
String encodedxml = new String(xmlFile.toString().getBytes());
System.out.println(encodedxml);

//Get Schema
File file = new File("C:/XML/Cardinalities/myRelaxNG.xml");
FileInputStream fileInput = new FileInputStream(file);
InputSource schemaSource = new InputSource(fileInput);

SchemaReader schemaReader = new AutoSchemaReader();
Schema schema = schemaReader.createSchema(schemaSource, PropertyMap.EMPTY);
ErrorHandler errorHandler = new ErrorHandlerImpl();
PropertyMapBuilder builder = new PropertyMapBuilder();
builder.put(ValidateProperty.ERROR_HANDLER, errorHandler);

Validator validator = schema.createValidator(builder.toPropertyMap());

TransformerFactory.newInstance().newTransformer()
.transform(new StreamSource(new ByteArrayInputStream(encodedxml.getBytes())),
new SAXResult(validator.getContentHandler()));






}

2.MyRelaxNG.xml

<?xml version="1.0"?>
<!-- Converted to RelaxNG using http://debeissat.nicolas.free.fr/XSDtoRNG.php -->
<rng:grammar xmlns:rng="http://relaxng.org/ns/structure/1.0"
ns="" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
<rng:start combine="choice">
<rng:ref name="obj006" />
</rng:start>
<rng:define name="obj006">
<rng:element name="obj006">
<rng:ref name="obj006ref" />
</rng:element>
</rng:define>
<rng:define name="obj006ref">
<rng:element name="myInput">
<rng:data type="string">
<param name="minlength">3</param>
</rng:data>
</rng:element>


</rng:define>

</rng:grammar>

3.)我的程序的输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<obj006>
<myInput></myInput>
</obj006>

最佳答案

您需要像这样指定参数:

<rng:param name="minLength">3</rng:param>

按照你的方式,你有 <param> ,它不在 Relax NG 命名空间中,因此被忽略。另外,你有minlength但参数名称是 minLength大写“L”。

关于java - Jing Relaxng 字符串长度验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21705713/

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