【问题标题】:How can I access enumeration ids in XML schema (using xerces-c)?如何访问 XML 模式中的枚举 ID(使用 xerces-c)?
【发布时间】:2013-04-03 15:40:36
【问题描述】:

假设我的 XSD 可能包含以下内容:

<simpleType name="CELESTIAL_IMPORIUM_CATEGORY">
    <restriction base="integer">
        <enumeration id="BELONGING_TO_THE_EMPEROR" value="8001"/>
        <enumeration id="EMBALMED"                 value="8002"/>
        <enumeration id="TRAINED"                  value="8003"/>
        <enumeration id="SUCKLING_PIGS"            value="8004"/>
    </restriction>
</simpleType>

假设我希望能够同时获取枚举值及其名称(在 id 属性中)。我正在尝试弄清楚这是否可能。

假设再进一步,我可能正在使用 xerces-c(比如 3.1.1),更具体地说,正在使用来自 xercesc/framework/psvi 的类。我已经初步了解了一下,但事情看起来并不乐观:

  • 看起来XSSimpleTypeDefinition 通过getMultiValueFacets() 提供对枚举详细信息的访问
  • 但是,这会返回一个XSMultiValueFacet,它似乎只提供对值(和注释)的访问。

也许,我错过了什么?

【问题讨论】:

    标签: xsd xerces-c


    【解决方案1】:

    Genericode 是一个很好的示例,说明如何以在 XSD 中仍然有效的方式包含代码、ID 和描述。 https://www.oasis-open.org/committees/tc_home.php?wg_abbrev=codelist

    这是完整列表的快速示例,请转到here

     <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ccts="urn:un:unece:uncefact:documentation:2" elementFormDefault="qualified" attributeFormDefault="unqualified">
        <xs:simpleType name="CurrencyCodeContentType">
        <xs:restriction base="xs:token"><xs:enumeration value="AED"><xs:annotation><xs:documentation>
                            <ccts:CodeName>Dirham</ccts:CodeName>
                            <ccts:CodeDescription/>
                        </xs:documentation>
                    </xs:annotation>
                </xs:enumeration><xs:enumeration value="AFN"><xs:annotation><xs:documentation>
                            <ccts:CodeName>Afghani</ccts:CodeName>
                            <ccts:CodeDescription/>
                        </xs:documentation>
                    </xs:annotation>
                </xs:enumeration><xs:enumeration value="ALL"><xs:annotation><xs:documentation>
                            <ccts:CodeName>Lek</ccts:CodeName>
                            <ccts:CodeDescription/>
                        </xs:documentation>
                    </xs:annotation>
                </xs:enumeration><xs:enumeration value="AMD"><xs:annotation><xs:documentation>
                            <ccts:CodeName>Dram</ccts:CodeName>
                            <ccts:CodeDescription/>
                        </xs:documentation>
                    </xs:annotation>
                </xs:enumeration><xs:enumeration value="ANG"><xs:annotation><xs:documentation>
                            <ccts:CodeName>Netherlands Antillian Guilder</ccts:CodeName>
                            <ccts:CodeDescription/>
                        </xs:documentation>
                    </xs:annotation>
                </xs:enumeration>
            </xs:restriction>
        </xs:simpleType>
    </xs:schema>
    

    【讨论】:

    • 通过 xerces-c 仍然无法真正访问
    【解决方案2】:

    我不认为您缺少任何东西(但我不精通 Xerces-C 内部结构)。 XSD 规范详细定义了模式验证后信息集和模式组件的抽象结构,但它不要求验证器提供对任何特定部分的访问,更不用说为此指定 API。 (在关键时刻,来自一家有影响力的公司的 WG 成员咆哮道:“我们不需要 steening API 规范”,房间里几乎所有的供应商都同意。)所以你能访问什么取决于具体的软件您正在使用的 API,您可以通过它访问它。

    然而,即使是最彻底的 API,也不太可能提供对 xsd:enumeration 元素上 ID 属性值的访问——ID 属性不对应于简单类型组件的任何部分,几乎所有设计者用于模式信息的 API 很可能将其视为组件的 XML 表示的附带现象,没有任何内在兴趣。

    如果您有权访问定义您正在使用的架构的架构文档,当然,您总是可以使用普通的 XML 工具来查找您感兴趣的 ID;这就是让架构文档首先成为 XML 文档的原因之一。

    【讨论】:

      【解决方案3】:

      它适用于我的枚举 :

      std::vector<std::basic_string<XMLCh>> values;
      if (type->getTypeCategory() == xercesc_3_2::XSTypeDefinition::SIMPLE_TYPE) {
          const auto simpleType = static_cast<xercesc_3_2::XSSimpleTypeDefinition *>(type);
          const auto multiValueFacets = simpleType->getMultiValueFacets();
          for (auto i = 0; multiValueFacets && i < multiValueFacets->size(); i++) {
              const auto el = multiValueFacets->elementAt(i);
              const auto facets = el->getLexicalFacetValues();
              for (auto vi = 0; facets && vi < facets->size(); vi++) {
                  values.emplace_back(facets->elementAt(vi));
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-27
        • 1970-01-01
        相关资源
        最近更新 更多