OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.


Help: OASIS Mailing Lists Help | MarkMail Help



   Re: [xml-dev] Node Level Validation against XML schema

[ Lists Home | Date Index | Thread Index ]

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> ...");
        // 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();
        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]);
                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());
        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.
        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: [xml-dev] 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@CraneSoftwrights.com
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>


News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS