2013-11-02 108 views
1

使用Xerces SAX庫的XSModel作爲XSD語法的表示,如果我有涉及模型組的複雜類型,如何檢索模型組?看起來像由Xerces提供的XSModel實例所表示的複雜類型定義僅提供對組(集合的元素)的展開(展開)內容的訪問,而不是實際的組或對組定義的引用(甚至是團體的名字; XSModelGroupImplgetName() -method由return null組成)。XSModel:獲取模型組信息

回答

5

Xerces很好地揭示了模型組。 但是,您應該使用org.apache.xerces.xs包。 模型組可以在頂層聲明中找到,也可以在複雜類型中找到。

以下是樣本Java代碼:

import org.apache.xerces.xs.*; 
import org.apache.xerces.dom.DOMXSImplementationSourceImpl; 
.... 

/** 
* Load an XSD file 
*/ 
void loadSchema (String xsdURI) 
{ 
    XSImplementation impl = (XSImplementation) 
    (new DOMXSImplementationSourceImpl()).getDOMImplementation ("XS-Loader"); 

    XSLoader schemaLoader = impl.createXSLoader (null); 

    XSModel xsModel = schemaLoader.loadURI (xsdURI); 
} 

/** 
* Process schema content 
*/ 
private void processXSModel (XSModel xsModel) 
{ 
    XSNamedMap xsMap; 

    // process model group definitions 
    xsMap = xsModel.getComponents (XSConstants.MODEL_GROUP_DEFINITION); 
    for (int i = 0; i < xsMap.getLength(); i ++) 
    { 
    XSModelGroupDefinition xsGroupDef = (XSModelGroupDefinition) xsMap.item (i); 
    XSModelGroup xsGroup = xsGroupDef.getModelGroup(); 
    ... 
    } 

    // process top-level type definitions 
    xsMap = xsModel.getComponents (XSConstants.TYPE_DEFINITION); 
    for (int i = 0; i < xsMap.getLength(); i ++) 
    { 
    XSTypeDefinition xsTDef = (XSTypeDefinition) xsMap.item (i); 
    processXSTypeDef (xsTDef); 
    } 

    // process top-level element declarations 
    xsMap = xsModel.getComponents (XSConstants.ELEMENT_DECLARATION); 
    for (int i = 0; i < xsMap.getLength(); i ++) 
    { 
    XSElementDeclaration xsElementDecl = (XSElementDeclaration) xsMap.item (i); 
    processXSElementDecl (xsElementDecl); 
    } 
} 

/** 
* Process type definition 
*/ 
private void processXSTypeDef (XSTypeDefinition xsTDef) 
{ 
    switch (xsTDef.getTypeCategory()) 
    { 
    case XSTypeDefinition.SIMPLE_TYPE: 

     processXSSimpleType ((XSSimpleTypeDefinition) xsTDef); 
     break; 

    case XSTypeDefinition.COMPLEX_TYPE: 

     XSComplexTypeDefinition xsCTDef = (XSComplexTypeDefinition) xsTDef; 

     // element's attributes 
     XSObjectList xsAttrList = xsCTDef.getAttributeUses(); 
     for (int i = 0; i < xsAttrList.getLength(); i ++) 
     { 
     processXSAttributeUse ((XSAttributeUse) xsAttrList.item (i)); 
     } 

     // element content 
     switch (xsCTDef.getContentType()) 
     { 
     case XSComplexTypeDefinition.CONTENTTYPE_EMPTY: 

      break; 

     case XSComplexTypeDefinition.CONTENTTYPE_SIMPLE: 

      parseValueType (xsCTDef.getSimpleType()); 
      break; 

     case XSComplexTypeDefinition.CONTENTTYPE_ELEMENT: 

      processXSParticle (xsCTDef.getParticle()); 
      break; 

     case XSComplexTypeDefinition.CONTENTTYPE_MIXED: 

      ... 
      processXSParticle (xsCTDef.getParticle()); 
      break; 
     } 
     } 

     break; 
    } 

    /** 
    * Process particle 
    */ 
    private void processXSParticle (XSParticle xsParticle) 
    { 
    XSTerm xsTerm = xsParticle.getTerm(); 
    switch (xsTerm.getType()) 
    { 
     case XSConstants.ELEMENT_DECLARATION: 

     processXSElementDecl ((XSElementDeclaration) xsTerm); 
     break; 

     case XSConstants.MODEL_GROUP: 

     // this is one of the globally defined groups 
     // (found in top-level declarations) 

     XSModelGroup xsGroup = (XSModelGroup) xsTerm; 

     // it also consists of particles 
     XSObjectList xsParticleList = xsGroup.getParticles(); 
     for (int i = 0; i < xsParticleList.getLength(); i ++) 
     { 
      processXSParticle ((XSParticle) xsParticleList.item (i)); 
     } 

     ... 
     break; 

     case XSConstants.WILDCARD: 

     ... 
     break; 
    } 
    }