2012-03-15 77 views
2

使用XSD允許,我要定義可能出現的三種不同形式的複雜元素:XSD:一個元素必須以不同的形式

<Scope Name="foo" /> <!-- no children --> 
<Scope Name="foo" Src="bar" /> <!-- When Src is present, there must be no children! --> 
<Scope Name="foo"><!-- other children --></Scope> 

在第三種情況下,它是明確界定,可能會出現什麼作爲兒童元素(例如,所有三種類型的「範圍」)。重要的部分是具有「Src」屬性的Scope元素必須是空的!

Furhtermore,在不同的地方,我只想要特定類型的元素。例如,在根標籤內部,我想準確地允許第三種類型的Scope元素;在大多數情況下,我會准許所有的情況。這就是問題所在:如何解決這個問題?

我到目前爲止做了什麼:我創建了一個複雜的類型,我可以在其中使用3種情況。但是,我不能使用:

<xs:choice minOccurs="0" maxOccurs="unbounded"> 
    <xs:element name="Scope" type="type_Scope_WithSrc" /> 
    <xs:element name="Scope" type="type_Scope_WithContent" /> 
    <xs:element name="Scope" type="type_Scope_Base" /> 
</xs:choice> 

我試圖創建這些的聯合,但聯合只允許simpleTypes。

我也試着定義一個使用xs:choice來包含它們的整體類型「type_Scope」。但是,XS:選擇將包括XS:元素也將在這種情況下:-(

需要一個名字,你能告訴我怎樣才能解決這個

請不要告訴我,這是不可能的XSD :-(:-(

謝謝

問候 DIVB

回答

2

您可能會得到不同的答案,這取決於您是否想與XSD 1.1或1.0 XSD實現這一目標,我會假設你在1.0 sol之後我將在這裏描述(我不相信1.1是實用的)。

如果您想保留元素名稱並更改其內容,那麼您唯一的選擇是使用xsi:type。而不是選擇,只需使用一個Scope元素;使其類型成爲一個複雜類型,並帶有一個名爲「Name」的屬性。其他兩種類型從這種基本類型擴展。你完成了。

注意:我使用了基本抽象類型作爲一種機制來告知人們其他類型預計會在那裏進行;現實是,即使沒有它,也可以從一開始就使用type_Scope_Base。

Type hierarchy

XSD:

<?xml version="1.0" encoding="utf-8" ?> 
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" 
elementFormDefault="qualified" 
xmlns="http://tempuri.org/XMLSchema.xsd" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 

    <xsd:complexType name="type_Scope_BaseA" abstract="true"> 
    </xsd:complexType> 

    <xsd:complexType name="type_Scope_Base"> 
     <xsd:complexContent> 
      <xsd:extension base="type_Scope_BaseA"> 
       <xsd:attribute name="Name" type="xsd:string"/>  
      </xsd:extension> 
     </xsd:complexContent> 
    </xsd:complexType> 

    <xsd:complexType name="type_Scope_WithSrc"> 
     <xsd:complexContent> 
      <xsd:extension base="type_Scope_Base"> 
       <xsd:attribute name="Src" type="xsd:string"/> 
      </xsd:extension> 
     </xsd:complexContent> 
    </xsd:complexType> 

    <xsd:complexType name="type_Scope_WithContent"> 
     <xsd:complexContent> 
      <xsd:extension base="type_Scope_Base"> 
       <xsd:sequence> 
        <xsd:any maxOccurs="unbounded" namespace="##other" processContents="lax"/> 
       </xsd:sequence> 
      </xsd:extension> 
     </xsd:complexContent> 
    </xsd:complexType> 

    <xsd:element name="root"> 
     <xsd:complexType> 
      <xsd:sequence> 
       <xsd:element ref="Scope"/> 
      </xsd:sequence> 
     </xsd:complexType> 
    </xsd:element> 

    <xsd:element name="Scope" type="type_Scope_BaseA"/> 

</xsd:schema> 

這三個樣品個XML,所有有效。第一個內容模型從type_Scope_Base

<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) --> 
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd"> 
    <Scope xsi:type="type_Scope_Base" Name="Name1"/> 
</root> 

這與type_Scope_WithSrc內容模型。

<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) --> 
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd"> 
    <Scope xsi:type="type_Scope_WithSrc" Name="Name1" Src="Src1"/> 
</root> 

而這與內容模型從type_Scope_WithContent

<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) --> 
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd"> 
    <Scope xsi:type="type_Scope_WithContent" Name="Name1"> 
     <me:hello.you xmlns:me="http://paschidev.com"/> 
    </Scope> 
</root> 

如果你想允許在標籤名稱的變化,相反,你可以放置有置換組,它至少可以給你不XSI解決的頭一個選擇:類型。

然後有基於XSD 1.1的解決方案,但是我會遠離開放環境中的任何東西;不是今天每個人都有一個合規的處理器,更不用說規範本身不是一個建議了。

相關問題