2012-09-26 64 views
2

我試圖在XML模式中設計和實現遞歸元素,但我對XML一般不太好。有關如何設計它的任何想法?XML中的遞歸元素

+0

[XML模式中的遞歸?]的可能重複(http://stackoverflow.com/questions/148988/recursion-in-an-xml-schema) –

回答

4

下面的模型基於創作風格,其中元素聲明是全局的,並且遞歸是通過引用元素定義來實現的。

<?xml version="1.0" encoding="utf-8" ?> 
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)--> 
<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:element name="recursive" type="Trecursive"/> 
    <xsd:complexType name="Trecursive"> 
     <xsd:sequence> 
      <xsd:element name="recursive" type="Trecursive" minOccurs="0"/> 
     </xsd:sequence> 
    </xsd:complexType> 
</xsd:schema> 

或者你可以在兩者之間:

<?xml version="1.0" encoding="utf-8" ?> 
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)--> 
<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:element name="recursive" type="Trecursive"/> 
    <xsd:complexType name="Trecursive"> 
     <xsd:sequence> 
      <xsd:element ref="recursive" minOccurs="0"/> 
     </xsd:sequence> 
    </xsd:complexType> 
</xsd:schema> 

有效樣本XML:

<?xml version="1.0" encoding="utf-8" ?> 
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)--> 
<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:element name="recursive"> 
     <xsd:complexType> 
      <xsd:sequence> 
       <xsd:element ref="recursive" minOccurs="0"/> 
      </xsd:sequence> 
     </xsd:complexType> 
    </xsd:element> 
</xsd:schema> 

或者,可以通過重新使用類型實現相同的

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

爲我的目標它是完美的第一個例子!謝謝 ;) – Zany