如果要检查架构文件的内容,可以使用XSLoader 获取XSModel。然后您可以使用XSModel 的方法来获取要检查的架构组件。
here 和 here 是帮助您入门的两个简短示例。
编辑如下:
我有时间制作一个代码示例。我对您的 xsd 文件做了一个稍微修改的版本,因为您的 form1.xsd 似乎缺少一些字符,可能是由于意外的复制和粘贴遗漏。此外,我使用“myDefinedType”而不是“myDefinedVal”作为 simpleType 的名称,因为我认为为了示例而更清楚。
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 代码:
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";
}
}
}
输出:
myDefinedType's maxInclusive: 99.999
myDefinedType's facets:
whitespace: collapse
fractionDigits: 3
maxInclusive: 99.999
minInclusive: 0.001
我使用XSFacet 的getLexicalFacetValue() 方法将构面值作为String 用于System.out.println()。 XSFacet 也有 getIntFacetValue() 和 getActualFacetValue() 以获取 XSFacet's javadoc 中记录的构面值。