Вопрос

Supposing I had an XSD that might contain something like this:

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

Suppose I wanted to be able to get hold of both enumeration values, and their names (which are in the id attributes). I'm trying to figure out if this is possible.

Supposing a little bit further, I might be using xerces-c (3.1.1, say), and more specifically, be using classes from xercesc/framework/psvi. I've had a preliminary poke about, and things aren't looking promising:

  • It looks like XSSimpleTypeDefinition provides access to enumeration details via getMultiValueFacets()
  • However, this returns an XSMultiValueFacet, which seems only to provide access to values (and annotations).

Is there, perhaps, something I'm missing?

Это было полезно?

Решение 2

I don't think there is anything you're missing (but I am not versed in Xerces-C internals). The XSD spec defines the post-schema-validation infoset and the abstract structure of schema components in some detail, but it does not require that validators provide access to any particular parts of either, and still less does it specify an API for doing so. (At the crucial moment, a WG member from an influential company snarled "We don't need no steenking API specs", and pretty much all the vendors in the room agreed.) So what you have access to is determined by the specific software you're using, as is the API through which you have access to it.

Even the most thorough of APIs, however, is unlikely to provide access to the values of ID attributes on the xsd:enumeration elements -- the ID attribute doesn't correspond to any part of the simple type component and virtually all designers of APIs for schema information are likely to regard it as an epiphenomenal artifact of the XML representation of the component, without any intrinsic interest.

If you have access to the schema documents which define the schema you are using, of course, you can always just use normal XML tools to find the IDs you're interested in; that's one of the reasons for making schema documents be XML documents in the first place.

Другие советы

Genericode is a great example of how one can include the code, ID, and Description in such a way that is still valid in an XSD. https://www.oasis-open.org/committees/tc_home.php?wg_abbrev=codelist

Here is a quick sample for the full list go 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>

It worked for me for enums with <xsd:restriction base="xsd:string">:

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));
        }
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top