2015-10-19 50 views
1

我有下面的XML(例如SNIPPIT)的情况:XML断言为子属性基于父属性

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

从本质上讲,我想限制的可能值基于属性“选择”父属性“type”的值。我知道断言不会让我看父母的价值,但我认为它应该让我看看孩子的价值观。这意味着我需要将父级别置于父级别,但我不确定将如何写入这些子级属性。如果他们在相同的元素,它会是这样的:

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

或者我完全脱离基地?

回答

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