Getting Parser to ignore SchemaLocation hint in XML

Discussions

XML & Web services: Getting Parser to ignore SchemaLocation hint in XML

  1. Hello

    I have an application than receives XML documents from an external source. The XML document typically has a "SchemaLocation" hint, which tells the parser where to find the XML Schema (XSD) to use for validation purposes. However, for various reasons, I do not want the Parser to use the SchemaLocation hint given within the XML file as it refers to a URL that my application cannot access (i.e. it refers to a loction accessible only by the sending location. So my Quesitons:

    1) How can I get SAX Parser to ignore the SchemaLocation hint in the XML document?

    2) When I parse the XML with SAX, how can I tell the SAX parser to use the XSD from another location (e.g. my c drive)?

    3) Is the situation any different if I use DOM?

    Thanks
  2. With DTD based documents and SAX one could do this (i.e. 1 and 2) using the EntityResolver interface. However, I don't know if that also works for Schema.

    Regards,
    Stefan
  3. I have tried this for Schemas. The Entity Resolver trick works there as well (at least, it does for the Xerces parser ...)
  4. Hi Paul

    Thanks for responding - Could you possibly provide me a link to some sample code that will provide this functionality via the EntityResolver mechanism?

    Thanks
  5. http://sax.sourceforge.net/apidoc/org/xml/sax/EntityResolver.html

    Regards, Stefan
  6. I wrote up some code the last time I talked about this:

    http://www.theserverside.com/discussions/thread.jsp?thread_id=22525

    The example uses the classpath to load the scheme. At the time, I had only done this trick with DTDs, but I tested it with schemas afterwards. The problems the other developer was having were related to malformed schemas, not the EntityResolver.
  7. Are you using Xerces-J?[ Go to top ]

    Are you using Xerces-J 2.4.x, 2.5.x, or 2.6.x ?

    If yes, you can take advantage of Xerces-J properties

    http://xml.apache.org/xerces-j/properties.html

    Property: http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation

    Property: http://apache.org/xml/properties/schema/external-schemaLocation
  8. Are you using Xerces-J?[ Go to top ]

    Ooo! Those are nice. I was not aware of this trick.

    The EntityResolver trick is still useful, though, if you want to load the XSD from a location that cannot be identified by a URL (such as the classpath).
  9. Xerces-J 2.6.1[ Go to top ]

    Also...the Apache XML Commons Resolver is now included in Xerces-J 2.6.1

    http://xml.apache.org/commons/components/resolver/index.html