Sun, 31 Aug 2008 19:34:07 +0200, /Eric Lilja/:

public class ParseXMLSchema {
   public static void main(String[] args) {
      ParseXMLSchema instance = new ParseXMLSchema();

      instance.parseXMLSchema(new File("test-1.xsd"));
   }

   private void parseXMLSchema(final File f) {
      parseXMLSchema(f.getAbsolutePath());
   }

   private void parseXMLSchema(final String schemaFileName) {
[...]
      XSModel xsModel = xsLoader.loadURI(schemaFileName);

As you may notice XSModel.loadURI(String) accepts an URI, not a file path, so what you're doing is basically wrong and you should use:

   private void parseXMLSchema(final File f) {
      parseXMLSchema(f.toURI().toString());
   }

[...]
The output I get when running is:
Complex element: movies, no attributes, no annotations
Complex element: movie, no attributes, 1 annotation(s)
<xs:annotation foo:bar="baz" xmlns:xs="http://www.w3.org/2001/XMLSchema"; xmlns="myns" xmlns:foo="http://www.foobarbaz.com/foo"; >
<xs:documentation>SYNTHETIC_ANNOTATION</xs:documentation>
</xs:annotation>
Simple element: title, 1 annotation(s)
<xs:annotation foo:bar="baz" xmlns:xs="http://www.w3.org/2001/XMLSchema"; xmlns="myns" xmlns:foo="http://www.foobarbaz.com/foo"; >
<xs:documentation>SYNTHETIC_ANNOTATION</xs:documentation>
</xs:annotation>
, no annotations
Simple element: releaseYear, no annotations

As you can see, the annotation is reported both for the complex element movie and the simple element title.

I believe this is just a bug in your example - note the ", no annotations" line just before the last output line "Simple element: releaseYear, no annotations". What I was pointing previously is the difference between:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema";
        xmlns:foo="http://www.foobar.com/foo";>
    <xs:element name="movies">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="movie" type="xs:string"
                        foo:bar="somevalue"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element/>
</xs:schema>

and:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema";
        xmlns:foo="http://www.foobar.com/foo";>
    <xs:element name="movies">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="movie" foo:bar="somevalue"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="movie" type="xs:string"/>
</xs:schema>

In the first case you get the same annotations for the "movie" element particle and its element declaration, while in the second you just get it for the particle.

Also, it seems a bit difficult to parse the annotation string properly so I can discard annotations I am not interested in.

I've previously used to write the content of the annotations to a DOM Element (or DocumentFragment), then use the DOM methods to traverse and extract what I recognize as significant for my application:

    Document doc;
    ...
    Node getAnnotation(XSAnnotation annotation) {
        DocumentFragment fragment = doc.createDocumentFragment();
        annotation.writeAnnotation(fragment,
                                   XSAnnotation.W3C_DOM_ELEMENT);
        return fragment;
    }

--
Stanimir

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

Reply via email to