XML Schema developer are very well aware of UPA (Unique Particle Attribution) rule violation and a lot of times might have faced this requirement to set it off. Here I will try to put it in simple words to set UPA violation off. For those who want to have a look at what UPA problem is all about I would suggest to go through this article UPA in plain English by Priya Lakshminarayanan.
1. Set UPA off while generating xml beans:
To get rid of UPA violation, set “noupa” to true while generating xml beans.
Lets say we have the following goal entry gen-test in the build script for generating xml beans for test-1.0.xsd :
build Script entry:
***********
<goal name=”gen-test” description=”Generate XMLBeans for test XSD” prereqs=”init-gen”>
<ant:xmlbean destfile=”${basedir}/target/test-1.0.jar” failonerror=”true” download=”true” noupa=”true”>
<classpath refid=”maven.dependency.classpath” />
<fileset dir=”./src/resources/xsd”>
<include name=”test-1.0.xsd*” />
</fileset>
</ant:xmlbean>
<ant:echo>blah blah….</ant:echo>
</goal>
************
Get the XmlObject (<XmlObject>.Factory.parse()) and avoid any UPA rule violation exception.
2. Set UPA off while creating Schema from XSD:
Lets say I need to implement this condition in my xsd schema. I want the sequence to be more flexible, keeping the explicit members there, the user should be able to add any extra nodes also.
XSD entry (test.xsd):
***************
…..
<xsd:sequence>
<xsd:element name=”test1″ minOccurs=”0″/>
<xsd:element name=”test2″ minOccurs=”0″/>
<xsd:any processContents=”skip” minOccurs=”0″ maxOccurs=”unbounded”/>
</xsd:sequence>
….
***************
While generating the Schema from this resource like
javax.xml.validation.SchemaFactory schemaFactory = javax.xml.validation.SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(<resource:test.xsd>);
it throws exception:
***************
org.xml.sax.SAXParseException: cos-nonambig: “http://www.test.com/test“:test1 and WC[##any] (or elements from their substitution group) violate “Unique Particle Attribution”. During validation against this schema, ambiguity would be created for those two particles.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:236)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:172)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:382)
at com.sun.org.apache.xerces.internal.impl.xs.XSConstraints.reportSchemaError(XSConstraints.java:315)
at com.sun.org.apache.xerces.internal.impl.xs.XSConstraints.fullSchemaChecking(XSConstraints.java:463)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:530)
at com.sun.org.apache.xerces.internal.jaxp.validation.xs.SchemaFactoryImpl.newSchema(SchemaFactoryImpl.java:206)
….
****************
we know the problem, XSConstraints throws XMLSchemaException exception while doing fullSchemaChecking. To set the full schema checking off we can use something like:
schemaFactory.setFeature(“http://apache.org/xml/features/validation/schema-full-checking “, false);
[ true : Enable full schema grammar constraint checking, including checking which may be time-consuming or memory intensive. Currently, unique particle attribution constraint checking and particle derivation restriction checking are controlled by this option. ]
But the problem is that not every implementation of SchemaFactory allows you to set this violation ON/OFF.
The com.sun.org.apache.xerces.internal.jaxp.validation.xs.SchemaFactoryImpl doesn’t allow you to set this feature but org.apache.xerces.jaxp.validation.XMLSchemaFactory allows you to set this violation rule to true/false.
I hope this would be of some help.

