2015-10-08 69 views
1

我需要在XML模式中描述以下內容:一個元素必須出現一次或多次,但是該元素的一次出現必須具有該屬性「屬性」 設置值 「導航」一個或多個元素,至少一個具有給定值的屬性

例子:

<manifest> 
    <item href="example" id="02" properties="cover-image" /> <!-- optional item --> 
    <item href="dummy" id="sample" properties="nav" /> <!-- mandatory item with "nav" value for "properties" attribute --> 
    <item href="example" id="02" properties="mathlm scripted" /> <!-- optional item --> 
</manifest> 

我的 「最好」 的嘗試是:

<xs:element name="manifest"> 
     <xs:complexType> 
     <xs:choice> 
      <xs:element name="item" minOccurs="1" maxOccurs="1" ><!-- at least one (item property="nav")--> 
      <xs:complexType> 
       <xs:attribute name="href" type="xs:string" use="required" /> 
       <xs:attribute name="id" type="xs:string" use="required" /> 
       <xs:attribute name="media-type" type="xs:string" use="required" /> 
       <xs:attribute name="fallback" type="xs:string" /> 
       <xs:attribute name="properties" type="xs:string" use="required" fixed="nav" /> 
       <xs:attribute name="media-overlay" type="xs:string" /> 
      </xs:complexType> 
      </xs:element> 
      <xs:element name="item" minOccurs="0" maxOccurs="unbounded" > 
      <xs:complexType> 
       <xs:attribute name="href" type="xs:string" use="required" /> 
       <xs:attribute name="id" type="xs:string" use="required" /> 
       <xs:attribute name="media-type" type="xs:string" use="required" /> 
       <xs:attribute name="fallback" type="xs:string" /> 
       <xs:attribute name="properties" type="xs:string" /> 
       <xs:attribute name="media-overlay" type="xs:string" /> 
      </xs:complexType> 
      </xs:element> 
     </xs:choice> 
     <xs:attribute name="id" type="xs:string" /> 
     </xs:complexType> 
    </xs:element> 

壞嘗試雖然,因爲驗證給了我以下錯誤:

local complex type: The content model is not determinist.

很顯然,這個模式是不確定的,因爲如果他是應該與第一檢查中遇到item元素驗證不能決定或者這個元素的2個定義中的第二個......
......但是我怎麼能做到這一點呢?這可能嗎?

+1

你可以使用模式1.1嗎?在這種情況下,你可以使用斷言('​​')。 – potame

+0

@potame我現在找不到答案,但我認爲我不能(在PHP中使用'DOMDocument :: schemaValidate()')。幾個月前我有另一個問題(條件子元素取決於「type」屬性我記得我發現的答案是:當php集成模式1.1時,這將是可能的......我將在那個方向進一步搜索。 – fpierrat

回答

3

這是使用XSD 1.0無法實現的,它需要XSD 1.1和斷言。我不認爲你在PHP中的默認模式驗證器將支持XSD 1.1。

+0

然後,我要放棄這個測試,或者我會嘗試像第一個XSLT-step轉換item [@properties ='nav']'到'item_nav'中,然後第二個XSD-step驗證這個需要'item_nav'元素和x可選的'item'元素......對於我認爲是一個簡單的需求來說,似乎相當長的一段路:-( – fpierrat

相關問題