我有带有元素限制的模式,用于验证 GUI 表单。我想获得一些预定义元素的值限制,以将它们作为表单上的工具提示提供。
架构加载到进行验证的 org.apache.xerces.parsers.DOMParser 中,我认为有一系列使用元素层次结构名称的方法我可以调用以从“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>
谢谢, 杰斯
最佳答案
如果你想检查模式文件的内容,你可以使用 XSLoader得到一个XSModel .然后,您可以使用 XSModel
的方法获取要检查的架构组件。
编辑如下:
我有时间制作一个代码示例。我对你的 xsd 文件做了一个稍微修改的版本,因为你的 form1.xsd 似乎缺少一些字符,这可能是由于意外的复制和粘贴遗漏。此外,我使用“myDefinedType”而不是“myDefinedVal”作为 simpleType 的名称,因为我认为为了示例的缘故它更清楚。
我的表单1.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>
我的基地.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()
来获取 facet 值,如 XSFacet's javadoc 中所述.
关于java - 如何使用 apache.xcerces 从 java 中的模式元素获取 maxInclusive 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28544178/