xml – XSD 1.1条件类型赋值,用于检查元素是否没有设置属性?
我想问一下,如果某个元素没有使用XPath查询的属性,有人知道如何进行XSD 1.1条件类型赋值检查,例如:
<!--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子集模式
是的,但请注意,默认情况下,您的XSD处理器可能会使用XPath CTA(Conditional Type Assignment)子集. (例如,Xerces,因此大多数基于Xerces的工具都这样做.)如果是这种情况,您将收到如下所示的错误:
要使用完整的XPath 2.0而不是CTA子集,请相应地配置您的工具.例如,对于Xerces,将以下功能设置为“true”: http://apache.org/xml/features/validation/cta-full-xpath-checking 在oXygen中,选项>中有一个复选框.偏好> XML> XML分析器> XML Schema,它将为您控制功能的价值. 使用完整的XPath 2.0,是的,您可以按照您在评论中建议的方式使用. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |