More on XSD, PSVI and non-native attributes

If you have been following along with the trials and tribulations of XSD, PSVI and non-native attributes then you have been left with wondering about case where you have a non-native attribute and no <xsd:annotation>. For example:

<xsd:element name="something" myNS:name="Something" ...>

Since there is no <xsd:annotation> one might expect that there is no XSAnnotation object. This is exactly what occurs. So then, how does one access the non-native attribute? After a brief session of splunking through Xerces I stumbled across the notion of a “synthetic annotation”. A quick hop over to Google and one quicly finds out about the generate synthetic annotations feature which purports to “[generate a synthetic annotation] when a schema component has non-schema attributes but no child annotation”.

So we’re done, right? Well, no. The page from which the “generate synthetic annotations” is taken is actually for the SAX parser which is not what we use. A quick search of the intersection between XSModel, XSLoader (which is used to parse the XSD into the XSModel) and “feature” reveals nothing. Broadening the search to include all of the synonyms of “feature” (such as “parameter”, “attribute” and “property”) finally reveals a hit. XSLoader exposes a DOMConfiguration which allows one to set “parameters”. Listing all parameter names (via getParameterNames()) shows the sought after “generate-synthetic-annotations”. Whew!

To round this out, the synthetic annotation appears as:

<xsd:annotation myNS:name="Something" ...>

and the setup code to get an XSModel from an XSD is:

final DOMImplementationRegistry registry =
final XSImplementation xsImpl =
final XSLoader schemaLoader =
xsImpl.createXSLoader(null/*all XML Schema Versions*/);
// NOTE:  synthetic annotation nodes MUST be created for non-native
//        attributes to be parsed and added to the XSAnnotation object
//        (for cases where there is no )
final DOMConfiguration config = schemaLoader.getConfig();
final XSModel xsModel = schemaLoader.loadURI(xsdURI.toString());

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s