On Mon, 2014-07-21 at 05:50 -0700, jgagnon wrote:
> Sorry for the confusion.  My application does not deal with the Java
> implementation of any of the schema types.  It uses the information from the
> schema to determine test cases to generate and then uses the XMLBeans API
> (mainly XmlCursor) to create an XML instance.  Once the instance is complete
> I then validate it via the XmlObject.validate(XmlOptions options) method and
> save the object to a file.
>
> One of the first things I do is to compile the schema XSD (with
> XmlBeans.compileXsd(...)) to get a SchemaTypeSystem object. This provides me
> with everything else I need.
> 
> The schema defines several abstract types, each of which has some number of
> other schema types that extend them (i.e. <xs:extension
> base="myPrefix:AbstractBaseType">).  As the program works its way through a
> document type to identify and generate test cases, when it encounters an
> element defined by an abstract type, it scans the schema for any types that
> extend that type.  It then generates the element with the type reference
> attribute to point to the specific concrete schema type.

OK - by "generates .. with the type reference..." do you mean that you
are doing something like this?

  xmlCursor.insertAttributeWithValue("type",
      "http://www.w3.org/2001/XMLSchema-instance";,
      "myPrefix:ConcreteType");

If so, it seems to me that at this point the XMLBeans API hasn't
associated "myPrefix:" with your namespace, hence the validation
failure. The cursor methods can only work with attribute values of type
java.lang.String, i.e. as far as I can see the XmlCursor API doesn't
provide a bridge between attribute _values_ and anything related to the
schema. What you actually want to insert as the attribute value is an
instance of javax.xml.namespace.QName or org.apache.xmlbeans.XmlQName
rather than a string, but it doesn't seem that there is a way of doing
that.

When the document is read back in by the command-line validator, it
interprets the value of the xsi:type attribute as a QName, so it
validates OK.

> I also generate
> the child content that is defined by the specific concrete type.
> 
> The generation logic inserts the namespace(s) as attributes of the root
> element.  I insert the "default" namespace (i.e.
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";) and the
> schema-specific namespace (e.g. xmlns:myPrefix="my-schema-site") ["myPrefix"
> and "my-schema-site" are not the actual values used, of course].

I work with documents that are very similar to yours without any
problems. There are two differences with what you are doing:

(i) I don't use XmlCursor, but rather the type-specific methods provided
by the XMLBeans-generated API, something like this:

  Instantiate an element of the correct type like this:

    myConcreteType = ConcreteType.Factory.newInstance();

   then populate it and insert it into the XML instance something like
this:

  myContainingElement.setSomeElement(i, myConcreteType);

I don't set the type reference at all, it is just taken care of
automatically by the XMLBeans-generated API.

If this is not practical for you, then perhaps saving and re-parsing the
XML instance before validating would be an acceptable workaround?

(ii) the xmlns:xsi and xmlns:myprefix attributes end up in the element
that is the immediate container of the explicitly typed element rather
than the root element:

 <ContainingElement xmlns:xsi="..." xmlns:myprefix="...">
   <TypedElement xsi:type="myprefix:MyType">
  ...
  </TypedElement>
 </ContainingElement>

or sometimes on the typed element itself. This is a minor annoyance, and
in my hands XmlObject.setSaveAggressiveNamespaces() has no effect when
the document is saved, but it really shouldn't make any difference to
the validation.

> Regarding your comment on the schema version that is used, what can I do
> about that?  

A version 1.0 schema is still valid under the 1.1 standard: I mentioned
it only in order to be clear about which standards document I was using
to look up your error message (the clause numbers are different in the
two standards). As far as I know, if you use XMLBeans, you are using XML
Schema 1.0. Only you can decide whether that is a problem for you (it
isn't for me, FWIW). The introduction to the XML Schema 1.1 document may
be a good starting point:

<http://www.w3.org/TR/xmlschema11-1/#intro1.1>

> I insert the "standard" XML heading into the file before
> validating and saving.  E.g. <?xml version="1.0" encoding="utf-8"?>  Does
> the version number here indicate the schema version number?

No, this is the version of the XML standard that is used, not of XML
Schema. Once again, only you can decide whether you have a need for XML
1.1, or if 1.0 is OK for you. As with XML Schema, there is a preamble to
the newer standard that says something about what issues it is
addressing:

<http://www.w3.org/TR/xml11/#sec-xml11>

Regards,
Peter.

-- 
Peter Keller                                     Tel.: +44 (0)1223 353033
Global Phasing Ltd.,                             Fax.: +44 (0)1223 366889
Sheraton House,
Castle Park,
Cambridge CB3 0AX
United Kingdom



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@xmlbeans.apache.org
For additional commands, e-mail: user-h...@xmlbeans.apache.org

Reply via email to