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 houseangel <ro...@web.de> on 2007/03/29 09:25:23 UTC

Xerces Validation: No Namespaces

Hi everybody,

hope someone here can help me :)

I am currently trying to validate an XML document using Xerces 2.5.0 against
an XML Schema. My problem: I get the following error message:

TargetNamespace.2: Expecting no namespace, but the schema document has a
target namespace of 'XYZ'.

My customer tells me the XML file is valid and that my validator should tell
the same, even if there is some problem with my namespace stuff ;)

So currently I am trying to switch of the namespace validation. My code is:

/** Namespaces feature id (http://xml.org/sax/features/namespaces). */
protected static final String NAMESPACES_FEATURE_ID =
"http://xml.org/sax/features/namespaces";

/** Namespace prefixes feature id
(http://xml.org/sax/features/namespace-prefixes). */
protected static final String NAMESPACE_PREFIXES_FEATURE_ID =
"http://xml.org/sax/features/namespace-prefixes";

/** Validation feature id (http://xml.org/sax/features/validation). */
protected static final String VALIDATION_FEATURE_ID =
"http://xml.org/sax/features/validation";

/** Schema validation feature id
(http://apache.org/xml/features/validation/schema). */
protected static final String SCHEMA_VALIDATION_FEATURE_ID =
"http://apache.org/xml/features/validation/schema";

/** Schema full checking feature id
(http://apache.org/xml/features/valid...full-checking). */
protected static final String SCHEMA_FULL_CHECKING_FEATURE_ID =
"http://apache.org/xml/features/validation/schema-full-checking";

/** Dynamic validation feature id
(http://apache.org/xml/features/validation/dynamic). */
protected static final String DYNAMIC_VALIDATION_FEATURE_ID =
"http://apache.org/xml/features/validation/dynamic";

protected static final String SCHEMA_NONS_LOCATION_ID =
"http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation";

...

SAXParserFactory saxfactory = SAXParserFactory.newInstance();

saxfactory.setValidating(true);
saxfactory.setNamespaceAware(false);

SAXParser parser = saxfactory.newSAXParser();

XMLReader reader = parser.getXMLReader();

reader.setFeature(NAMESPACES_FEATURE_ID, false);
reader.setFeature(NAMESPACE_PREFIXES_FEATURE_ID, false);
reader.setFeature(VALIDATION_FEATURE_ID, true);
reader.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true);
reader.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, false);
reader.setFeature(DYNAMIC_VALIDATION_FEATURE_ID, false);

reader.setContentHandler(handler);
reader.setErrorHandler(handler);

reader.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
xsdFile.toURL().toString());

reader.parse(inp); 

Unfortunately this doesnt work and the validation exception concerning the
namespace is still handed over to my error handler. Does anybody have an
idea what I could do? 

Can I switch off the namespace awareness somehow?
Is it somehow possible to differ between this namespace exception and other
validation exceptions without dirty stuff like parsing the exception message
text?
-- 
View this message in context: http://www.nabble.com/Xerces-Validation%3A-No-Namespaces-tf3484801.html#a9728245
Sent from the Xerces - J - Users mailing list archive at Nabble.com.


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


Re: Xerces Validation: No Namespaces

Posted by Michael Glavassevich <mr...@ca.ibm.com>.
Hi,

XML schema validation requires namespace support. You need to set 
namespace awareness to true on the SAXParserFactory otherwise it won't 
work. And based on the error message you're getting, the schema document 
you've specified does have a target namespace so you should be setting the 
external-schemaLocation [1][2] property instead of the 
external-noNamespaceSchemaLocation one.

Thanks.

[1] 
http://xerces.apache.org/xerces2-j/properties.html#schema.external-schemaLocation
[2] http://marc.info/?l=xerces-j-dev&m=117431525725963&w=2

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org

houseangel <ro...@web.de> wrote on 03/29/2007 03:25:23 AM:

> 
> Hi everybody,
> 
> hope someone here can help me :)
> 
> I am currently trying to validate an XML document using Xerces 2.5.0 
against
> an XML Schema. My problem: I get the following error message:
> 
> TargetNamespace.2: Expecting no namespace, but the schema document has a
> target namespace of 'XYZ'.
> 
> My customer tells me the XML file is valid and that my validator should 
tell
> the same, even if there is some problem with my namespace stuff ;)
> 
> So currently I am trying to switch of the namespace validation. My code 
is:
> 
> /** Namespaces feature id (http://xml.org/sax/features/namespaces). */
> protected static final String NAMESPACES_FEATURE_ID =
> "http://xml.org/sax/features/namespaces";
> 
> /** Namespace prefixes feature id
> (http://xml.org/sax/features/namespace-prefixes). */
> protected static final String NAMESPACE_PREFIXES_FEATURE_ID =
> "http://xml.org/sax/features/namespace-prefixes";
> 
> /** Validation feature id (http://xml.org/sax/features/validation). */
> protected static final String VALIDATION_FEATURE_ID =
> "http://xml.org/sax/features/validation";
> 
> /** Schema validation feature id
> (http://apache.org/xml/features/validation/schema). */
> protected static final String SCHEMA_VALIDATION_FEATURE_ID =
> "http://apache.org/xml/features/validation/schema";
> 
> /** Schema full checking feature id
> (http://apache.org/xml/features/valid...full-checking). */
> protected static final String SCHEMA_FULL_CHECKING_FEATURE_ID =
> "http://apache.org/xml/features/validation/schema-full-checking";
> 
> /** Dynamic validation feature id
> (http://apache.org/xml/features/validation/dynamic). */
> protected static final String DYNAMIC_VALIDATION_FEATURE_ID =
> "http://apache.org/xml/features/validation/dynamic";
> 
> protected static final String SCHEMA_NONS_LOCATION_ID =
> 
"http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation";
> 
> ...
> 
> SAXParserFactory saxfactory = SAXParserFactory.newInstance();
> 
> saxfactory.setValidating(true);
> saxfactory.setNamespaceAware(false);
> 
> SAXParser parser = saxfactory.newSAXParser();
> 
> XMLReader reader = parser.getXMLReader();
> 
> reader.setFeature(NAMESPACES_FEATURE_ID, false);
> reader.setFeature(NAMESPACE_PREFIXES_FEATURE_ID, false);
> reader.setFeature(VALIDATION_FEATURE_ID, true);
> reader.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true);
> reader.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, false);
> reader.setFeature(DYNAMIC_VALIDATION_FEATURE_ID, false);
> 
> reader.setContentHandler(handler);
> reader.setErrorHandler(handler);
> 
> reader.setProperty("http://apache.
> org/xml/properties/schema/external-noNamespaceSchemaLocation",
> xsdFile.toURL().toString());
> 
> reader.parse(inp); 
> 
> Unfortunately this doesnt work and the validation exception concerning 
the
> namespace is still handed over to my error handler. Does anybody have an
> idea what I could do? 
> 
> Can I switch off the namespace awareness somehow?
> Is it somehow possible to differ between this namespace exception and 
other
> validation exceptions without dirty stuff like parsing the exception 
message
> text?
> -- 
> View this message in context: http://www.nabble.com/Xerces-
> Validation%3A-No-Namespaces-tf3484801.html#a9728245
> Sent from the Xerces - J - Users mailing list archive at Nabble.com.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
> For additional commands, e-mail: j-users-help@xerces.apache.org


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