首页 > 解决方案 > xsd 模式中的常量键

问题描述

我有以下 XSD 架构(摘录):

<xs:key name="type">
  <xs:selector xpath="./datatypes/struct|./datatypes/array" />
  <xs:field xpath="@type" />
</xs:key>
<xs:keyref name="typeref" refer="type">
  <xs:selector xpath="./function/parameter|./function/return|./gloabalVariable|./datatypes/struct/field|./datatypes/array" />
  <xs:field xpath="@typeref" />
</xs:keyref>

说明:我想指定一些我可以在函数、变量和其他类型中引用的类型。

现在我想引用原始类型,它们没有在 XML 中声明,但应该是 XSD 模式中可以引用的常量键。

我尝试过的(但不起作用):

<xs:key name="type">
  <xs:selector xpath="./datatypes/struct|./datatypes/array" />
  <xs:field xpath="@type|'uint8'|'int32'|..." />
</xs:key>

如何在 XSD 架构中指定常量键?

标签: xmlxsdkeyref

解决方案


我想出并为我工作的东西:

<xs:simpleType name="type">
  <xs:union memberTypes="primitiveType complexType" />
</xs:simpleType>
<xs:simpleType name="primitiveType">
  <xs:restriction base="xs:string">
    <xs:enumeration value="int8" />
    <xs:enumeration value="uint8" />
    <xs:enumeration value="int16" />          
    <xs:enumeration value="uint16" /> 
    <xs:enumeration value="int32" /> 
    <xs:enumeration value="uint32" /> 
    <xs:enumeration value="int64" /> 
    <xs:enumeration value="uint64" /> 
    <xs:enumeration value="double" /> 
    <xs:enumeration value="float" /> 
    <xs:enumeration value="string" /> 
    <xs:enumeration value="bool" /> 
  </xs:restriction>
</xs:simpleType>
<xs:simpleType name="complexType">
  <xs:restriction base="xs:IDREF" />
</xs:simpleType>

推荐阅读