Reputation: 328
I am trying to design and implement a recursive element in an XML schema, but I'm not very good with XML in general. Any ideas on how to design it?
Upvotes: 2
Views: 5296
Reputation: 21638
The model below is based on an authoring style where the element declaration is global and the recursiveness is achieved through referencing the element definition.
<?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>
Alternatively, you may achieve the same by re-using a type:
<?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>
Or you can go somewhere in between:
<?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>
Valid sample XML:
<?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>
Upvotes: 4