Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What xsd will let an element have itself as a sub element infinitely?

Tags:

xml

xsd

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">
    <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>
like image 785
David Basarab Avatar asked May 21 '10 20:05

David Basarab


People also ask

What is complexType and simpleType in XSD?

XSD elements can be of type simpleType , complexType , or anyType . An element of type simpleType contains only text. It cannot have attributes and elements. An element of type complexType can contain text, elements, and attributes.

What is minOccurs and maxOccurs in XSD?

The minOccurs attribute specifies the minimum number of times that the element can occur. It can have a value of 0 or any positive integer. The maxOccurs attribute specifies the maximum number of times that the element can occur.

What is Nillable true in XSD?

The nillable attribute can be defined on an xsd:element within an XML schema. It specifies that the xsi:nil attribute is valid for the element. If an XML schema has defined the nillable attribute as true, it is mapped as a required attribute and is included in the document, however, its values are nullified.

What does complexType mean in XSD?

The complexType element defines a complex type. A complex type element is an XML element that contains other elements and/or attributes.


2 Answers

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">

  <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.

like image 86
Joren Avatar answered Nov 16 '22 01:11

Joren


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>
like image 27
Julien Lebosquain Avatar answered Nov 15 '22 23:11

Julien Lebosquain