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" ...> <xsd:simpleType> .. </xsd:simpleType> </xsd:element>
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
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" ...> <xsd:documentation>SYNTHETIC_ANNOTATION</xsd:documentation> </xsd:annotation>
and the setup code to get an
XSModel from an XSD is:
System.setProperty(DOMImplementationRegistry.PROPERTY, "org.apache.xerces.dom.DOMXSImplementationSourceImpl"); final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); final XSImplementation xsImpl = (XSImplementation)registry.getDOMImplementation("XS-Loader"); 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(); config.setParameter("http://apache.org/xml/features/generate-synthetic-annotations", true); final XSModel xsModel = schemaLoader.loadURI(xsdURI.toString());