You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Subbu Iyer <su...@gmail.com> on 2005/02/04 03:33:54 UTC

Re: XML Validation of root node

Thanks guys, for helping out. 

I find that setting the Feature
"http://apache.org/xml/features/validation/dynamic" to True causes
this problem to happen. If I keep this flag to false, the incorrect
root node is properly identified. If the flag is True, it does not
catch the incorrect root node.

Any idea why this happens? 


Here's the code: 

        SAXParser par = new SAXParser ();
        par.setErrorHandler(new ParserErrorHandler()); //
ParserErrorHandler and ParserEntityResolver are trivial classes..
nothing significant there..
        par.setEntityResolver(new ParserEntityResolver());
        try
        {
            par.setFeature("http://xml.org/sax/features/validation", true);
            par.setFeature("http://apache.org/xml/features/validation/schema",
true);
            par.setFeature("http://apache.org/xml/features/validation/dynamic",
true); // This is the problem!!

            FileInputStream fis = new FileInputStream
("D:/cdcatalog_invalid.xml");
            ProtectedInputStream pis = new ProtectedInputStream(fis);
            InputSource is = new InputSource (pis);
            par.parse(is);
        }
        catch (Exception e)
        { }

	public class ProtectedInputStream extends InputStream {
	    private InputStream is;
	    public ProtectedInputStream(InputStream is) {
	        this.is = is;
	    }
	    public int read() throws IOException {
	        return is.read();
	    }
	    public void close() { // Overridden close function to prevent
Xerces from closing my stream
	    }
	}


Thanks, 
Subbu. 


On Wed, 26 Jan 2005 15:13:45 -0700, Wax, Ed <Ed...@csgsystems.com> wrote:
>  
> 
> Subbu, 
> 
> I have a Xerces-based (J-2.6.2) validator.  It would appear my validation
> runs differ from yours.  
> 
> Validating the <catalog_abc123 ...> instance document shown below (which you
> state is OK by Xerces): 
> 
> command line is: -p org.apache.xerces.parsers.XML11Configuration -s
> ../temp/cdcatalog.xsd -i ../temp/cdcatalog1.xml 
> setting parser configuration ... 
> parser configuration is: org.apache.xerces.parsers.XML11Configuration 
> setting schema support ... 
> adding schema: ../temp/cdcatalog.xsd to the list ... 
> adding instance file: ../temp/cdcatalog1.xml to the list ... 
> registering the XML_SCHEMA grammar ... 
> parsing ../temp/cdcatalog.xsd ... 
> ... done (schema conforms to W3C spec) 
> Validating XML instance documents ... 
> Validating ../temp/cdcatalog1.xml ... 
> [Error] cdcatalog1.xml:2:47: cvc-elt.1: Cannot find the declaration of
> element 'catalog_abc123'. 
> ... found some error(s) 
> 
> Validating the <catalog ...> instance document shown below (which you state
> is in err by Xerces): 
> 
> command line is: -p org.apache.xerces.parsers.XML11Configuration -s
> ../temp/cdcatalog.xsd -i ../temp/cdcatalog2.xml 
> setting parser configuration ... 
> parser configuration is: org.apache.xerces.parsers.XML11Configuration 
> setting schema support ... 
> adding schema: ../temp/cdcatalog.xsd to the list ... 
> adding instance file: ../temp/cdcatalog2.xml to the list ... 
> registering the XML_SCHEMA grammar ... 
> parsing ../temp/cdcatalog.xsd ... 
> ... done (schema conforms to W3C spec) 
> Validating XML instance documents ... 
> Validating ../temp/cdcatalog2.xml ... 
> ... done (instance document is VALID) 
> 
> Regards, 
> Ed 
>  
> 
> -----Original Message----- 
> From: Subbu Iyer [mailto:subbdi@gmail.com] 
> Sent: Wednesday, January 26, 2005 2:33 PM 
> To: xerces-j-user@xml.apache.org 
> Subject: XML Validation of root node 
> 
> 
>  
>  
> 
> Hi, 
> 
> I am trying to validate the following XML using Xerces 2.5 (also tried 
> with latest 2.6 version). I notice that if the root node is incorrect, 
> the message still gets validated successfully without any errors. 
> However, if any element within the document is incorrect, the XML is 
> reported to be invalid. 
> 
> This is the original correct XML doc. 
> 
> <catalog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
> xsi:noNamespaceSchemaLocation="cdcatalog.xsd"> 
>         <cd> 
>                 <title>Empire Burlesque</title> 
>                 <artist>Bob Dylan</artist> 
>                 <country>USA</country> 
>                 <company>Columbia</company> 
>                 <price>10.90</price> 
>                 <year>1985</year> 
>         </cd> 
> </catalog> 
> 
> This following XML is NOT reported by Xerces to be invalid though the 
> root element is incorrect. 
> 
> <catalog_abc123 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
> xsi:noNamespaceSchemaLocation="cdcatalog.xsd"> 
>         <cd> 
>                 <title>Empire Burlesque</title> 
>                 <artist>Bob Dylan</artist> 
>                 <country>USA</country> 
>                 <company>Columbia</company> 
>                 <price>10.90</price> 
>                 <year>1985</year> 
>         </cd> 
> </catalog_abc123> 
>  
> 
> However, the following XML is correctly reported to be invalid. 
> 
> <catalog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
> xsi:noNamespaceSchemaLocation="cdcatalog.xsd"> 
>         <cd> 
>                 <bad_title>Empire Burlesque</bad_title> <!-- Invalid element
> --> 
>                 <artist>Bob Dylan</artist> 
>                 <country>USA</country> 
>                 <company>Columbia</company> 
>                 <price>10.90</price> 
>                 <year>1985</year> 
>         </cd> 
> </catalog> 
>  
> 
> The XSD (cdcatalog.xsd) is attached with this mail. 
> 
> Any ideas on what could be wrong? 
> 
> Thanks, 
> Subbu. << File: cdcatalog.xsd >>  << File: ATT34260.txt >>

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org