如何允许元素包含子元素或数字中的一个?

时间:2015-08-26 12:47:46

标签: xml xsd

我正在尝试开发Pipes and Filters pattern的实现,并且我希望允许用户在配置文件中的管道中指定过滤器。 XML是一个很好的选择,因为它是一种分层语言 - 可以通过将对应于特定类型的过滤器的XML元素嵌套在另一个中来指定管道。

我尝试过定义少量过滤器和" InputType"包含xsd:choice元素的类型定义,用于在它们之间进行选择。但是,choice元素似乎只允许在子元素之间进行选择,而不允许在xs:stringxs:decimal之类的其他可能类型的数据中进行选择。

我目前有这样的架构:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:complexType name="InputLinearScaleType">
        <xs:sequence>
            <xs:element name="Input" type="InputType"/>
            <xs:element name="Gain" type="InputType" minOccurs="0"/>
            <xs:element name="Offset" type="InputType" minOccurs="0"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="NumericLoggingFilterType">
        <xs:sequence>
            <xs:element name="Input" type="InputType" />
            <xs:element name="LogPath" type="xs:string" />
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="InputType">
        <xs:sequence>
            <xs:choice>
                <xs:element name="LinearScale" type="InputLinearScaleType" />
                <xs:element name="Logger" type="NumericLoggingFilterType" />
                <xs:element name="Constant" type="xs:decimal" />
                <xs:element name="SimulatedNumericSource" />
            </xs:choice>
        </xs:sequence>
    </xs:complexType>

    <xs:element name="FilterExample" type="InputType" />
</xs:schema>

这允许我验证这样的事情:

<FilterExample xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:///W:/Shared/XML Schemas/schemaFilterTemp.xsd">
    <Logger>
        <Input>
            <LinearScale>
                <Input>
                    <SimulatedNumericSource/>
                </Input>
                <Gain>
                    <Constant>5</Constant>
                </Gain>
                <Offset>
                    <Constant>11</Constant>
                </Offset>
            </LinearScale>
        </Input>
        <LogPath>/dev/null</LogPath>
    </Logger>
</FilterExample>

但是,我想要的是能够验证以下内容:

<FilterExample xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:///W:/Shared/XML Schemas/schemaFilterTemp.xsd">
    <Logger>
        <Input>
            <LinearScale>
                <Input>
                    <SimulatedNumericSource/>
                </Input>
                <Gain>5</Gain>
                <Offset>11</Offset>
            </LinearScale>
        </Input>
        <LogPath>/dev/null</LogPath>
    </Logger>
</FilterExample>

这将要求模式允许许多元素被解释为数字源过滤器的指定类型十进制数。

我有办法做到这一点吗?

0 个答案:

没有答案