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

时间:2015-02-16 14:56:47

标签: java schema xerces

我有用于验证GUI表单的元素限制的模式。我想得到一些预定义元素的值限制,以在表单上提供它们作为工具提示。

架构加载到org.apache.xerces.parsers.DOMParser中进行验证,我认为有一系列方法使用我可以调用的元素层次结构名称来获取" myDefinedVal&的限制#34;

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>

谢谢, 杰斯

2 个答案:

答案 0 :(得分:4)

如果要检查架构文件的内容,可以使用XSLoader获取XSModel。然后,您可以使用XSModel的方法来获取要检查的模式组件。

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

编辑如下:

我有时间编写代码示例。我对你的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的{​​{1}}方法将facet值作为getLexicalFacetValue()用于StringSystem.out.println()还有XSFacetgetIntFacetValue()来获取XSFacet's javadoc中记录的方面值。

答案 1 :(得分:2)

只是添加信息以提取模式

代码必须如下:

import 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));
            }