[英]Restrict XSD attribute value based on another attribute value
我在XML中有這個:
<Const Name="a" Value="1.0"/>
<Const Name="b" Value="1"/>
<Const Name="c" Value="A"/>
<Const Name="d" Value="B"/>
現在僅對於Name =“b” Const
, Value
必須為1,2,3或4.不允許其他值。 其他Const
可能包含其他值,如圖所示。 我如何在XSD中表達這一點?
到目前為止我有這個:
<xs:element name="Const">
<xs:complexType>
<xs:attribute name="Value" type="xs:string" use="required"/>
<xs:attribute name="Name" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
我使用XSD 1.0,似乎:VS2013 ......所以“替代”對我不起作用......遺憾的是......
您可以使用XSD 1.1的條件類型分配執行此操作:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
elementFormDefault="qualified"
vc:minVersion="1.1">
<xs:element name="Const">
<xs:alternative test="@Name = 'a'" type="aType"/>
<xs:alternative type="otherType"/>
</xs:element>
<xs:complexType name="aType">
<xs:sequence/>
<xs:attribute name="Name" type="xs:string"/>
<xs:attribute name="Value">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="4"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
<xs:complexType name="otherType">
<xs:sequence/>
<xs:attribute name="Name" type="xs:string"/>
<xs:attribute name="Value" type="xs:string"/>
</xs:complexType>
</xs:schema>
示例解決方案uss xs:assert假設您正在使用XSD 1.1 :
<xs:element name="Const">
<xs:complexType>
<xs:attribute name="Value" type="xs:string" use="required"/>
<xs:attribute name="Name" type="xs:string" use="required"/>
<xs:assert test="(@Name='b' and @Value=('1', '2', '3', '4'))
or
(@Name='a' and @Value=('1.0', '2.0', '3.0', '4.0'))
or
(@Name='c')
or
(@Name='d')"></xs:assert>
</xs:complexType>
</xs:element>
請注意,這只是一個示例,您可能需要更改它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.