2014-04-02 133 views
0

如果元素值的組合是唯一的,是否可以通過模式進行驗證?唯一XML屬性組合

有效期:

<Hardware ID="1"> 
    <COMPort>COM1</COMPort> 
    <BaudRate>9600</BaudRate> 
</Hardware> 
<Hardware ID="2"> 
    <COMPort>COM2</COMPort> 
    <BaudRate>9600</BaudRate> 
</Hardware> 
<Hardware ID="3"> 
    <COMPort>COM1</COMPort> 
    <BaudRate>115200</BaudRate> 
</Hardware> 

無效:

<Hardware ID="1"> 
    <COMPort>COM1</COMPort> 
    <BaudRate>9600</BaudRate> 
</Hardware> 
<Hardware ID="2"> 
    <COMPort>COM2</COMPort> 
    <BaudRate>9600</BaudRate> 
</Hardware> 
<Hardware ID="3"> 
    <COMPort>COM1</COMPort> 
    <BaudRate>9600</BaudRate> 
</Hardware> 

這段代碼是無效的,因爲舒適與波特率的組合是不是唯一的。

回答

2

是的,這是可能的:對硬件元素的父元素聲明,使用類似

<xs:unique name="u"> 
    <xs:selector xpath="Hardware"/> 
    <xs:field xpath="COMPort"/> 
    <xs:field xpath="BaudRate"/> 
</xs:unique>