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子集模式

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,是的,您可以按照您在评论中建议的方式使用.

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念