<!--inline alternative type definitions --> <element name="TimeTravel" type="TravelType"> <alternative test="@direction='Future'"> <complexType> <complexContent> <restriction base="TravelType" .... <!-- some past travel related elements go here --> </complexType> </alternative> <alternative test="@direction='Past'"> <complexType> <complexContent> <restriction base="TravelType" .... <!-- some future travel related elements go here --> </complexType> </alternative> </element> OR <!--Named alternative type definitions --> <element name="TimeTravel" type="TravelType"> <alternative test="@direction='Future' type="FutureTravelType"/> <alternative test="@direction='Past' type="PastTravelType"/> </element>
在此示例中,’alternative test =“”’检查TimeTravel元素的属性“direction”是否具有值“Future”或“Past”.我应该如何编写XPath查询以检查是否例如当前元素没有“方向”属性?
XPath“@direction”将测试当前元素是否存在方向属性:<alternative test="@direction" type="DirectionType"/>
XPath“not(@direction)”将测试当前元素是否缺少direction属性:
<alternative test="not(@direction)" type="NoDirectionType"/>
另请注意,可以完全省略替代/ @ test属性以提供默认类型.
<alternative type="DefaultType"/>
根据OP的后续问题更新以解决CTA子集模式
So this
<alternative test="@direction='a_value' and not(@another_attribute)"/>
is correct and would make it right?
是的,但请注意,默认情况下,您的XSD处理器可能会使用XPath CTA(Conditional Type Assignment)子集. (例如,Xerces,因此大多数基于Xerces的工具都这样做.)如果是这种情况,您将收到如下所示的错误:
c-cta-xpath: The XPath expression ‘not(@direction)’ couldn’t compile
successfully in ‘cta-subset’ mode, during CTA evaluation.c-cta-xpath: The XPath expression ‘@direction=’a_value’ and
not(@another_attribute)’ couldn’t compile successfully in ‘cta-subset’
mode, during CTA evaluation.
要使用完整的XPath 2.0而不是CTA子集,请相应地配置您的工具.例如,对于Xerces,将以下功能设置为“true”:
http://apache.org/xml/features/validation/cta-full-xpath-checking
在oXygen中,选项>中有一个复选框.偏好> XML> XML分析器> XML Schema,它将为您控制功能的价值.
使用完整的XPath 2.0,是的,您可以按照您在评论中建议的方式使用.