开发者

Problem in XML Validation. MIn occurs is not working properly

I have a sample code in which i am trying to validate a xml using xml validation method. And it works correctly too except for minOccurs.I have given the code below. Please help me to find my mistake.

XSD File (Live.xsd):-

            <?xml version="1.0" encoding="UTF-8"?>

             <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">

                    <xsd:element name="Test" />

                    <xsd:complexType name="Test">
                        <xsd:sequence>
                        <xsd:element ref="player" />
                    </xsd:sequence> 
                    </xsd:complexType>


                    <xsd:element name="player" >

                    <xsd:complexType>
                        <xsd:sequence minOccurs="2">
                          <xsd:element name="name" type="xsd:string"/>
                          <xsd:element name="address">
                                  <xsd:complexType>
                                    <xsd:sequence>
                                      <xsd:element name="houseno" type="xsd:int" />
                                      <xsd:element name="street" type="xsd:string" />
                                    </xsd:sequence>
                                  </xsd:complexType>
                                </xsd:element>
                        </xsd:sequence>
                        <xsd:attribute name="id" type="xsd:string" />
                      </xsd:complexType>    
                      </xsd:element>


            </xsd:schema>

Xml file (example.xml) :-

                <?xml version="1.0" encoding="UTF-8"?>

                <Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="Live.xsd" >

                    <player id="1">
                        <name>Owen</name>
                        <address>
                            <houseno>10</houseno>
                            <street>downing hill</street>       
                        </address>
                    </player&开发者_开发知识库gt;

                </Test> 

Java method :-

                                 private void validate(File xml) {
                        try {
                            url = new URL(xsd.toURI().toString());//xsd
                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                        }


                        source = new StreamSource(xml); //xml
                        try {
                            System.out.println(url);
                            schema = schemaFactory.newSchema(url);
                        } catch (SAXException e) {
                            e.printStackTrace();
                        }
                        validator = schema.newValidator();
                        System.out.println(xml);
                        try {
                            validator.validate(source);
                        } catch (SAXException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

I tried giving minOccurs=2 inside < xsd element ref="player" /> too but didint work


Remember that the default value for maxOccurs is 1. You're allowing a minimum of 2 and a maximum of 1 ...

Try <xsd:sequence minOccurs="2" maxOccurs="unbounded">

Edit: careful with 'unbounded' though; you'd want to set it to an acceptable maximum without giving anyone the chance of bombarding your system with a gazillion of nodes.

Edit2: provided the xsd (corrected with maxOccurs) and xml above, this code will output "Validation failed!!!":

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

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;
import org.xml.sax.SAXParseException;

public class XMLValidate {

static File xsd;
static File xml;
static URL url;
static StreamSource source;
static SchemaFactory schemaFactory;
static Schema schema;
static Validator validator;

/**
 * @param args
 */
public static void main(String[] args) {
    xml = new File("example.xml");
    xsd = new File("Live.xsd");

    try {
        url = new URL(xsd.toURI().toString());// xsd
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    source = new StreamSource(xml); // xml
    try {
        //System.out.println(url);
        schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        schema = schemaFactory.newSchema(url);
    } catch (SAXException e) {
        e.printStackTrace();
    }
    validator = schema.newValidator();
    //System.out.println(xml);
    try {
        validator.validate(source);
        System.out.println("Validation succesful!");
    } catch (SAXParseException e) {
        System.out.println("Validation failed!!!");
        //e.printStackTrace(); -- uncomment for detailed info on validation failing
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

If you change the code to validate the following example2.xml, it will output "Validation succesful!":

<?xml version="1.0" encoding="UTF-8"?>

<Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<player id="1">
    <name>Owen</name>
    <address>
        <houseno>10</houseno>
        <street>downing hill</street>
    </address>
    <name>Maggy</name>
    <address>
        <houseno>10</houseno>
        <street>downing hill</street>
    </address>
</player>

</Test>

Your xsd with the minOccurs=2 forces you to have the sequence name-address twice in the xml. Not sure if that's what you were looking for in the first place; it strikes me as odd but I don't have insight in the requirements.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜