Discussions

XML & Web services: validating xml with xsd NOT USING xsi:noNamespaceSchemaLocation

  1. Hi everyone, I successfully validated this xml: <!--?xml version="1.0" encoding="UTF-8"?--> with this xsd: <!--?xml version="1.0" encoding="UTF-8"?--> and the code: SAXBuilder saxBuilder = new SAXBuilder("org.apache.xerces.parsers.SAXParser", true); saxBuilder.setFeature("http://apache.org/xml/features/validation/schema", true); Document document = saxBuilder.build(new File("/home/anthony/workspace/MartService/test.xml")); Now I'm trying to do the same without using "noNamespaceSchemaLocation" (same xsd): <!--?xml version="1.0" encoding="UTF-8"?--> I've tried everything and can't make the following code work: SAXBuilder saxBuilder = new SAXBuilder("org.apache.xerces.parsers.SAXParser", true); saxBuilder.setFeature("http://apache.org/xml/features/validation/schema", true); //saxBuilder.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation", "/home/anthony/workspace/00MartConfigurator/XML/test.xsd"); // tried with and without this line, doesn't change Document document = saxBuilder.build(new File("/home/anthony/workspace/MartService/test2.xml")); the error message i get is: "Cannot find the declaration of element 'test:orders'." I found about a million examples of such a validation using "noNamespaceSchemaLocation" and none as simple as is ^_^' Any help would be greatly appreciated!! :) Thanks, Anthony
  2. I think your xml and xsd should be like this: XML doc ======== <!--?xml version="1.0" encoding="UTF-8"?--> XSD doc ======= <!--?xml version="1.0" encoding="UTF-8"?--> Thanks Meena
  3. Hi Meena, Thanks a lot for your help, really appreciated. What you suggested did work pretty well, however: 1. I still don't understand why it's necessary to have a targetNamespace, why can't the XSD file itself suffice? 2. In the XML doc (as suggested by you), if i put instead of , it complains about it, any idea why? 3. It seems if I add more schemas, it works but the elements are not validated.. for instance if I add xmlns:other="http://www.myotherschema.com", then whatever tag like will be accepted even if they don't comply to the schema Anyway, at least i'm not stuck anymore ^_^ thanks again! Anthony
  4. You are most welcome, anthony. 1. I still don't understand why it's necessary to have a targetNamespace, why can't the XSD file itself suffice? -- targetNamespace is to place elements and attributes from the default namespace into a different namespace. 2. In the XML doc (as suggested by you), if i put instead of , it complains about it, any idea why? --- To specify whether the locally declared elements and attributes of the schema should appear qualified by a namespace, either explicitly by using a prefix or implicitly by default, we can use the elementFormDefault and attributeFormDefault attributes on the element to globally specify the qualification of local elements and attributes, In this case, it would be NOW should work. 3. It seems if I add more schemas, it works but the elements are not validated.. for instance if I add xmlns:other="http://www.myotherschema.com", then whatever tag like will be accepted even if they don't comply to the schema -- You need to use to use multiple schemas. Pls find the following url for more details: http://www.xfront.com/HideVersusExpose.html Thanks Meena