2

I have such xml structure:

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

This is a simplified example.

I want to create xsd schema which invalidates such xml:

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

because of there is no object item with the name "fruit" into list of objects.

I can't simply create <xsd:enumeration> because object names are always different and I don't know all of them. It seems to be a list of possible values of action's names should be created dynamically.

It would be wonderful to create enumeration dynamically for IntelliSense support (<xsd:assert> can't provide it).

Is it possible?

4

1 回答 1

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 显示具有 key/keyref 功能的 XSD 图

于 2012-07-28T16:15:22.033 回答