XSD 1.1条件类型赋值<alternative test ="">检查元素是否没有设置属性?

ton*_*nix 3 xml xpath xsd-validation 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>
Run Code Online (Sandbox Code Playgroud)

在此示例中,'alternative test =""'检查TimeTravel元素的属性"direction"是否具有值"Future"或"Past".我应该如何编写XPath查询以检查例如当前元素是否没有"direction"属性?

kjh*_*hes 7

中的XPath "@direction"将测试对于存在一个的direction当前元素上属性:

<alternative test="@direction" type="DirectionType"/>
Run Code Online (Sandbox Code Playgroud)

中的XPath "not(@direction)"将测试对于不存在一个的direction当前元素上属性:

<alternative test="not(@direction)" type="NoDirectionType"/>
Run Code Online (Sandbox Code Playgroud)

另请注意,alternative/@test可以完全省略该属性以提供默认类型.

<alternative type="DefaultType"/>
Run Code Online (Sandbox Code Playgroud)

根据OP的后续问题更新以解决CTA子集模式

所以这<alternative test="@direction='a_value' and not(@another_attribute)"/>是正确的,并会使它正确吗?

是的,但请注意,默认情况下,您的XSD处理器可能会使用XPath CTA(条件类型分配)子集.(例如,Xerces,因此大多数基于Xerces的工具都这样做.)如果是这种情况,您将收到如下所示的错误:

c-cta-xpath:在CTA评估期间,XPath表达式'not(@direction)'无法在'cta-subset'模式下成功编译.

C-CTA-的xpath:XPath表达式 '@方向=' a_value '而不是(@another_attribute)' 不能CTA评估期间在 'CTA-子集' 模式编译成功.

要使用完整的XPath 2.0而不是CTA子集,请相应地配置您的工具.例如,对于Xerces,将以下功能设置为"true":

http://apache.org/xml/features/validation/cta-full-xpath-checking
Run Code Online (Sandbox Code Playgroud)

在oXygen中,有一个复选框Options > Preferences > XML > XML Parser > XML Schema,可以为您控制该功能的值.

使用完整的XPath 2.0,是的,您可以and按照您在评论中建议的方式使用.

  • 您可以使用[**boolean expressions**](http://www.w3.org/TR/xpath/#booleans)使用`和`,`或`和`not`来构建更复杂的条件. (2认同)