0

我要为源代码(多种语言)编写一个生成器。类,即基本数据容器,应指定为 XML 文件。为了自动验证和解析这些 XML 文件,我定义了一个 XSD 模式。这应该是一个有效的文件:

<?xml version="1.0"?>
<class>
    <customType name="vector3D">
        <variable name="x" type="int"/>
        <variable name="y" type="int"/>
        <variable name="z" type="int"/>
    </customType>
    <variable name="identifier" type="string"/>
    <variable name="direction" type="vector3D"/>
</class>

我定义了我的根元素class,元素customTypevariable为:

<xsd:complexType name="Class">
    <xsd:sequence>
        <xsd:element name="customType" type="CustomType"
            minOccurs="0" maxOccurs="unbounded"/>
        <xsd:element name="variable" type="Variable"
            minOccurs="0" maxOccurs="unbounded"/>
    </xsd:sequence>
<xsd:complexType>

<xsd:complexType name="CustomType">
    <xsd:sequence>
        <xsd:element name="variable" type="Variable"
            minOccurs="0" maxOccurs="unbounded"/>
    </xsd:sequence>
    <xsd:attribute name="name" type="xsd:string"
        use="required"/>
</xsd:complexType>

<xsd:complexType name="Variable">
    <xsd:attribute name="name" type="xsd:string"
        use="required"/>
    <xsd:attribute name="type" type="ValidType"
        use="required"/>
</xsd:complexType>

但是,我正在努力尝试允许一组有限的基本类型customType标签中定义的名称。定义我的一组基本类型很容易:

<xsd:simpleType name="ValidType">
    <xsd:restriction base="xsd:string">
        <xsd:enumeration value="bool"/>
        <xsd:enumeration value="int"/>
        <xsd:enumeration value="string"/>
    </xsd:restriction>
</xsd:simpleType>

但是有什么方法我也可以允许在标签name 属性中定义customType标识符,还是我必须允许任何 标识符xsd:string并检查生成器中的有效性?

编辑

如果我正确理解了 W3C XML Schema Definition Language (XSD) Recommendation 的 3.16.2,我想要的不能使用 XSD 完成(因为限制仅限于 minExclusive | minInclusive | maxExclusive | maxInclusive | totalDigits | fractionDigits | length | minLength | maxLength | enumeration | whiteSpace | pattern | assertion | explicitTimezone,不支持这种动态限制),我必须这样做这是在手动验证 XSD 架构之后。

任何人都可以确认这是正确的吗?

4

1 回答 1

3

是的,我认为你是对的。在 xsd 1.0 中,不可能进行动态限制,例如“如果属性名称等于 XX,则属性类型只能等于 ZZ”。在 xsd 1.1 中可以定义断言,但我不确定可用解析器对它的支持程度(可能 Saxon 可能具有此功能)。

于 2013-07-15T13:39:07.887 回答