问题描述
我正在使用Jing API进行RelaxNG和xml验证.我无法验证我指定为Relaxng的String的长度.我已经在Java中编写了以下独立代码.在Relaxng文件中,我将minLength验证指定为3,在代码中,我传递了空字符串,但jing api不会抛出任何错误.字符串的验证没有发生错误.我正在使用Jing API.
I am Using Jing API for RelaxNG and xml Validation.I am Unable to Validate length of String which I have specified relaxng.I have Written the below standalone code in java.In the relaxng file i have specified minLength validation as 3 and in Code I am passing empty string still jing api is not throwing any error.The validation of string is not happing.I am using 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
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.)我程序的输出:
3.)OUTPUT of my Program :
<?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".
The way you had it, you had <param>
, which was not in the Relax NG namespace and thus was ignored. Also, you had minlength
but the parameter name is minLength
with a capital 'L'.
这篇关于Jing Relaxng字符串的长度验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!