Jing Relaxng length Validation for String

风格不统一 提交于 2019-12-11 05:52:02

问题


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.

My Code:

 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.)OUTPUT of my Program :

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

回答1:


You need to specify your parameter like this:

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

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'.



来源:https://stackoverflow.com/questions/21705713/jing-relaxng-length-validation-for-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!