I have schemas with elements restrictions use to validate GUI forms. I want to get the value restrictions of some predefined elements, to provide them as tooltip on the forms.
The schema is loaded in org.apache.xerces.parsers.DOMParser which does the validation, I would think that there is a series of methods using the element hierarchy names I could call to get the restrictions from "myDefinedVal"?
form1.xsd:
<xsd:include schemaLocation="myBase.xsd"/>
<xsd:element name="conf"
<xsd:element name="def"
<xsd:element name="Level" type="myDefinedVal"
</xsd:element
</xsd:element
</xsd:element
myBase.xsd:
<xsd:simpleType name="myDefinedVal">
<xsd:restriction base="xsd:decimal">
<xsd:minInclusive value="0.001"/>
<xsd:maxInclusive value="99.999"/>
<xsd:fractionDigits value="3"/>
</xsd:restriction>
</xsd:simpleType>
Thanks, Jess
If you want to examine the contents of a schema file, you can use XSLoader to get an XSModel. You can then use the methods of the XSModel
to obtain schema components to examine.
Two brief examples to get you started are here and here.
EDIT follows:
I had time to make a code example. I made a slightly modified version of your xsd files because your form1.xsd seemed to be missing some characters perhaps due to an accidental copy and paste omission. Also, I used "myDefinedType" instead "myDefinedVal" for the name of the simpleType because I thought it was more clear for the sake of the example.
myform1.xsd:
<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:include schemaLocation="myBase.xsd"/>
<xsd:element name="conf"/>
<xsd:element name="def"/>
<xsd:element name="Level" type="myDefinedType"/>
</xsd:schema>
myBase.xsd:
<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:simpleType name="myDefinedType">
<xsd:restriction base="xsd:decimal">
<xsd:minInclusive value="0.001"/>
<xsd:maxInclusive value="99.999"/>
<xsd:fractionDigits value="3"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
Java code:
package dbank.so;
import java.io.File;
import org.apache.xerces.xs.XSFacet;
import org.apache.xerces.xs.XSImplementation;
import org.apache.xerces.xs.XSLoader;
import org.apache.xerces.xs.XSModel;
import org.apache.xerces.xs.XSObject;
import org.apache.xerces.xs.XSObjectList;
import org.apache.xerces.xs.XSSimpleTypeDefinition;
import org.apache.xerces.xs.XSTypeDefinition;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
public class XSLoaderTest {
public static void main(String[] args) {
System.setProperty(DOMImplementationRegistry.PROPERTY, "org.apache.xerces.dom.DOMXSImplementationSourceImpl");
DOMImplementationRegistry registry;
try {
registry = DOMImplementationRegistry.newInstance();
XSImplementation impl = (XSImplementation) registry.getDOMImplementation("XS-Loader");
XSLoader schemaLoader = impl.createXSLoader(null);
XSModel xsModel = schemaLoader.loadURI(new File("form1.xsd").toURI().toString());
XSTypeDefinition xsTypeDef = xsModel.getTypeDefinition("myDefinedType", null);
if(xsTypeDef.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE) {
XSSimpleTypeDefinition xsSimpleType = (XSSimpleTypeDefinition) xsTypeDef;
//get just maxInclusive
XSObject maxIncObj = xsSimpleType.getFacet(XSSimpleTypeDefinition.FACET_MAXINCLUSIVE);
if(maxIncObj != null) {
XSFacet maxIncFacet = (XSFacet) maxIncObj;
System.out.println("myDefinedType's maxInclusive: "+maxIncFacet.getLexicalFacetValue()+"\n");
}
//get all facets (except enumeration and pattern facets, which don't exist in this schema example anyway)
XSObjectList facetList = xsSimpleType.getFacets();
System.out.println("myDefinedType's facets: ");
for(int i = 0; i < facetList.getLength(); ++i) {
XSFacet facet = (XSFacet) facetList.get(i);
System.out.println(" "+facetKindToString(facet.getFacetKind())+": "+facet.getLexicalFacetValue());
}
//if you had a schema with enumeration and pattern facets and you wanted their information, you would use xsSimpleType.getMultiValueFacets()
}
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException | ClassCastException e) {
//handle exception
}
}
//a simple utility method to give a String representation of the facetKind
private static String facetKindToString(short facetKind) {
switch(facetKind) {
case XSSimpleTypeDefinition.FACET_NONE: return "none";
case XSSimpleTypeDefinition.FACET_LENGTH: return "length";
case XSSimpleTypeDefinition.FACET_MINLENGTH: return "minLength";
case XSSimpleTypeDefinition.FACET_MAXLENGTH: return "maxLength";
case XSSimpleTypeDefinition.FACET_PATTERN: return "pattern";
case XSSimpleTypeDefinition.FACET_WHITESPACE: return "whitespace";
case XSSimpleTypeDefinition.FACET_MAXINCLUSIVE: return "maxInclusive";
case XSSimpleTypeDefinition.FACET_MAXEXCLUSIVE: return "maxExclusive";
case XSSimpleTypeDefinition.FACET_MINEXCLUSIVE: return "minExclusive";
case XSSimpleTypeDefinition.FACET_MININCLUSIVE: return "minInclusive";
case XSSimpleTypeDefinition.FACET_TOTALDIGITS: return "totalDigits";
case XSSimpleTypeDefinition.FACET_FRACTIONDIGITS: return "fractionDigits";
case XSSimpleTypeDefinition.FACET_ENUMERATION: return "enumeration";
default: return "unknown facet kind";
}
}
}
Output:
myDefinedType's maxInclusive: 99.999
myDefinedType's facets:
whitespace: collapse
fractionDigits: 3
maxInclusive: 99.999
minInclusive: 0.001
I used XSFacet
's getLexicalFacetValue()
method to get the facet value as a String
to use in the System.out.println()
's. XSFacet
also has getIntFacetValue()
and getActualFacetValue()
to get the facet value as documented in XSFacet's javadoc.