我需要在XML模式中描述以下内容:一个元素必须出现1次或多次,但是恰好出现一次该元素必须将属性"properties"设置为值"nav"
的例子:
<manifest>
<item href="example" id="02" properties="cover-image" /> <!-- optional item -->
<item href="dummy" id="sample" properties="nav" /> <!-- mandatory item with "nav" value for "properties" attribute -->
<item href="example" id="02" properties="mathlm scripted" /> <!-- optional item -->
</manifest>
我的"最好"尝试是:
<xs:element name="manifest">
<xs:complexType>
<xs:choice>
<xs:element name="item" minOccurs="1" maxOccurs="1" ><!-- at least one (item property="nav")-->
<xs:complexType>
<xs:attribute name="href" type="xs:string" use="required" />
<xs:attribute name="id" type="xs:string" use="required" />
<xs:attribute name="media-type" type="xs:string" use="required" />
<xs:attribute name="fallback" type="xs:string" />
<xs:attribute name="properties" type="xs:string" use="required" fixed="nav" />
<xs:attribute name="media-overlay" type="xs:string" />
</xs:complexType>
</xs:element>
<xs:element name="item" minOccurs="0" maxOccurs="unbounded" >
<xs:complexType>
<xs:attribute name="href" type="xs:string" use="required" />
<xs:attribute name="id" type="xs:string" use="required" />
<xs:attribute name="media-type" type="xs:string" use="required" />
<xs:attribute name="fallback" type="xs:string" />
<xs:attribute name="properties" type="xs:string" />
<xs:attribute name="media-overlay" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:choice>
<xs:attribute name="id" type="xs:string" />
</xs:complexType>
</xs:element>
错误的尝试,因为验证器给我以下错误:
local complex type:内容模型不是确定性的。
很明显,这个模式不是确定性的,因为验证器不能决定他是否应该用这个元素的两个定义中的第一个或第二个来检查遇到的item
元素…
…但我怎么才能做到呢?
使用XSD 1.0无法实现这一点,它需要XSD 1.1和断言。我认为PHP中的默认模式验证器不会支持XSD 1.1。