我怎样才能允许一个元素包含一个选择的子元素或一个数字?
How can I allow an element to contain one of a choice of child elements OR a number?
我正在尝试开发 Pipes and Filters pattern 的实现,我希望允许用户在配置文件中的管道内指定过滤器。 XML 是一个不错的选择,因为它是一种分层语言 - 可以通过嵌套 XML 对应于特定类型过滤器的元素来指定管道。
我已经尝试定义少量过滤器和一个 "InputType" 类型定义,其中包含一个 xsd:choice
元素到 select 之间。但是,choice
元素似乎只允许在子元素之间进行选择,而不是其他可能的数据类型,例如 xs:string
或 xs: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>
这将需要架构允许多个元素,这些元素将被解释为指定类型的数字源过滤器或十进制数。
我有办法做到这一点吗?
不,没有干净的方法可以做到这一点。在 XSD 1.1 中,您可以定义混合内容,然后使用断言强制内容为一件事或另一件事,但即便如此,内容也会被键入为字符串而不是数字。
一般来说,XSD的设计并不是你可以设计任何你喜欢的XML,然后在XSD中描述它。它的设计基础是您将使用 XSD 提供的构建块来设计您的 XML。
我正在尝试开发 Pipes and Filters pattern 的实现,我希望允许用户在配置文件中的管道内指定过滤器。 XML 是一个不错的选择,因为它是一种分层语言 - 可以通过嵌套 XML 对应于特定类型过滤器的元素来指定管道。
我已经尝试定义少量过滤器和一个 "InputType" 类型定义,其中包含一个 xsd:choice
元素到 select 之间。但是,choice
元素似乎只允许在子元素之间进行选择,而不是其他可能的数据类型,例如 xs:string
或 xs: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>
这将需要架构允许多个元素,这些元素将被解释为指定类型的数字源过滤器或十进制数。
我有办法做到这一点吗?
不,没有干净的方法可以做到这一点。在 XSD 1.1 中,您可以定义混合内容,然后使用断言强制内容为一件事或另一件事,但即便如此,内容也会被键入为字符串而不是数字。
一般来说,XSD的设计并不是你可以设计任何你喜欢的XML,然后在XSD中描述它。它的设计基础是您将使用 XSD 提供的构建块来设计您的 XML。