如何使用 apache.xcerces 从 java 中的模式元素获取 maxInclusive 值?

How to get maxInclusive value from a schema element in java using apache.xcerces?

我有元素限制的模式用于验证 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 to get an XSModel。然后,您可以使用 XSModel 的方法获取要检查的架构组件。

让您入门的两个简短示例是 here and here

编辑如下:

我有时间做一个代码示例。我对你的 xsd 文件做了一个稍微修改的版本,因为你的 form1.xsd 似乎缺少一些字符,这可能是由于意外的复制和粘贴遗漏。另外,我使用 "myDefinedType" 而不是 "myDefinedVal" 作为 simpleType 的名称,因为我认为为了这个例子更清楚。

我的form1.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

我使用 XSFacetgetLexicalFacetValue() 方法将构面值作为 StringSystem.out.println() 中使用。 XSFacet 也有 getIntFacetValue()getActualFacetValue() 来获取 XSFacet's javadoc.

中记录的构面值

只是为了添加信息以提取模式

代码需要如下:

导入org.apache.xerces.xs.XSMultiValueFacet;

            XSObjectList facetList = xsSimpleType.getMultiValueFacets();
            System.out.println("myDefinedType's facets:  ");
            for(int i = 0; i < facetList.getLength(); i++) {
                XSMultiValueFacet facet = (XSMultiValueFacet ) facetList.item(i);
                System.out.println("  "+facetKindToString(facet.getFacetKind()));
                    StringList strList = facet.getLexicalFacetValues();
                    System.out.println("        " + strList.item(0));
            }