1

I have an XML schema in which I know that an element has to have a particular child, but I do not know the depth at which that child will exist at. Take the following example XML:

<node id="top">
   <node id="inner">
     <event/>
   </node>
</node>

<node id="top">
   <event/>
</node>

The only requirements I have is that an <event> element must be a descendant of the <node id="top"> element, but I do not know at what depth it will occur. In other words, there could be any number of <node id="inner"> elements in between the top <node id="top"> and an <event> element. In the example above, each of those are valid within the schema. My attempted solution was:

<xs:element name="node">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="node" minOccurs="0">
                <xs:complexType>
                     <xs:sequence>
                        <xs:element name="event"/>
                     </xs:sequence>
                   <xs:attribute name="id" fixed="inner"/>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
      </xs:complexType>
      <xs:attribute name="id" fixed="top"/>
</xs:element>

But, as expected, this does not take into account many "depths" of <node> elements that can occur after the <node id="top"> element.

  • Looks like this is mostly a duplicate. The simple answer to this question is that you MUST specify maxOccurs="unbounded" to get unknown recursive depth. –  Mar 08 '13 at 21:55

1 Answers1

0

This is answered! Try something like below

<?xml version="1.0"?> 
<xs:schema targetNamespace="http://test"
  elementFormDefault="qualified" attributeFormDefault="unqualified"
  xmlns:test="http://test"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="nodes">   
      <xs:complexType> 
           <xs:sequence minOccurs="0" maxOccurs="unbounded">
                   <xs:element name="node"  type="test:nodeType" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
 </xs:complexType> 
  </xs:element>

<xs:complexType name="nodeType">  
                <xs:sequence minOccurs="0" maxOccurs="unbounded">
                           <xs:element name="event" minOccurs="0"/>
                           <xs:element name="node" type="test:nodeType" minOccurs="0">

</xs:schema>
Community
  • 1
  • 1
Rohit
  • 583
  • 6
  • 9