【问题标题】:XSModel: getting model group informationXSModel:获取模型组信息
【发布时间】:2013-11-02 19:26:52
【问题描述】:

使用 Xerces SAX 库的 XSModel 作为 XSD 语法的表示,如果我有一个引用模型组的复杂类型,我如何检索模型组?似乎由 Xerces 提供的 XSModel 实例表示的复杂类型定义仅提供对组的扁平(扩展)内容(例如组的元素)的访问,而不是实际组或对组定义(甚至是组的名称;XSModelGroupImpl 的getName() 方法由return null 组成......)。

【问题讨论】:

    标签: xml xsd xerces


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2015-06-11
      • 2021-07-04
      • 2018-01-19
      • 2018-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-27
      相关资源
      最近更新 更多