XSD 1.1:断言枚举中是否包含属性的值



我有元素Config,它包含3个属性:设备、名称和值。

<Config device="phone" name="id" value="1111" />

基于@name的值,我想验证属性@value是否为字符串、int或布尔值。我使用xs:alternative创建了模式,该模式允许基于属性更改元素的类型。

<xs:element name="Config" type="configType">
<xs:alternative type="configInt" test="@name = ('id','port')" />
<xs:alternative type="configString" test="@name = ('deviceUdid','company')" />
<xs:alternative type="configBoolean" test="@name = ('isRunning','isStopped')" />
</xs:element>
<!-- Base for Config -->
<xs:complexType name="configType">
<xs:attribute name="device" type="devices" />
</xs:complexType>
<!-- Alternative for int -->
<xs:complexType name="configInt">
<xs:complexContent>
<xs:extension base="configType">
<xs:attribute name="name" type="configIntList" />
<xs:attribute name="value" type="xs:int" />
</xs:extension>
</xs:complexContent>
</xs:complexType>
<!-- Alternative for string-->
<xs:complexType name="configString">
<xs:complexContent>
<xs:extension base="configType">
<xs:attribute name="name" type="configStringList" />
<xs:attribute name="value" type="xs:string" />
</xs:extension>
</xs:complexContent>
</xs:complexType>
<!-- Alternative for boolean -->
<xs:complexType name="configBoolean">
<xs:complexContent>
<xs:extension base="configType">
<xs:attribute name="name" type="configBooleanList" />
<xs:attribute name="value" type="xs:boolean" />
</xs:extension>
</xs:complexContent>
</xs:complexType>

对于每个config:configBoolean、configInt和configString,我都有属性"的可能值的枚举;名称";。

<xs:simpleType name="configStringList">
<xs:restriction base="xs:string">
<xs:enumeration value="deviceUdid" />
<xs:enumeration value="company" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="configIntList">
<xs:restriction base="xs:string">
<xs:enumeration value="id" />
<xs:enumeration value="port" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="configBooleanList">
<xs:restriction base="xs:string">
<xs:enumeration value="isRunning" />
<xs:enumeration value="isStopped" />
</xs:restriction>
</xs:simpleType>

我的问题是-我是否能够在xs:alternative中使用此枚举进行测试,而不是列出@name属性的所有可能值?我想完成这样的事情:

<xs:element name="Config" type="configType">
<xs:alternative type="configInt" test="@name = configIntList" />
<xs:alternative type="configString" test="@name = configStringList" />
<xs:alternative type="configBoolean" test="@name = configBooleanList" />
</xs:element>

虽然XPath 2和更高版本允许像@name castable as ns1:configIntList这样的表达式/检查,即允许您检查例如name属性的值是否可以强制转换为某个类型,例如configIntList类型,但我认为这对您的用例没有帮助;XPath2允许检查类型替换的子集似乎受到了很大的限制,并且只允许使用内置类型,因此您无法访问正在处理的模式中的类型。

最新更新