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 Predrag Knezevic <kn...@ipsi.fhg.de> on 2003/08/08 22:49:22 UTC

validation against schema that imports other namespaces

  Hi,

  I need to validate some XML documents against schema that imports some
other namespaces and use them to construct some more complex types that
include types from those namespace as well.

  Now, I simply fail in trying to validate document using standard JAXP
1.2 possibilities. I have try using:

factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "blabla.xsd");

and that works fine if my schema does not import any other namespace.
Otherwise, I get message that the parser is not able to find declaration
of document root element.

  Any idea? Thanks in advance,

Predrag


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


Re: validation against schema that imports other namespaces

Posted by Milan Trninic <mt...@galdosinc.com>.
Sorry, can't say much there. I undersand the problem now, but I haven't
dealt with it precisely. Wish you luck.

Milan
  ----- Original Message -----
  From: Predrag Knezevic
  To: xerces-j-user@xml.apache.org
  Sent: Friday, August 08, 2003 15:14
  Subject: Re: validation against schema that imports other namespaces


  > Are all necessary prefixes declared in both schemas?

    Yes. I have continued to try and here is what I have found:

    - validation works if one the schemas is referenced through
  schemaLocation in the document. The reference is absolute and really
  refer to the schema file. In all other cases thing does not work.

    This is not what I want; I simply want that schemaLocation attribute
  is not considered, if any value exist. The reason: during runtime, I
  would discover where the schema is located (somewhere in my classpath)
  and then I want to provide this information during validation to the
  parser. Therefore, I could move my application quite easy anywhere and
  not to change anything.

    Here is the code snippet:

  String SCHEMA_LANGUAGE =
        "http://java.sun.com/xml/jaxp/properties/schemaLanguage",
          XML_SCHEMA =
        "http://www.w3.org/2001/XMLSchema",
          SCHEMA_SOURCE =
        "http://java.sun.com/xml/jaxp/properties/schemaSource";
          try {
          DocumentBuilderFactory factory =
  DocumentBuilderFactory.newInstance();
          factory.setNamespaceAware(true);
          factory.setValidating(true);
          factory.setNamespaceAware(true);
          factory.setValidating(true);
          try
          {
           factory.setAttribute(SCHEMA_LANGUAGE,XML_SCHEMA);


factory.setAttribute(SCHEMA_SOURCE,ClassLoader.getSystemResourceAsStream("de
/fhg/ipsi/oasys/mgn/spec/test2.xsd"));
  }
            catch(IllegalArgumentException x)
            {
               System.err.println("Your DOM parser is not JAXP 1.2
  compliant.");
            }
          DocumentBuilder builder = factory.newDocumentBuilder();
          builder.setErrorHandler(new ErrorPrinter());

builder.parse(ClassLoader.getSystemResourceAsStream("de/fhg/ipsi/oasys/mgn/s
pec/test.xml"));
          } catch (Exception e) {e.printStackTrace();}

  and here are two schemas and a document:


  test.xsd:
  =========
  <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
              targetNamespace="http://xml.netbeans.org/examples/targetNS"
              xmlns="http://xml.netbeans.org/examples/targetNS"
              xmlns:ns2="http://xml.netbeans.org/examples/targetNS2"
              elementFormDefault="qualified">

  <xsd:import namespace="http://xml.netbeans.org/examples/targetNS2"
  schemaLocation="blatest2.xsd" />

  <xsd:element name="r" type="test" />

  <xsd:element name="r2" type="test" />


  <xsd:complexType name="test" >
      <xsd:sequence>
          <xsd:element name="rootns1" type="ns2:Root" />
      </xsd:sequence>
  </xsd:complexType>

  </xsd:schema>

  test2.xsd:
  ==========

  <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
              targetNamespace="http://xml.netbeans.org/examples/targetNS2"
              xmlns="http://xml.netbeans.org/examples/targetNS2"
              elementFormDefault="qualified">

  <xsd:element name="rootns2" type="Root" />

  <xsd:complexType name="Root" >
      <xsd:sequence>
          <xsd:element name="root" type="xsd:string" />
      </xsd:sequence>
  </xsd:complexType>

  </xsd:schema>

  test.xml
  ========

  <r xmlns='http://xml.netbeans.org/examples/targetNS'
     xmlns:ns2='http://xml.netbeans.org/examples/targetNS2'
    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
    xsi:schemaLocation='http://xml.netbeans.org/examples/targetNS

file:/usr/local/home/pedjak/projects/mgn/src/de/fhg/ipsi/oasys/mgn/spec/test
.xsd'>
  <rootns1>
      <ns2:root>bla </ns2:root></rootns1>
  </r>


  =======================

    I have tried to provide to factory.setAttribute an array of two
  InputStreams, but it does not help.

    Still any idea? Is is possible to do that under JAXP 1.2?

  Predrag


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



Re: validation against schema that imports other namespaces

Posted by Predrag Knezevic <kn...@ipsi.fhg.de>.
> Are all necessary prefixes declared in both schemas? 

  Yes. I have continued to try and here is what I have found:

  - validation works if one the schemas is referenced through
schemaLocation in the document. The reference is absolute and really
refer to the schema file. In all other cases thing does not work.

  This is not what I want; I simply want that schemaLocation attribute
is not considered, if any value exist. The reason: during runtime, I
would discover where the schema is located (somewhere in my classpath)
and then I want to provide this information during validation to the
parser. Therefore, I could move my application quite easy anywhere and
not to change anything.

  Here is the code snippet:

String SCHEMA_LANGUAGE =
      "http://java.sun.com/xml/jaxp/properties/schemaLanguage",
        XML_SCHEMA =
      "http://www.w3.org/2001/XMLSchema",
        SCHEMA_SOURCE =
      "http://java.sun.com/xml/jaxp/properties/schemaSource";
        try {
        DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setValidating(true);
        factory.setNamespaceAware(true);
        factory.setValidating(true);
        try
        {
         factory.setAttribute(SCHEMA_LANGUAGE,XML_SCHEMA);

factory.setAttribute(SCHEMA_SOURCE,ClassLoader.getSystemResourceAsStream("de/fhg/ipsi/oasys/mgn/spec/test2.xsd"));
}
          catch(IllegalArgumentException x)
          {
             System.err.println("Your DOM parser is not JAXP 1.2
compliant.");
          }
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setErrorHandler(new ErrorPrinter());
builder.parse(ClassLoader.getSystemResourceAsStream("de/fhg/ipsi/oasys/mgn/spec/test.xml"));
        } catch (Exception e) {e.printStackTrace();}

and here are two schemas and a document:


test.xsd:
=========
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://xml.netbeans.org/examples/targetNS"
            xmlns="http://xml.netbeans.org/examples/targetNS"
            xmlns:ns2="http://xml.netbeans.org/examples/targetNS2"
            elementFormDefault="qualified">

<xsd:import namespace="http://xml.netbeans.org/examples/targetNS2"
schemaLocation="blatest2.xsd" />

<xsd:element name="r" type="test" />

<xsd:element name="r2" type="test" />


<xsd:complexType name="test" >
    <xsd:sequence>
        <xsd:element name="rootns1" type="ns2:Root" />
    </xsd:sequence>
</xsd:complexType>

</xsd:schema>

test2.xsd:
==========

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://xml.netbeans.org/examples/targetNS2"
            xmlns="http://xml.netbeans.org/examples/targetNS2"
            elementFormDefault="qualified">

<xsd:element name="rootns2" type="Root" />

<xsd:complexType name="Root" >
    <xsd:sequence>
        <xsd:element name="root" type="xsd:string" />
    </xsd:sequence>
</xsd:complexType>

</xsd:schema>

test.xml
========

<r xmlns='http://xml.netbeans.org/examples/targetNS'
   xmlns:ns2='http://xml.netbeans.org/examples/targetNS2'
  xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
  xsi:schemaLocation='http://xml.netbeans.org/examples/targetNS
file:/usr/local/home/pedjak/projects/mgn/src/de/fhg/ipsi/oasys/mgn/spec/test.xsd'>
<rootns1> 
    <ns2:root>bla </ns2:root></rootns1>
</r>


=======================

  I have tried to provide to factory.setAttribute an array of two
InputStreams, but it does not help. 

  Still any idea? Is is possible to do that under JAXP 1.2?

Predrag


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


Re: validation against schema that imports other namespaces

Posted by Milan Trninic <mt...@galdosinc.com>.
Are all necessary prefixes declared in both schemas?

Milan
  ----- Original Message -----
  From: Predrag Knezevic
  To: xerces-j-user@xml.apache.org
  Sent: Friday, August 08, 2003 13:49
  Subject: validation against schema that imports other namespaces


    Hi,

    I need to validate some XML documents against schema that imports some
  other namespaces and use them to construct some more complex types that
  include types from those namespace as well.

    Now, I simply fail in trying to validate document using standard JAXP
  1.2 possibilities. I have try using:


factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage
", "blabla.xsd");

  and that works fine if my schema does not import any other namespace.
  Otherwise, I get message that the parser is not able to find declaration
  of document root element.

    Any idea? Thanks in advance,

  Predrag


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