XML基于父属性的子属性的Assert

时间:2015-10-19 19:24:04

标签: xml validation xsd assert

我遇到以下XML(示例snippit)的情况:

<foo type="color">
  <bar choice="red"/>
  <bar choice="blue"/>
</foo>
<foo type="shape">
  <bar choice="circle"/>
  <bar choice="square"/>
</foo>

基本上,我想根据父属性“type”的值限制属性“choice”的可能值。我知道断言不会让我看一下父值,但我认为它应该让我看看孩子们的价值观。这意味着我需要将其放在父级别,但我不确定如何编写它来引用子属性。如果它们属于同一个元素,那就是这样的:

<assert test="(@type='color' and @choice=('red','blue')) or (@type='shape' and @choice=('circle','square'))"/>

还是我完全偏离基地?

1 个答案:

答案 0 :(得分:0)

引用子元素使用的属性:child_name/@attributeName: 在你的情况下,试试这个:

<assert test="(@type='color' and bar/@choice=('red','blue')) or (@type='shape' and bar/@choice=('circle','square'))"/>.

注意:使用此断言存在一些问题:例如,此XML文件有效:

<foo type="color">
  <bar choice="red"/>
  <bar choice="aaaa"/>
</foo>
<foo type="shape">
  <bar choice="bbbb"/>
  <bar choice="square"/>
</foo>

我建议您使用xs:alternative