2011-03-22 38 views
0

我們在編寫正確(有效)xsd時發生了一些問題: - xsd應在http://www.w3.org/2001/03/webdata/xsv上驗證 - 應該可以添加我們需要在'xsd'中指定'標題'屬性值。這是一個(極其簡化的)XML結構。這是一個極其簡化的XML結構: 如何爲具有相同名稱但含有不同內容的元素編寫有效的xsd

1)這個w作爲我們的第一個版本(顯然是無效的):你不能有兩個名稱爲「input」的元素,其中不同。

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> 
    <xsd:element name="test"> 
     <xsd:complexType> 
      <xsd:sequence> 
       <xsd:element name="input"> 
        <xsd:complexType> 
         <xsd:attribute name="title" fixed="Pretty title" use="required"/> 
        </xsd:complexType> 
       </xsd:element> 
       <xsd:element name="input"> 
        <xsd:complexType> 
         <xsd:attribute name="title" fixed="Different title" use="required"/> 
        </xsd:complexType> 
       </xsd:element> 
      </xsd:sequence> 
     </xsd:complexType> 
    </xsd:element> 

2)第二次嘗試:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> 
<xsd:element name="test" type="Test"/> 
<xsd:complexType name="input1"> 
    <xsd:attribute name="title" fixed="Pretty title" use="required"/> 
</xsd:complexType> 
<xsd:complexType name="input2"> 
    <xsd:attribute name="title" fixed="Different title" use="required"/> 
</xsd:complexType> 
<xsd:complexType name="Test"> 
    <xsd:sequence> 
     <xsd:element name="input" type="input1"/> 
     <xsd:element name="input" type="input2"/> 
    </xsd:sequence> 
</xsd:complexType> 

  • w3.org想出瞭如前相同的錯誤消息:輸入:{無}的非法重新聲明
  • xmlspy告訴我們這是一個有效的xsd
  • sql server引發錯誤:類型'Test'的內容模型包含兩個具有相同名稱'input'和不同類型,nillability或值約束的元素。

如果有人能夠給我們一些見解,這將是很好的。

Manu。

ps:實際上,我們的XML結構比實例更復雜。我們正在構建一個Web表單生成器。 這裏還有一個(仍然簡單)例如:

<?xml version="1.0" encoding="UTF-8"?> 
<ZForm title="main title" attachment="attachment1" type="NM08"> 
<Part title="part title"> 
    <SubTitle code="I.1)" title="title 1"/> 
    <ShortText title="short text title 1"/> 
    <SubTitle code="I.2)" title="title 2" subtitle="subtitle 1"/> 
    <SelectList type="select type" title="select title"/> 
    <ShortText title="short text title 2"/> 
    <MultiSelectList type="multiType2" title="multi select title"/>  
    <RadioButtonList type="yesNo" title="lala"/> 
    <SubTitle code="I.3)" title="some other title" subtitle="what?"/> 
    <MultiSelectList type="multiType2" title=""/> 
</Part> 
<Part title="second part title"> 
    <Repeater add="add a new repeater item" remove="remove last repeating part"> 
     <RepeatingPart> 
      <SubTitle code="II.1)" title="tiiiiiitle"/> 
      <ShortText/> 
     </RepeatingPart> 
    </Repeater> 
</Part> 
</ZForm> 
+0

您是否嘗試過使用屬性組?你有多少個'input'聲明需要什麼? – 2011-03-22 08:55:37

+0

據我所知,我們不能使用屬性組,因爲'fixed'屬性在兩種情況下都是不同的。 正因如此,這就是爲什麼_requirement_聲明2個不同的輸入元素。 – TweeZz 2011-03-22 09:51:36

+0

因此,您可以使用兩個屬性組,但在您的輸入類型定義中,只需指定可以使用任一組但不能同時使用時間。我不確定我頭頂的語法,但我認爲這是可能的。? – 2011-03-23 04:46:25

回答

2

的XSD規範定義的約束「元素聲明一致」,其作用是,當兩個同級元素具有相同的名稱,它們還必須具有相同的類型。

XMLSpy因不執行規範中某些更尷尬的規則而衆所周知。我不知道是不是因爲他們沒有開始編寫代碼,或者他們覺得他們的版本是否有所改進,但使用一致性方法稍微有點兒的工具來檢查模式總是一個好主意更嚴格(Xerces實際上比W3C MSV服務更強大)。

+0

這是否意味着我們不能爲我在最後粘貼的xml結構編寫「嚴格」模式定義? xsd 1.1呢?那會給我們足夠的自由來做到這一點嗎? – TweeZz 2011-03-22 11:55:30

相關問題