xml - XS型号 : getting model group information

标签 xml xsd xerces

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

最佳答案

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

关于xml - XS型号 : getting model group information,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19745906/

相关文章:

java - 在 java 中将 XML 文档写入文件的最佳方法是什么?

java - 在 ProGuard 中解析 "duplicate definition of library class"

c# - 获取 XElement 的名称

java - 如何去掉pom.xml下的红线?

jquery - 使用 jQuery 读取 xml

spring - Spring 应用程序上下文模式中的错误

java - 如何在 XSD 中指定属性 namespace 以便 JAXB 正确解释它?

c# - 将 Any-Element 添加到动态创建的 XSD-Schema 时出现问题

c# 如何从 XML 文档中获取 targetNamespace

c++ - 从 Xerces 获取 XML 数据 (c++)