2015-10-19 74 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