XSD中的动态枚举

时间:2012-07-28 13:42:42

标签: xml xsd

我有这样的xml结构:

<main>
  <objects>
    <object name="book" />
    <object name="table" />
  </objects>
  <actions>
    <action input="book" />
    <action input="table" />
    <action input="book" />
  </actions>
</main>

这是一个简化的例子。

我想创建xsd架构,使xml:

无效
<main>
  <objects>
    <object name="book" />
    <object name="table" />
  </objects>
  <actions>
    <action input="book" />
    <action input="table" />
    <action input="fruit" />
  </actions>
</main>

因为在对象列表中没有名称为“fruit”的对象项。

我不能简单地创建<xsd:enumeration>,因为对象名称总是不同的,我不知道所有这些。似乎应该动态创建动作名称的可能值列表。

为IntelliSense支持动态创建枚举会很棒(<xsd:assert>无法提供)。

有可能吗?

1 个答案:

答案 0 :(得分:3)

从生成的XSD开始,然后调整它以引入您想要的约束。 Intellisense部分高度依赖于编辑器。即使用于推断“智能”智能感知的元数据在那里(通过key / keyref),我怀疑市场上的编辑会利用它。下面的XSD将验证您提供的XML并将其失败。

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="main">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="objects">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element maxOccurs="unbounded" name="object">
                                <xsd:complexType>
                                        <xsd:attribute name="name" type="xsd:string" use="required"/>
                                </xsd:complexType>
                            </xsd:element>
                        </xsd:sequence>
                    </xsd:complexType>
                </xsd:element>
                <xsd:element name="actions">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element maxOccurs="unbounded" name="action">
                                <xsd:complexType>
                                        <xsd:attribute name="input" type="xsd:string" use="required"/>
                                </xsd:complexType>
                            </xsd:element>
                        </xsd:sequence>
                    </xsd:complexType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
        <xsd:key name="Objects">
            <xsd:selector xpath="objects/object"/>
            <xsd:field xpath="@name"/>
        </xsd:key>
        <xsd:keyref name="ActionToObjects" refer="Objects">
            <xsd:selector xpath="actions/action"/>
            <xsd:field xpath="@input"/>
        </xsd:keyref>
    </xsd:element>
</xsd:schema>

图表:

QTAssistant showing XSD diagram with key/keyref features