Thanks for the reply George. Q1: How to specify "xmlns" as a property of SAXParse?
I though that maybe you could specify the default namespace of the XML instance as a property of the SAXParser itself. After all you can specify "http://apache.org/xml/properties/schema/external-schemaLocation" property and tell the parser where to find the XSD file for the given namespace. So, why can't I specify the "xmlns" part of the XML? Q2: What is the best way to write a Document to a file? My document is generated by another Java class. That class takes a Document and writes it out to a file. I was using the folloging piece of code: <BEGIN CODE> // this is Xerces-specific OutputFormat format = new OutputFormat(xmlDoc); format.setIndenting(true); XMLSerializer serializer = new XMLSerializer(out, format); //serializer.setNamespaces(true); serializer.serialize(xmlDoc); <END CODE> If the second to last line is uncommented, then namespaces of ALL elements in the Document are written in the file, which makes it really big. I haven't found a way to output a document to a file so that only the top XML element's namespace is written out. Any ideas? I also played with TransformerFactory trying to find another way to output a document to a file. Here is some code: <BEGIN CODE> DOMSource domSource = new DOMSource(xmlDoc); StreamResult streamResult = new StreamResult(out); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.transform(domSource, streamResult); <END CODE> With this approach I haven't found a way to force it to output namespaces at all. So, what is the right way to write a Document to a file preserving the namespace of the top XML element? I am sure that the code above and the resolution of the issues will help a lot of programmers. Please respond if you have any ideas. Thanks. Oleg -----Original Message----- From: George Cristian Bina [mailto:[EMAIL PROTECTED] Sent: Tuesday, November 22, 2005 11:41 AM To: [email protected] Subject: Re: validating XML with no xmlns Hi Oleg, Well, you are validating the document against the schema. As your document is not valid you get an error message. To have the document valid against the schema you must at least set the correct namespace for the root element, so add xmlns="http://mynamespace" to the root element. You should also enable the namespaces feature I think. Hope that helps, George -- George Cristian Bina <oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger http://www.oxygenxml.com Oleg Lebedev wrote: > Sorry, I accidentally sent incomplete email. Here is a full version: > > Greetings. > > I've been struggling with this problem for the last two days. I am > trying to validate an xml document, which does not have any xmlns > namespaces set. Here is an example: > > <?xml version="1.0" encoding="UTF-8"?> <data> > <report end-date="11/22/2005" name="My Report" start-date="12/31/1969"/> > <totals class-avg="0.0" stud-avg="94.0"/> </data> > > I have an XSD schema to which this document should conform to. I am > using sax parser to validate the document as follows: > > <BEGIN CODE> > > String ns = "http://mynamespace"; > String xsdUrl = "MySchema.xsd"; > SAXParserFactory spfactory = SAXParserFactory.newInstance(); > spfactory.setNamespaceAware(true); > spfactory.setValidating(true); > SAXParser parser = spfactory.newSAXParser(); > XMLReader xmlReader = parser.getXMLReader(); > > // Features > > xmlReader.setFeature("http://xml.org/sax/features/namespaces", false); > > xmlReader.setFeature("http://xml.org/sax/features/validation", true); > > xmlReader.setFeature("http://apache.org/xml/features/validation/schema > ", > true); > > xmlReader.setFeature("http://apache.org/xml/features/validation/schema-f ull-checking", > true); > > xmlReader.setFeature("http://apache.org/xml/features/validation/schema > ", > true); > > xmlReader.setFeature("http://xml.org/sax/features/xmlns-uris", true); > > // Properties > > xmlReader.setProperty("http://apache.org/xml/properties/schema/external- schemaLocation", > ns + " " + xsdUrl ); > > ... > > xmlReader.parse(xmlDocUrl); > > <END CODE> > > Note that MySchema.xsd file contains the schema with default namespace > "http://mynamespace" against which I am trying to validate the document. > > The last line in the code above always throws an exception: > > "org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration > of element 'data'." > > Could anybody tell me how to validate XML documents with no namespaces > specified against XSD schemas? > > Thanks. > > > ---------------------------------------------------------------------- > -- > *From:* Oleg Lebedev [mailto:[EMAIL PROTECTED] > *Sent:* Tuesday, November 22, 2005 10:49 AM > *To:* [email protected] > *Subject:* validating XML with no xmlns > > Greetings. > > I've been struggling with this problem for the last two days. I am > trying to validate an xml document, which does not have any xmlns > namespaces set. Here is an example: > > <?xml version="1.0" encoding="UTF-8"?> <data> > <report end-date="11/22/2005" name="My Report" start-date="12/31/1969"/> > <totals class-avg="0.0" stud-avg="94.0"/> </data> > > I have an XSD schema to which this document should conform to. I am > using sax parser to validate the document as follows: > > > > -- > This message has been scanned for viruses and dangerous content by > *MailScanner* <http://www.mailscanner.info/>, and is believed to be > clean. > If you have questions about this email, Please contact the IT Help > Desk. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean. If you have questions about this email, please contact the IT Help Desk. Mail --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
