XSD 复杂枚举
这是我在 stackoverflow 上的第一个问题(我通常能够通过这里已有的答案找出我的问题)。
这与这些问题类似,但我认为我的问题有点不同(而且,XSD 真的很难):
我试图定义一组受限制的“类型”代表参数数据类型。例如
- Binary bit-mapped B/BM 1 byte
- Unsigned Integer U/I 2 bytes
- Alphanumeric ALPHA 1 byte
- etc...
,我想出了以下 xsd:
<xs:complexType name="parameterDataType" abstract="true">
<xs:sequence>
<xs:element name="type" type="xs:string" />
<xs:element name="abbreviation" type="xs:string" />
<xs:element name="length" type="xs:positiveInteger" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="binaryBitMappedType">
<xs:complexContent>
<xs:restriction base="parameterDataType">
<xs:sequence>
<xs:element name="type" type="xs:string" fixed="Binary Bit-Mapped" />
<xs:element name="abbreviation" type="xs:string" fixed="B/BM" />
<xs:element name="length" type="xs:positiveInteger" fixed="1" />
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
etc...
注意:我还尝试使用序列和“类型、缩写、长度”作为 xs:attributes,所以如果这是更好的解决方案,请告诉我。
这工作正常,但它需要 XML 将元素重新定义为它们的受限值(否则它是无效的),有点像这样:
<message>
<id>123</id>
<name>Some status type message</name>
<dataType xsi:type="binaryBitMappedType">
<type>Binary Bit-Mapped</type>
<abbreviation>B/BM</abbreviation>
<length>1</length>
</dataType>
...etc
</message>
<message>
<id>321</id>
<name>A DIFFERENT status type message</name>
<dataType xsi:type="binaryBitMappedType">
<type>Binary Bit-Mapped</type>
<abbreviation>B/BM</abbreviation>
<length>1</length>
</dataType>
...etc
</message>
<message>
<id>456</id>
<name>a continuous value type message</name>
<dataType xsi:type="unsignedIntegerType">
<type>Unsigned Integer</type>
<abbreviation>U/I</abbreviation>
<length>2</length>
</dataType>
...etc
</message>
有没有一种方法可以避免必须在 XML 中重新定义这些值(可以将定义拉入)或者,是否可以在自己的 XML 文件中提供定义,然后根据 xsi:type“导入”这些定义?
还有像这样的东西吗?
<message>
<id>123</id>
<name>Some status type message</name>
<dataType xsi:type="binaryBitMappedType" /> <!-- don't have to repeat the dataType contents -->
...etc
</message>
<message>
<id>321</id>
<name>A DIFFERENT status type message</name>
<dataType xsi:type="binaryBitMappedType" /> <!-- don't have to repeat the dataType contents -->
...etc
</message>
还是我什么都躲着?提前致谢
This is my first question on stackoverflow (I have usually been able to figure out my problems with the answers already here).
This is similar to these questions, but I think my question is a little different (also, XSD is really hard):
I am trying to define a restricted set of "types" that represent parameter data types. E.g.
- Binary bit-mapped B/BM 1 byte
- Unsigned Integer U/I 2 bytes
- Alphanumeric ALPHA 1 byte
- etc...
So I have come up with the following xsd:
<xs:complexType name="parameterDataType" abstract="true">
<xs:sequence>
<xs:element name="type" type="xs:string" />
<xs:element name="abbreviation" type="xs:string" />
<xs:element name="length" type="xs:positiveInteger" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="binaryBitMappedType">
<xs:complexContent>
<xs:restriction base="parameterDataType">
<xs:sequence>
<xs:element name="type" type="xs:string" fixed="Binary Bit-Mapped" />
<xs:element name="abbreviation" type="xs:string" fixed="B/BM" />
<xs:element name="length" type="xs:positiveInteger" fixed="1" />
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
etc...
NOTE WELL: I also tried this with a sequence and "type, abbreviation, length" being xs:attributes, so if that's a better solution let me know.
This works fine but it requires the XML to redefine the element as their restricted values (otherwise it is not valid), a bit like so:
<message>
<id>123</id>
<name>Some status type message</name>
<dataType xsi:type="binaryBitMappedType">
<type>Binary Bit-Mapped</type>
<abbreviation>B/BM</abbreviation>
<length>1</length>
</dataType>
...etc
</message>
<message>
<id>321</id>
<name>A DIFFERENT status type message</name>
<dataType xsi:type="binaryBitMappedType">
<type>Binary Bit-Mapped</type>
<abbreviation>B/BM</abbreviation>
<length>1</length>
</dataType>
...etc
</message>
<message>
<id>456</id>
<name>a continuous value type message</name>
<dataType xsi:type="unsignedIntegerType">
<type>Unsigned Integer</type>
<abbreviation>U/I</abbreviation>
<length>2</length>
</dataType>
...etc
</message>
Is there a way to avoid having to redefine these values in the XML (can the definition be pulled in from the XSD, or assumed from the XSD?) Alternatively, can the definitions be provided in their own XML file and then those be "imported" in based on the xsi:type?
Something a little more like this?
<message>
<id>123</id>
<name>Some status type message</name>
<dataType xsi:type="binaryBitMappedType" /> <!-- don't have to repeat the dataType contents -->
...etc
</message>
<message>
<id>321</id>
<name>A DIFFERENT status type message</name>
<dataType xsi:type="binaryBitMappedType" /> <!-- don't have to repeat the dataType contents -->
...etc
</message>
Or am I on a hiding to nothing? Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
XSD 1.0 中的一般规则是您无法定义共同约束:即一个元素的有效值完全独立于任何其他元素的有效值。
在 XSD 1.1 中,您可以使用断言做您喜欢做的事情。
The general rule in XSD 1.0 is that you can't define co-constraints: that is, the valid values of one element are entirely independent of the valid values of any other element.
In XSD 1.1 you can do what you like, using assertions.