Hi Fabio,

You aren't setting namespace awareness to true on the 
DocumentBuilderFactory. Both schema validation and XInclude require 
namespace support. Assuming your schema actually contains an element 
declaration for "NFe", I imagine setting namespace awareness would make it 
work.

Note that you're not actually using Apache Xerces. 
com.sun.org.apache.xerces.* classes are part of Sun's three year old fork 
off the Xerces codebase. It has bugs which are unique to it and others 
which have long been fixed in Apache so you may run into problems which 
users here have never experienced (and never will experience).

Thanks.

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: [EMAIL PROTECTED]
E-mail: [EMAIL PROTECTED]

"Fabio Retzlaff" <[EMAIL PROTECTED]> wrote on 04/10/2007 
03:46:23 PM:
 
> Hi,
> 
> I'm trying to validate a xml using a schema with include but I get this
> error:
> 
> org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of
> element 'NFe'.
>    at
> com.sun.org.apache.xerces.internal.jaxp.validation.Util.toSAXParseExcept
> ion(Unknown Source)
>    at
> com.sun.org.apache.xerces.internal.jaxp.validation.ErrorHandlerAdaptor.e
> rror(Unknown Source)
> 
> Here is my code:
> 
>    public Document validate (String message, String[] schemas)
> throws Exception {
> 
> System.setProperty("javax.xml.validation.SchemaFactory:http://www.w3.org
> /2001/XMLSchema",
> "com.sun.org.apache.xerces.internal.jaxp.validation.xs.SchemaFactoryImpl
> ");
> 
> System.setProperty("org.w3c.dom.DOMImplementationSourceList","com.sun.or
> g.apache.xerces.internal.dom.DOMXSImplementationSourceImpl");
>       //try {
> 
>         DOMParser dp = new DOMParser();
> 
> dp.setFeature("http://apache.org/xml/features/nonvalidating/load-externa
> l-dtd",false);
> 
> dp.setFeature("http://xml.org/sax/features/validation",false);
> 
> dp.setFeature("http://apache.org/xml/features/validation/schema",false);
>          dp.parse(new InputSource(new StringReader(message)));
>          Document doc = dp.getDocument();
>          Element de = doc.getDocumentElement();
>          NamedNodeMap nnm = de.getAttributes();
> 
>          Attr att1 = doc.createAttribute("xmlns:xsi");
> 
> att1.setValue("http://www.w3.org/2001/XMLSchema-instance";);
>          nnm.setNamedItem(att1);
> 
>          Node n =
> nnm.getNamedItem("xsi:noNamespaceSchemaLocation");
>          if (n !=null) nnm.removeNamedItemNS(n.getNamespaceURI(),
> n.getLocalName());
>          ByteArrayOutputStream baos = new ByteArrayOutputStream();
>          XMLSerializer ser = new XMLSerializer(baos,new
> OutputFormat());
>          ser.serialize(doc);
>          baos.close();
> 
>          message = new String(baos.toByteArray());
> 
>          DocumentBuilderFactory docFactory =
> DocumentBuilderFactory.newInstance();
>          docFactory.setXIncludeAware(true);
>          DocumentBuilder parser = docFactory.newDocumentBuilder();
>          Document document = parser.parse(new InputSource(new
> StringReader(message)));
> 
>          SchemaFactory factory =
> SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
>          Source[] sources = new Source[schemas.length];
>          for (int i = 0; i < sources.length; i++) {
>             StreamSource s = new StreamSource(new
> ClassPathResource(schemas[i]).getInputStream());
>          sources[i] = s;
>       }
>          Schema schema = factory.newSchema(sources);
>          Validator validator = schema.newValidator();
>          validator.validate(new DOMSource(document));
>          return document;
>    }
> 
> There is anything wrong ??
> 
> Fabio
> 
> ---------------------------------------------------------------------
> 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]

Reply via email to