xerces-j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Sandy Gao" <sandy...@ca.ibm.com>
Subject Re: Antwort: programmatically resolving namespaces to local schemas
Date Tue, 29 Apr 2003 13:38:19 GMT
> it seems that bar.xsd isn't actually read by the parser

Guess you meant "foo.xsd". You are right. It's not read. There is a bug in
the parser. I'll fix it soon.

> Secondly, I noticed that although I've specified that I want schema
> validation enabled (with the
> "http://apache.org/xml/features/validation/schema" feature), I can
> modify bar child elements (but not the bar element itself) to be
> anything and have parsing still succeed as long as I don't resolve the
> schemaLocation URLs.

You also need to enable validation, by turning on this feature (please
refer to the feature page [1]):

http://xml.org/sax/features/validation

[1] http://xml.apache.org/xerces2-j/features.html#validation.schema

Hope this helps,
Sandy Gao
Software Developer, IBM Canada
(1-905) 413-3255
sandygao@ca.ibm.com



                                                                                         
                                       
                      Anders Kristensen                                                  
                                       
                      <akristensen@dynam        To:       xerces-j-dev@xml.apache.org 
                                          
                      icsoft.com>               cc:                                   
                                          
                                                Subject:  Re: Antwort: programmatically resolving
namespaces to local schemas    
                      04/25/2003 07:25                                                   
                                       
                      PM                                                                 
                                       
                      Please respond to                                                  
                                       
                      xerces-j-dev                                                       
                                       
                                                                                         
                                       
                                                                                         
                                       




thomas2.maesing@bgs-ag.de wrote:
> Hi,
>
> just try the setEntityResolver Method of the XMLReader or the
> DocumentBuilder.
> It works also for XML Schemas.

Thanks, that should probably have been obvious to me. Might be
worthwhile adding an FAQ item anyway, though.

Of course, I'm still running into problems. When resolving an HTTP
system URL specified with xsd:schemaLocation to a local .xsd file, the
parser seems either not to read the xsd InputSource I return from my sax
EntityResolver, or else the semantics changes somehow. The xsd file
contains a declaration of a named model group

The following is my attempt to produce a minimal example that
illustrates the problem. I have two schemas with the same
targetNamespace: foo.xsd specifies common stuff (a single named model
group, "DescriptionGroup") while bar.xsd includes foo.xsd and defines a
"bar" root element whose content model refers to the DescriptionGroup.
The foobar.xml instance conforms to the bar/foo schemas and specifies a
schemaLocation pointing to the bar.xsd resource (a file: URL).

Now, when I parse *without* resolving the file: URL, I get no parse
error but when I resolve the file: URL to a local resource (using
Class.getResourceAsStream to find the xsd file relative to the
classpath), I get the following error:

[Error] :13:48: src-resolve: Cannot resolve the name
'foo:DescriptionGroup' to a (n) group component.

When I look at last accessed times for the local xsd files, it seems
that bar.xsd isn't actually read by the parser even though I returned an
InputStream for it in my EntityResolver:

$ java FooBarParser foobar.xml
resolved entity 'file:/c:/foobar/bar.xsd' to '/bar.xsd'
resolved entity 'file:/c:/foobar/foo.xsd' to '/bar.xsd'
[Error] :13:48: src-resolve: Cannot resolve the name
'foo:DescriptionGroup' to a (n) group component.
startElement: bar       [http://dynamicsoft.com/xml/ns/foo]
startElement: description       [http://dynamicsoft.com/xml/ns/foo]
startElement: comment   [http://dynamicsoft.com/xml/ns/foo]
$ ls -lu *xsd
-rwxrwxrwx    1 65535    mkgroup       625 Apr 25 19:05 bar.xsd
-rwxrwxrwx    1 65535    mkgroup       452 Apr 25 18:49 foo.xsd

So it seems that for some reason the parser just isn't reading foo.xml
when I resolve the <xsd:include> schemaLocation.  Is there some way to
get the parser to emit debug or trace information to get more info on
the processing it's performing?

Secondly, I noticed that although I've specified that I want schema
validation enabled (with the
"http://apache.org/xml/features/validation/schema" feature), I can
modify bar child elements (but not the bar element itself) to be
anything and have parsing still succeed as long as I don't resolve the
schemaLocation URLs.


I've attached the foo.xsd and bar.xsd schemas and the foobar doc
instance as well as the Java class used for parsing. The schemaLocation
file: URLs assume all is in a single directory c:/foobar. Running
FooBarParser with a -Dnoresolve system property disables the entity
resolver.

Any help *much* appreciated. Trying to figure this stuff out can be
pretty frustrating ;)

Anders
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://dynamicsoft.com/xml/ns/foo"
            xmlns:foo="http://dynamicsoft.com/xml/ns/foo"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:group name="DescriptionGroup">
    <xsd:sequence>
        <xsd:element name="description" type="xsd:string"/>
        <xsd:element name="comment" type="xsd:string"/>
    </xsd:sequence>
</xsd:group>

</xsd:schema>
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://dynamicsoft.com/xml/ns/foo"
            xmlns:foo="http://dynamicsoft.com/xml/ns/foo"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:include schemaLocation="file:/c:/foobar/foo.xsd"/>

<xsd:element name="bar" type="foo:BarType" />

<xsd:complexType name="BarType">
    <xsd:sequence>
        <xsd:group ref="foo:DescriptionGroup"/>
        <xsd:element name="distributable" type="xsd:string" minOccurs="0"/>
    </xsd:sequence>
</xsd:complexType>

</xsd:schema>
<?xml version="1.0" encoding="UTF-8"?>

<bar xmlns='http://dynamicsoft.com/xml/ns/foo'
     xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
     xsi:schemaLocation='http://dynamicsoft.com/xml/ns/foo
                         file:/c:/foobar/bar.xsd'>

  <description>description goes here</description>
  <comment>comment goes here</comment>
</bar>
import java.io.*;
import java.util.*;
import org.xml.sax.Attributes;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.helpers.DefaultHandler;
import org.apache.xerces.parsers.SAXParser;

public class FooBarParser extends DefaultHandler {
    private static final String VALIDATION_SCHEMA =
        "http://apache.org/xml/features/validation/schema";

    public static void main(String[] args) throws Exception {
        FileInputStream in = new FileInputStream(args[0]);

        SAXParser saxParser = new SAXParser();
        saxParser.setFeature(VALIDATION_SCHEMA, true);
        saxParser.setEntityResolver(new FooBarEntityResolver());
        saxParser.setContentHandler(new FooBarParser());
        saxParser.parse(new InputSource(in));
    }

    public void startElement(String uri,
                             String localName,
                             String qName,
                             Attributes atts)
        throws SAXException
    {
        System.out.println("startElement: " + localName + "\t[" + uri +
"]");
    }
}

class FooBarEntityResolver implements EntityResolver {
    public InputSource resolveEntity(String publicId, String systemId) {
        String loc = null;

        if (System.getProperty("noresolve") != null) return null;

        if ("file:/c:/foobar/bar.xsd".equals(systemId)) {
            loc = "bar.xsd";
        } else if ("file:/c:/foobar/foo.xsd".equals(systemId)) {
            loc = "foo.xsd";
        }

        if (loc != null) {
            try {
                java.net.URL url =
FooBarEntityResolver.class.getResource(loc);
                System.out.println("resource: " + url);

                InputStream in =
                    FooBarEntityResolver.class.getResourceAsStream(loc);
                if (in != null) {
                    System.out.println("resolved entity '" + systemId +
                                       "' to '" + loc + "'");
                    return new InputSource(in);
                }
            } catch (Exception ex) {
                ex.printStackTrace(System.err);
            }
        }

        return null;
    }
}

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



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


Mime
View raw message