"Bernd Kolb \([EMAIL PROTECTED])" <[EMAIL PROTECTED]> wrote on 01/09/2007
09:58:08 AM:
> Thanks Michael for your explanations.
>
> I have tried to add your remarks to my code,
> without success
>
> I am using JDK 1.4.2_13 with xerces 2.9.0
>
> -------------------------------------------------------------
>
> Here is the code:
>
> element.setAttribute("xmlns",
> "http://example.com/zedv/services/Logger/log");
> element.setAttribute("xmlns:xsi",
> XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
setAttribute() creates non-namespace-aware attribute nodes. You should be
using setAttributeNS() instead though as I said in a previous e-mail this
will not do what you think. Adding a namespace declaration to the tree has
no effect on the namespace URI of other nodes. To change the namespace URI
of the elements/attributes in the DOM you must use renameNode().
> DocumentBuilderFactory newInstance = DocumentBuilderFactory.
> newInstance();
> newInstance.setNamespaceAware(true);
This needs to be set on the DocumentBuilder which created the Document
you're importing from. Setting this here has no effect.
> Document doc2Validate = newInstance.newDocumentBuilder().newDocument();
>
> Element importNode = (Element) doc2Validate.importNode(element, true);
> doc2Validate.appendChild(importNode);
> doc2Validate.normalize();
>
> Schema schema = SchemaFactory.newInstance(
> XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(
> new StreamSource(this.getClass().
> getResourceAsStream("/LoggerLog.xsd")));
> Validator newValidator = schema.newValidator();
> newValidator.setErrorHandler(new ValidationErrorHandler());
> newValidator.validate(new DOMSource(doc2Validate));
>
> -------------------------------------------------------------
>
> The XSD looks like this:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
> xmlns="http://example.com/zedv/services/Logger/log"
> targetNamespace="http://example.com/zedv/services/Logger/log"
> elementFormDefault="qualified">
> <xs:element name="log" type="log"/>
> <xs:complexType name="log">
> <xs:attribute name="info" type="xs:string"/>
> </xs:complexType>
> </xs:schema>
>
> -------------------------------------------------------------
>
> The XML is here:
>
> <?xml version="1.0" encoding="UTF-8" ?>
> <log info="starting test"/>
>
> -------------------------------------------------------------
>
> I have added xerces 2.9.0 to the classpath an have run
> the code above with jdk 1.4.3_13
>
> The result was this exception
>
> org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration
> of element 'log'.
> at org.apache.xerces.util.ErrorHandlerWrapper.
> createSAXParseException(Unknown Source)
> at org.apache.xerces.util.ErrorHandlerWrapper.
> error(Unknown Source)
> at org.apache.xerces.impl.XMLErrorReporter.
> reportError(Unknown Source)
> at org.apache.xerces.impl.XMLErrorReporter.
> reportError(Unknown Source)
> at org.apache.xerces.impl.xs.XMLSchemaValidator.
> handleStartElement(Unknown Source)
> at org.apache.xerces.impl.xs.XMLSchemaValidator.
> startElement(Unknown Source)
> at org.apache.xerces.jaxp.validation.DOMValidatorHelper.
> beginNode(Unknown Source)
> at org.apache.xerces.jaxp.validation.DOMValidatorHelper.
> validate(Unknown Source)
> at org.apache.xerces.jaxp.validation.DOMValidatorHelper.
> validate(Unknown Source)
> at org.apache.xerces.jaxp.validation.ValidatorImpl.
> validate(Unknown Source)
> at javax.xml.validation.Validator.validate(Unknown Source)
> at XMLValidation.<init>(XMLValidation.java:58)
> at XMLValidation.main(XMLValidation.java:23)
> Exception in thread "main"
>
>
> Then I changed the jdk to 1.5_6 and the same code is working fine....
>
> Do you have any idea?
>
> Regards
>
> Bernd
>
> > -----Original Message-----
> > From: Michael Glavassevich [mailto:[EMAIL PROTECTED]
> > Sent: Tuesday, January 09, 2007 3:21 PM
> > To: [email protected]
> > Subject: RE: Add schema to xmldocument
> >
> > Each node in a DOM [1] carries its own namespace URI. Adding
> > a namespace declaration to the tree has no effect on the
> > namespace URI of other nodes.
> > If you want to change the namespace URI of a node (element or
> > attribute) you need to use renameNode() [2]. Also if you're
> > going to be validating this DOM against an XML schema it
> > needs to have been constructed from a namespace aware parser.
> > In other words you need to explicitly set namespace awareness
> > to true (docBuilderFactory.setNamespaceAware(true)) on the
> > instance of the DocumentBuilderFactory since the default
> > value is false.
> >
> > Once you've done all that there's no need to add the
> > xsi:schemaLocation attribute to the document. The validator
> > will work just fine without it (and in fact it will ignore it).
> >
> > [1] http://www.w3.org/DOM/faq.html#namespacefixup
> > [2]
> > http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.h
> tml#Document3-renameNode
> >
> > Michael Glavassevich
> > XML Parser Development
> > IBM Toronto Lab
> > E-mail: [EMAIL PROTECTED]
> > E-mail: [EMAIL PROTECTED]
> >
> > "Bernd Kolb \([EMAIL PROTECTED])" <[EMAIL PROTECTED]> wrote on 01/09/2007
> > 03:15:58 AM:
> >
> > >
> > > Yes, that's true.
> > >
> > > Is it somehow possible to add
> > >
> > > xmlns="http://sample.com"
> > > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> > >
> > > to an already parsed document?
> > > As I said, I tried to add these arguments to the documents root
> > > element, without success.
> > >
> > > Thanks again
> > >
> > > Regards
> > >
> > > Bernd Kolb
> > >
> > >
> > >
> > >
> > > > -----Original Message-----
> > > > From: Dick Deneer [mailto:[EMAIL PROTECTED]
> > > > Sent: Tuesday, January 09, 2007 8:19 AM
> > > > To: [email protected]
> > > > Subject: Re: Add schema to xmldocument
> > > >
> > > >
> > > > Apparently your schema simple.xsd has a targetnamespace http://
> > > > sample.com.
> > > > Your test.xml has no namespace decaration, so it is in the "null"
> > > > namespace and has to be validated against a schema thas
> > als has a
> > > > "null" targetnamespace.
> > > >
> > > > Either you must remove the targetnamespace in your xsd or
> > you have
> > > > to add xmlns="http://sample.com"
> > > > xmlns:xsi="http://www.w3.org/2001/
> > > > XMLSchema-instance" to your test.xml.
> > > >
> > > >
> > > > Regards
> > > > Dick Deneer
> > > >
> > > >
> > > >
> > > > Op 8-jan-2007, om 23:43 heeft Bernd Kolb (([EMAIL PROTECTED])) het
> > > > volgende
> > > > geschreven:
> > > >
> > > > > Hi,
> > > > >
> > > > > I have a question: I am using xerces 2.9.0.
> > > > >
> > > > > I have a XML-Document witch was not created by me and I am
> > > > not able to
> > > > > load it from a file. I am getting the XML-Document Object.
> > > > >
> > > > > Now I want to validate this document against a schema.
> > > > > This schema might not be in the document.
> > > > >
> > > > > So I want to add the schema programmatically.
> > > > >
> > > > > How can I achieve this?
> > > > >
> > > > >
> > > > > Document doc =
> > > > >
> > DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(ne
> > > > > w
> > > > > File("test.xml"));
> > > > >
> > > > > Schema schema =
> > > > > SchemaFactory.newInstance
> > > > > (XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(new
> > > > > StreamSource("simpleSchema.xsd"));
> > > > > schema.newValidator().validate(new DOMSource(doc));
> > > > >
> > > > >
> > > > > test.xml looks like this
> > > > >
> > > > > <?xml version="1.0" encoding="UTF-8"?> <persons age="0">
> > > > > <name>name</name>
> > > > > <surname>surname</surname>
> > > > > </persons>
> > > > >
> > > > > But I'd like xerces to act as if it looked like the
> > following one:
> > > > > <?xml version="1.0" encoding="UTF-8"?> <persons age="0"
> > > > > xmlns="http://sample.com"
> > > > > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> > > > > xsi:schemaLocation="http://sample.com simpleSchema.xsd ">
> > > > > <name>name</name>
> > > > > <surname>surname</surname>
> > > > > </persons>
> > > > >
> > > > > I tried to add the xmlns, xmlns:xsi and xsi:schemaLocation
> > > > attributes
> > > > > to the root element by code, without success...
> > > > >
> > > > >
> > > > > Any idea?
> > > > >
> > > > > Thanks a lot!
> > > > >
> > > > >
> > > > > Best wishes
> > > > >
> > > > > Bernd Kolb
> > > > >
> > > > >
> > > > >
> > > >
> > --------------------------------------------------------------------
> > > > -
> > > > > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > > > > For additional commands, e-mail: [EMAIL PROTECTED]
> > > > >
> > > > >
> > > > >
> > > >
> > > >
> > > >
> > --------------------------------------------------------------------
> > > > - To unsubscribe, e-mail: [EMAIL PROTECTED]
> > > > For additional commands, e-mail: [EMAIL PROTECTED]
> > > >
> > > >
> > >
> > >
> > >
> > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: [EMAIL PROTECTED]
E-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]