[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Node Level Validation against XML schema


verify xml against schema
Validating a complete XML document is OK. What I want is to validate a node. Here is my TEST program ( modified from example program DOMVerifier.java):
 
    public static void main(String[] args) throws Exception {
        if (args.length < 2) {
            System.out.println("Usage: DOMVerifier <schema> <instance> ...");
            return;
        }
 
        // setup JARV and compile a schema.
        VerifierFactory factory = new com.sun.msv.verifier.jarv.TheFactoryImpl();
        Verifier verifier = factory.compileSchema(args[0]).newVerifier();
        // instead, you can call factory.newVerifier(args[0])
        // this will result in the same behavior.
 
        // setup JAXP
        DocumentBuilderFactory domf = DocumentBuilderFactory.newInstance();
        domf.setNamespaceAware(true);
        DocumentBuilder builder = domf.newDocumentBuilder();
 
        for (int i = 1; i < args.length; i++) {
            // parse a document into a DOM.
            Document dom = builder.parse(new File(args[i]));
 
            // performs the validation on the whole tree.
            // instead, you can pass an Element to the verify method, too.
            // e.g.,  verifier.verify(dom.getDocumentElement())
            if (verifier.verify(dom))
                System.out.println("valid  :" + args[i]);
            else
                System.out.println("invalid:" + args[i]);
 
            // or you can pass an Element to validate that subtree
 
 
    //////////////////////// problems here ////////////////////////////////////////////////
            //No, this does not work
 
            //For example, if you try the following code
            // you always get
            //tag name xxxxx is not allowed. Possible tag names are: ...
 

            validateAllChildren(verifier, dom);
 
        }
    }
 
 public static void validateAllChildren(Verifier verifier, Node node) throws Exception {
 
        System.out.println("getLocalName()=" + node.getLocalName());
        System.out.println("getNodeName()=" + node.getNodeName());
        System.out.println("getNodeType()=" + node.getNodeType());
        System.out.println("");
 
        try {
            if (verifier.verify(node)) {
                // ...
                System.out.println(node.getNodeName() + " is valid");
            }
            else {
                System.out.println(node.getNodeName() + " is not valid");
            }
        }
        catch (SAXException e) {
            //e.printStackTrace();  //To change body of catch statement use Options | File Templates.
            System.out.println("\n"+e.getMessage()+"\n");
        }
        System.out.println("-----------------------------------");
 
        NodeList nodes = node.getChildNodes();
 
        for (int j = 0; j < nodes.getLength(); j++) {
 
            Node n0 = nodes.item(j);
 
            validateAllChildren(verifier, n0);
 
        }

    }
----- Original Message -----
Sent: 04 March 2004 15:07
Subject: Re: Node Level Validation against XML schema

At 2004-03-04 15:01 +0000, Albert Yu wrote:
>I am looking for a Node Level Validation( against XML schema) package. I
>tried MSV from Sun. Its documentation says it can do node level
>validation, but actually it do not work, it can only validate root element.

Check again ... I use MSV to validate complete XML documents against their
schemas.  Perhaps you are invoking the software incorrectly or
misinterpreting the messages that you see.

................. Ken

--
US XSL training: Washington,DC March 15; San Francisco,CA March 22
World-wide on-site corporate, government & user group XML training
G. Ken Holman                 mailto:gkholman@C...
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/x/
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Breast Cancer Awareness  http://www.CraneSoftwrights.com/x/bc


-----------------------------------------------------------------
The xml-dev list is sponsored by XML.org <http://www.xml.org>, an
initiative of OASIS <http://www.oasis-open.org>

The list archives are at http://lists.xml.org/archives/xml-dev/

To subscribe or unsubscribe from this list use the subscription
manager: <http://www.oasis-open.org/mlmanage/index.php>


PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.