2012-06-29 31 views
1

我正在嘗試爲xsd中的元素應用模式。在xsd中寫入模式<a attributes><img/></a>

元素是XHTML類型。

我想要應用這樣的模式。

<a attributes="some set of attributes"><img attributes="some set of attribtes"/></a> 

規則:

<a> tag with attributes followed by <img> with attributes. 

樣品有效數據:

<a xlink:href="some link" title="Image" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/1999/xhtml"> 
    <img alt="No Image" title="No Image" xlink:href="soem path for image" xlink:title="Image" xmlns="http://www.w3.org/1999/xhtml" xmlns:xlink="http://www.w3.org/1999/xlink" /> 
    </a> 

無效:

<a>data<img/></a>--Data Present, no attributes 
    <a><img>abcd</img></a>--data Present, No attributes 
    <a><img/></a>---No attributes 

任何一個可以建議如何寫模式這一點。

 <xsd:restriction base="xs:string"> 
      <xs:pattern value="Need help"/>    
     </xsd:restriction> 

謝謝。

+0

爲什麼不從有效的XML生成xsd然後用它來驗證其他xml? – Urik

回答

1

XSD pattern構面用於使用正則表達式來約束簡單類型(即表示該類型實例的文字字符串集合)的「詞法空間」。它不會幫助你要求某些元素必須具有屬性。

如果你想具體屬性是存在(例如在img元件上的a元素titlexlink:hreftitlealt),在架構這樣做最簡單的方法是通過根據需要聲明的那些屬性。對於XHTML 1.0的模式嚴格,例如(在http://www.w3.org/TR/xhtml1-schema/#xhtml1-strict)宣佈雙方srcalt作爲被要求在img

<xs:element name="img"> 
    <xs:complexType> 
    <xs:attributeGroup ref="attrs"/> 
    <xs:attribute name="src" use="required" type="URI"/> 
    <xs:attribute name="alt" use="required" type="Text"/> 
    ... 
    </xs:complexType> 
</xs:element> 

如果你想要的僅僅是需要使用一些屬性,但你不不關心它,XSD不會讓任務變得簡單:在XSD 1.0中,沒有方便的方式來說「我不在乎哪個屬性出現,但必須有一個」。你可以用這個通過使用XSD 1.1中的斷言或者除了你的XSD模式之外使用Schematron模式來強制執行這樣一個約束(即使像我這樣的觀察者覺得有點奇怪)。

相關問題