Hi,

I have successfully read in a grammar (from my understanding that would be
nothing more than a XML schema file) and am able to get a list of the global
elements, complex types etc. but I have not been able to figure out how to
actually look at the contents of a particular element or complex type? I
checked out the FAQ as follows and it provides an inadequate amount of info
to really do what the FAQ question asks:

<<< From Xerces Caching & Preparsing Grammars FAQ begin
>>>>>>>>>>>>>>>>>>>>>>>>>>>
But I don't want to "preparse" grammars for efficiency; I want to parse them
in order to look at their contents using some API! Can I do this?


Yes, for grammar types for which such an API is defined. No such API exists
at the current moment for DTD's. For XML Schemas, there is a Xerces-specific
schema component model API. For details, it's best to look at the javadocs
for the org.apache.xerces.impl.xs.psvi package. Assuming you have produced a
Grammar object from an XML Schema document by some means, to turn that
object into an object usable in this API, do the following:

Cast the Grammar object to org.apache.xerces.xni.grammars.XSGrammar;
Call the toXSModel() method on the casted object;
Use the methods in the org.apache.xerces.impl.xs.psvi.XSModel interface to
examine the new object; methods on this interface and others in the same
package should allow you to access all aspects of the schema.
<<< From Xerces Caching & Preparsing Grammars FAQ end
>>>>>>>>>>>>>>>>>>>>>>>>>>>

But I cannot access all aspects of a schema! As you'll see below, when I
make a call to an element's getEnclosingCTDefinition method, it comes back
as null even though it really does have a complex type "element"!

For example, suppose I had this simple schema file: how can I access the
complex type for "Customer", for example and determine that there is a
"choice" selector and that it is composed of "NickName" and "FullName" which
itself is a  complex type with its own content?:

<?xml version = "1.0" ?>
        <schema xmlns = "http://www.w3.org/2001/XMLSchema";
          targetNamespace = "http://www.example.com/Test";
          xmlns:testNamespace = "http://www.example.com/Test";>

        <element name = "Customer">
                <complexType>
                        <choice>
                                <element name = "NickName" type = "string" />
                                <element name = "FullName" type = 
"testNamespace:FullNameType" />
                        </choice>
                </complexType>
        </element>

        <element name = "SimpleElement" type = "string" />

        <complexType name = "FullNameType">
                <choice>
                        <element name = "FirstName" type = "string" />
                        <element name = "MiddleInitial" type = "string" />
                        <element name = "LastName" type = "string" />
                </choice>
        </complexType>

        <complexType name = "TestComplexType">
                <attribute name = "testAttribute" type = "string"/>
        </complexType>

        <attribute name = "languageUsed" type = "language"/>

</schema>

Based on the FAQ above I got an XML grammar object as follows but do not
know how to proceed further in order to actually look at the contents of the
various elements, complex types etc?:

        XMLSchemaLoader xmlSchemaLoader = new XMLSchemaLoader();
        xmlSchemaLoader.setErrorHandler (this);

        XMLInputSource xmlInputSource = new XMLInputSource (null,
schemaFile.
          getName (), schemaFile.toURI ().getPath ());

        FileInputStream inputStream = null;
        try
        {
                inputStream = new FileInputStream (schemaFile);
        }
        catch (Exception pException) { pException.printStackTrace (); }

        xmlInputSource.setByteStream (inputStream);

        XSGrammar schemaGrammar = null;
        try
        {
                schemaGrammar = (XSGrammar) xmlSchemaLoader.loadGrammar (
                  xmlInputSource);
                XSModel schemaModel = schemaGrammar.toXSModel();

                XSNamedMap elementComponents = pSchemaModel.getComponents (
                  XSConstants.ELEMENT_DECLARATION);
                System.out.println ("Element components begin (" +
elementComponents.
                  getLength () + ")");

                for (int i = 0; i < elementComponents.getLength (); i++)
                {
                        XSElementDeclaration elementDeclaration =
(XSElementDeclaration)
                          elementComponents.item (i);
                        System.out.println ("\tNamespace:Name: " +
elementDeclaration.
                          getNamespace () + ":" + elementDeclaration.getName
());
                        System.out.println ("\tConstraint value: \"" +
elementDeclaration.
                          getConstraintValue() + "\"");

                        System.out.println ("\tType def name: \"" +
elementDeclaration.
                          getTypeDefinition().getName () + "\"");

                                // always comes back as null!
                                if 
(elementDeclaration.getEnclosingCTDefinition() != null)
                        {
                                System.out.println ("\tEnclosing ct type def
name: \"" +

elementDeclaration.getEnclosingCTDefinition().getParticle ().
                                  getName () + "\"");
                        }
                        else
                        {
                                System.out.println ("\tNo complex type
definition...");
                        }

                        System.out.println ();
                }
                System.out.println ("Element components end...\n");
        }
        catch (Exception pException)
        {
                pException.printStackTrace ();
        }

I suspect the above code is not the right approach at all but only gives a
cursory view of the Schema grammar?

My "actual" schema is composed of other files which are then included and in
some cases "imported" (e.g.,
        <import namespace = "file:///c:/schemas/test.xsd"
          schemaLocation = "../test.xsd"/>

But even in my more complex actual schema, the above code picks up all the
global elements, complex types etc. and has the correct namespaces
associated with each of them. But again I have the same problem as in the
simple "Customer" schema file above and cannot figure out how to access the
actual contents.

I would really appreciate any "enlightenment" anyone can give me on this?

Thanks.

_____________________________________________
Karl R. Mueller
[EMAIL PROTECTED]
_____________________________________________



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to