XSD 1.1 条件类型分配<alternative test=“">检查元素是否没有设置属性? [英] XSD 1.1 Conditional Type Assignment <alternative test=""> to check if an element hasn't an attribute set?

查看:29
本文介绍了XSD 1.1 条件类型分配<alternative test=“">检查元素是否没有设置属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下是否有人知道如何进行 XSD 1.1 条件类型分配检查元素是否没有使用 XPath 查询的属性,例如:

<元素名称="TimeTravel" type="TravelType"><alternative test="@direction='Future'"><复杂类型><复杂内容><restriction base="TravelType"....<!-- 一些过去旅行相关的元素放在这里--></complexType></替代方案><alternative test="@direction='Past'"><复杂类型><复杂内容><restriction base="TravelType"....<!-- 一些未来旅行相关的元素在这里--></complexType></替代方案></元素>或者<!--命名的替代类型定义--><元素名称="TimeTravel" type="TravelType"><alternative test="@direction='Future' type="FutureTravelType"/><alternative test="@direction='Past' type="PastTravelType"/></元素>

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

解决方案

XPath "@direction" 将测试方向的存在当前元素上的 code> 属性:

XPath "not(@direction)" 将测试当前元素上 direction 属性的缺失:

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

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

<块引用>

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

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

<块引用>

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

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

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

http://apache.org/xml/features/validation/cta-full-xpath-checking

在 oXygen 中,Options > 中有一个复选框.首选项XML >XML解析器XML 模式,它将为您控制功能的价值.

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

I would like to ask if someone knows how to make an XSD 1.1 Conditional Type Assignment check if an element hasn't an attribute using the XPath query, e.g.:

<!--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>

In this example the 'alternative test=""' checks whether the attribute "direction" of the TimeTravel element has a value of "Future" or "Past". How should I write the XPath query to check if e.g. there's no "direction" attribute for the current element?

解决方案

The XPath "@direction" will test for the presence of a direction attribute on the current element:

<alternative test="@direction" type="DirectionType"/>

The XPath "not(@direction)" will test for the absence of a direction attribute on the current element:

<alternative test="not(@direction)" type="NoDirectionType"/>

Note also that the alternative/@test attribute can be omitted altogether to provide a default type.

<alternative type="DefaultType"/>

Update to address CTA subset mode per OP's follow-up question

So this <alternative test="@direction='a_value' and not(@another_attribute)"/> is correct and would make it right?

Yes, but be aware that your XSD processor may use the XPath CTA (Conditional Type Assignment) subset by default. (Xerces, and therefore most Xerces-based tools, do this, for example.) If this is the case, you will get an error that looks something like this:

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.

To use full XPath 2.0 rather than the CTA subset, configure your tool accordingly. For example, for Xerces, set the following feature to 'true':

http://apache.org/xml/features/validation/cta-full-xpath-checking

In oXygen, there's a checkbox in Options > Preferences > XML > XML Parser > XML Schema that will control the value of the feature for you.

Using full XPath 2.0, yes, you can use and in the manner you suggest in your comment.

这篇关于XSD 1.1 条件类型分配&lt;alternative test=“"&gt;检查元素是否没有设置属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆