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 Lingzhi Zhang <lz...@cse.ogi.edu> on 2004/04/15 00:42:57 UTC

validate xml data fragment agaist schema

Hi,

I am wondering how to validate a xml data fragment against XML schema
using SAX in Xerces. For example, I have two schema schemaA.xsd, and
schemaB.xsd, and the XML document is like,

<A>
    ...
    <B>
	...
    </B>

    ...
</A>

The parser first validates the document agains schemaA.xsd; when the
parser sees the start tag <B> it uses schemaB.xsd; when parser sees the
end tag </B>, it goes back to use schemaA.xsd.

I thought about switching ContentHandler, however, I didn't figure out
how to direct SAX data stream from one to the other. Any advices. Thanks a
lot in advance.

Lingzhi


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


Re: URI to validate XML Schema

Posted by Jeff Greif <jg...@alumni.princeton.edu>.
----- Original Message ----- 
From: "Robert van Loenhout" <r....@greenvalley.nl>
To: <xe...@xml.apache.org>
Sent: Friday, April 16, 2004 7:17 AM
Subject: Re: URI to validate XML Schema


> Jeff Greif wrote:
> > > It expects an URI such as file:///c:/test.xsd
> > >
> > > Is there a bug in Java or Xerces?
> >
> > Older versions of Xerces allowed the form produced by Java, but
somewhere
> > around 2.2.x, the Xerces maintainers decided that the authority
component
> > was required and had some good argument for doing so.
>
> Do you know what were these good arguments?
...
I suggest searching the archives of this mailing list.
Jeff


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


Re: URI to validate XML Schema

Posted by Robert van Loenhout <r....@greenvalley.nl>.
Jeff Greif wrote:
> > It expects an URI such as file:///c:/test.xsd
> >
> > Is there a bug in Java or Xerces?
>
> Older versions of Xerces allowed the form produced by Java, but somewhere
> around 2.2.x, the Xerces maintainers decided that the authority component
> was required and had some good argument for doing so.

Do you know what were these good arguments?

For now I'll do it like this to get a Xerces compiant URI

            URI schemaURI = new File(schema).toURI();
            schemaURI = new
URI(schemaURI.getScheme(),"",schemaURI.getPath(),null,null);
            parser.setProperty(XercesConst.SCHEMA_NONS_LOCATION_ID,
schemaURI.toString());





                                                                              
                                                                              
                                                                               
                                                                            

                
                                                                 

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


Re: URI to validate XML Schema

Posted by Jeff Greif <jg...@alumni.princeton.edu>.
----- Original Message ----- 
From: "Robert van Loenhout" <r....@greenvalley.nl>
To: <xe...@xml.apache.org>
Sent: Friday, April 16, 2004 1:23 AM
Subject: URI to validate XML Schema


...
> When I have a java File object I can use the toURI() method.
> For example the output of the following code is file:/c:/test.xsd

> System.out.println(new File("c:\\test.xsd").toURI().toString());
>
> Calling the toURL() gives the same result.
>
> However Xerces does not think this is a good URI when setting the Schema
> Location with this value.
> It expects an URI such as file:///c:/test.xsd
>
> Is there a bug in Java or Xerces?

According to my reading of RFC 2396, to which java.net.URI' s documentation
refers, it appears that Java is correct and Xerces incorrect in disallowing
what it produces.  My interpretation may be wrong, though.

The Javadoc says that such absolute hierarchical uri's are of the form
    scheme:[//authority][path][?query][#fragment]
where //authority may be absent (as may any other component, except the
scheme).

The RFC agrees, saying that for hierarchical uri's with an absolute path,
that an absolute hierarachichal uri is of the form
  scheme ":" ( net-path | abs-path) [ "?" query ]
where net-path = "//" authority [ abs-path ]
and abs-path = "/" path-segments

Note that the difference of interpretation between Java and Xerces boils
down to whether it is "//"authority which is optional, or just authority.

Older versions of Xerces allowed the form produced by Java, but somewhere
around 2.2.x, the Xerces maintainers decided that the authority component
was required and had some good argument for doing so.

Jeff


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


URI to validate XML Schema

Posted by Robert van Loenhout <r....@greenvalley.nl>.
Hi,

When I try to validate an XML document I have to set the Schema Location
property of the parser
with an URI as the value.
I have a question about this URI value.

When I have a java File object I can use the toURI() method.
For example the output of the following code is file:/c:/test.xsd

System.out.println(new File("c:\\test.xsd").toURI().toString());

Calling the toURL() gives the same result.

However Xerces does not think this is a good URI when setting the Schema
Location with this value.
It expects an URI such as file:///c:/test.xsd

Is there a bug in Java or Xerces? Or is there just something I don't
understand?
Can I use some other method to construct a Xerces URI from a file object?

Thanks for any help,
Robert.



                                                                              
                                                                              
                                                                               
                                                                            

                
                                                                 

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


Re: validate xml data fragment agaist schema

Posted by Martin Vysny <vy...@sozo.fns.uniba.sk>.
Lingzhi Zhang wrote:

>Hi,
>
>I am wondering how to validate a xml data fragment against XML schema
>using SAX in Xerces. For example, I have two schema schemaA.xsd, and
>schemaB.xsd, and the XML document is like,
>
><A>
>    ...
>    <B>
>	...
>    </B>
>
>    ...
></A>
>
>The parser first validates the document agains schemaA.xsd; when the
>parser sees the start tag <B> it uses schemaB.xsd; when parser sees the
>end tag </B>, it goes back to use schemaA.xsd.
>
>I thought about switching ContentHandler, however, I didn't figure out
>how to direct SAX data stream from one to the other. Any advices. Thanks a
>lot in advance.
>
>  
>
I assume that you're using SAXParser which does the validation job. I'm 
afraid that it validates the document before it invokes the 
contentHandler methods hence you cannot switch its 'internal' 
contenthandler. Maybe you should define another namespace for B and pray 
that SAXParser supports divided validation :-). However you can use 
Sun's MSV as a XMLFilter (good examples are in the example/ directory in 
the msv zip file). But be aware that you may need to validate this B 
element against both schemas - the schemaA may want to see the B element 
(for example with empty content only) - this depends on the definition 
of the schema.
Martin Vysny

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