2

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?

james.garriss
  • 12,959
  • 7
  • 83
  • 96
Zany
  • 308
  • 1
  • 4
  • 18
  • Possible duplicate of [Recursion in an XML schema?](http://stackoverflow.com/questions/148988/recursion-in-an-xml-schema) – james.garriss Feb 09 '16 at 15:29

1 Answers1

4

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>
Petru Gardea
  • 21,373
  • 2
  • 50
  • 62