我正在尝试使用 XSD 1.1 创建架构定义,其中其他元素的数量取决于另一个元素的属性。例如,BaPath 元素的数量 BaPath 取决于“Conn”元素的属性“service”的值。我写的xsd是
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="Mapping">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Link" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="Env">
<xsd:complexType>
<xsd:attribute name="name" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="UTEST" />
<xsd:enumeration value="TEST" />
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
<xsd:element name="Link">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Conn" maxOccurs="unbounded" />
</xsd:sequence>
<xsd:attribute name="service" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="FILESNF" />
<xsd:enumeration value="MSGSNF" />
<xsd:enumeration value="MSGRT" />
<xsd:enumeration value="FILERT" />
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
<xsd:element name="Conn">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="BaPath" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="BaPath">
<xsd:complexType>
<xsd:attribute name="flow" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:assertion test="@service eq 'MSGRT'">
<xsd:enumeration value="TRS" />
<xsd:enumeration value="ZTRS" />
</xsd:assertion>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
<xsd:element name="Dep">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Env" maxOccurs="unbounded" />
<xsd:element ref="Mapping" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="CfgType">
<xsd:sequence>
<xsd:element ref="Dep" />
</xsd:sequence>
</xsd:complexType>
<xsd:element name="Cfg" type="CfgType"></xsd:element>
</xsd:schema>
</Cfg>
例如,如果 Conn 元素具有属性服务 eq 'MSGRT',则必须有 2 个具有属性 TRS 和 ZTRS 的 BaPath 元素
<Cfg xmlns="http://www.alpha.com/beta">
<Dep>
<Env name="UTEST"/>
<Mapping>
<Link t2s_service="MSGRT">
<Conn>
<BaPath flow="ZTRS"/>
<BaPath flow="TRS"/>
</Conn>
</Link>
</Mapping>
</Dep>
如果 Conn 的服务 eq 'FILESNF' 必须有 3 个具有 FTS、ZFTS 和 MSSDN 属性的 BaPath 元素
我尝试了不同的解决方案,但似乎没有一个有效。是否可以通过 xsd-1.1 的断言来解决这个问题?