views:

47

answers:

3

How can I create an xsd to give me this type of xml structure that can go on infinitely?

<?xml version="1.0" encoding="utf-8" ?>
<SampleXml>
    <Items>
        <Item name="SomeName" type="string">
            This would be the value
        </Item>
        <Item name="SecondName" type="string">
            This is the next string
        </Item>
        <Item name="AnotherName" type="list">
            <Items>
                <Item name="SubName" type="string">
                    A string in a sub list
                </Item>
                <Item name="SubSubName" type="list">
                    <Items>
                        <Item name="HowDoI" type="string">
                            How do I keep this going infinately?
                        </Item>
                    </Items>
                </Item>
            </Items>
        </Item>
    </Items>
</SampleXml>

The only solution I have found has been to just repeat in the xsd as many times as I am willing to copy. Like below.

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
    <xs:element name="SampleXml">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Items">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element maxOccurs="unbounded" name="Item">
                                <xs:complexType mixed="true">
                                    <xs:sequence minOccurs="0">
                                        <xs:element name="Items">
                                            <xs:complexType>
                                                <xs:sequence>
                                                    <xs:element maxOccurs="unbounded" name="Item">
                                                        <xs:complexType mixed="true">
                                                            <xs:sequence minOccurs="0">
                                                                <xs:element name="Items">
                                                                    <xs:complexType>
                                                                        <xs:sequence>
                                                                            <xs:element name="Item">
                                                                                <xs:complexType>
                                                                                    <xs:simpleContent>
                                                                                        <xs:extension base="xs:string">
                                                                                            <xs:attribute name="name" type="xs:string" use="required" />
                                                                                            <xs:attribute name="type" type="xs:string" use="required" />
                                                                                        </xs:extension>
                                                                                    </xs:simpleContent>
                                                                                </xs:complexType>
                                                                            </xs:element>
                                                                        </xs:sequence>
                                                                    </xs:complexType>
                                                                </xs:element>
                                                            </xs:sequence>
                                                            <xs:attribute name="name" type="xs:string" use="required" />
                                                            <xs:attribute name="type" type="xs:string" use="required" />
                                                        </xs:complexType>
                                                    </xs:element>
                                                </xs:sequence>
                                            </xs:complexType>
                                        </xs:element>
                                    </xs:sequence>
                                    <xs:attribute name="name" type="xs:string" use="required" />
                                    <xs:attribute name="type" type="xs:string" use="required" />
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
+4  A: 

Try something like this, using the ref attribute:

<xs:element name="Items">
  <xs:complexType>
    <xs:sequence minOccurs="0" maxOccurs="unbounded">
      <xs:element ref="Item" />
    </xs:sequence> 
  </xd:complexType
 </xd:element>

<xs:element name="Item">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="Items" />
    </xs:sequence> 
  </xd:complexType
 </xd:element>
Julien Lebosquain
+5  A: 

Use <xs:element ref="bla" /> to recursively refer to the elements. A simple example:

<xs:element name="recursive">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="recursive" minOccurs="0" maxOccurs="unbounded" />
    </xs:sequence>
  </xs:complexType>
</xs:element>

Allows you to write this:

<recursive>
  <recursive>
    <recursive />
  </recursive>
  <recursive />
  <recursive>
    <recursive>
      <recursive />
    </recursive>
  </recursive>
</recursive>

Usage of the 'ref' attribute also helps greatly in increasing the readability of your XSD. This is how I would write yours:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;

  <xs:element name="SampleXml">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="Items" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:element name="Items">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="Item" maxOccurs="unbounded" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:element name="Item">
    <xs:complexType mixed="true">
      <xs:sequence>
        <xs:element ref="Items" minOccurs="0" />
      </xs:sequence>
      <xs:attribute name="name" type="xs:string" use="required" />
      <xs:attribute name="type" type="xs:string" use="required" />
    </xs:complexType>
  </xs:element>

</xs:schema>

Note how use of 'ref' even when it's not strictly necessary (such as when SampleXml refers to Items) makes the XSD less of a nested mess and more readable.

Joren
+4  A: 

Try using the ref attribute:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
    <xs:element name="Items">
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="unbounded" name="Item">
                    <xs:complexType mixed="true">
                        <xs:sequence minOccurs="0">
                            <xs:element ref="Items" />
                        </xs:sequence>
                        <xs:attribute name="name" type="xs:string" use="required" />
                        <xs:attribute name="type" type="xs:string" use="required" />
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="SampleXml">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="Items" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
Eric