title: Apache XMLSchema core XSD/XML parser shows the root element as parent for all children instead of its immediate parent
body: <p>I am trying to parse the <code>XSD</code> using <code>Apache XMLSchema Core</code>. I am able to parse the file and store the <code>Parent</code> and its <code>Child Element</code> information within the <code>HashMap</code>.</p>
  <p>Each <code>Child</code> element is stored in the type <code>XmlSchemaElement</code> which also consists of the <code>Parent</code> information of that child element. When I try to see the information related to a child element it shows the <code>Root</code> element for all instead of its immediate parent.</p>
  <p>For example: In my <code>XSD</code>, <code>RestaurantMenu</code> is the <code>Root</code> element and its immediate children are <code>Food</code>. Also, <code>Food</code> has childrens <code>name</code>, <code>price</code>, <code>calories</code> etc. When I see the parent element for <code>name</code> I would expect it to be <code>Food</code> as that's the immediate Parent but during <code>Debug</code> shows the <code>RestaurantMenu</code> as its parent which is a bit confusing.</p>
  <p>I wanted to know how can I get the information related to the immediate parent for each element.</p>
  <p>Following is my <code>XSD</code> file:</p>
  <pre><code>&lt;xs:schema attributeFormDefault=&quot;unqualified&quot;
      elementFormDefault=&quot;qualified&quot;
      xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;&gt;
      &lt;xs:element name=&quot;RestaurantMenu&quot;&gt;
          &lt;xs:complexType&gt;
              &lt;xs:sequence&gt;
                  &lt;xs:element name=&quot;food&quot; maxOccurs=&quot;5&quot; minOccurs=&quot;0&quot;&gt;
                      &lt;xs:complexType&gt;
                          &lt;xs:sequence&gt;
                              &lt;xs:element type=&quot;xs:string&quot; name=&quot;name&quot; /&gt;
                              &lt;xs:element type=&quot;xs:string&quot; name=&quot;price&quot; /&gt;
                              &lt;xs:element type=&quot;xs:string&quot; name=&quot;description&quot; /&gt;
                              &lt;xs:element type=&quot;xs:short&quot; name=&quot;calories&quot; /&gt;
                          &lt;/xs:sequence&gt;
                      &lt;/xs:complexType&gt;
                  &lt;/xs:element&gt;
              &lt;/xs:sequence&gt;
          &lt;/xs:complexType&gt;
      &lt;/xs:element&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  <p>Following is the code I have which is written using the <code>Apache XmlSchema Core XML Parser</code>:
  I am trying to <code>Debug</code> at the point :</p>
  <pre><code>List&lt;XmlSchemaElement&gt; childElements = getElementType(new QName(&quot;food&quot;));
  System.out.println(childElements.get(0));
  </code></pre>
  <p>Above code will return all the <code>Children</code> element of food: <code>name</code>, <code>price</code>, <code>calories</code>, <code>description</code>, <code>ingredients</code>. Then I am trying to see the parent for one of the element i.e <code>name</code>. Following is the debug path I am taking:</p>
  <pre><code>childElements -&gt; [0] -&gt; namedDelegate -&gt; parentSchema -&gt; items -&gt; [0] -&gt; namedDelegate -&gt; qName.
  </code></pre>
  <p>In this <code>qName</code> I was expecting the <code>food</code> as thats my immediate parent but I get the <code>RestaurantMenu</code> which is my <code>Root</code> xsd element.</p>
  <p>Please find the complete code below:</p>
  <pre><code>public class TestParser {
  
      private static XmlSchemaCollection xmlSchemaCollection;
      private static Map&lt;QName, List&lt;XmlSchemaElement&gt;&gt; xsdElements = new HashMap&lt;QName, List&lt;XmlSchemaElement&gt;&gt;();
      private static List&lt;XmlSchemaElement&gt; schemaElements = new ArrayList&lt;XmlSchemaElement&gt;();
  
      public static void main(String[] args) throws URISyntaxException, FileNotFoundException, UnsupportedEncodingException {
          // Path for the file which is stored within the Resource folder
          String xsdPath = Paths.get(TestParser.class.getClassLoader().getResource(&quot;test.xsd&quot;).toURI()).toFile().getAbsolutePath();
          String filePath = Path.of(xsdPath).toString();
  
          InputStream is = new FileInputStream(filePath);
          xmlSchemaCollection = new XmlSchemaCollection();
  
          // Schema contain the complete XSD content which needs to be parsed
          XmlSchema schema = xmlSchemaCollection.read(new StreamSource(is));
          // schema.write(System.out);
  
          // Get the root element from XSD
          Map.Entry&lt;QName, XmlSchemaElement&gt; entry = schema.getElements().entrySet().iterator().next();
  
          // Get all the elements based on the parent element
          XmlSchemaElement childElement = xmlSchemaCollection.getElementByQName(entry.getKey());
  
          // Call method to get all the child elements
          getChildElementNames(childElement);
  
          // Get the elements type based on choice
          List&lt;XmlSchemaElement&gt; childElements = xsdElements.get(new QName(&quot;food&quot;));
          System.out.println(childElements.get(0));
  
      }
  
      // Method to check for the child elements and return list
      private static void getChildElementNames(XmlSchemaElement element) {
  
          // Get the type of the element
          XmlSchemaType elementType = element != null ? element.getSchemaType() : null;
  
          // Confirm if the element is of Complex type
          if (elementType instanceof XmlSchemaComplexType) {
              // Get all particles associated with that element Type
              XmlSchemaParticle allParticles = ((XmlSchemaComplexType) elementType).getParticle();
  
              // Check particle belongs to which type
              if (allParticles instanceof XmlSchemaSequence) {
                  // System.out.println(&quot;Sequence Schema Type&quot;);
                  final XmlSchemaSequence xmlSchemaSequence = (XmlSchemaSequence) allParticles;
                  final List&lt;XmlSchemaSequenceMember&gt; items = xmlSchemaSequence.getItems();
  
                  items.forEach((item) -&gt; {
                      XmlSchemaElement itemElements = (XmlSchemaElement) item;
                      schemaElements.add(itemElements);
                      addChild(element.getQName(), itemElements);
                      // Call method recursively to get all subsequent element
                      getChildElementNames(itemElements);
                      schemaElements = new ArrayList&lt;XmlSchemaElement&gt;();
                  });
  
              }
          }
      }
  
      // Add child elements based on its parent
      private static void addChild(QName qName, XmlSchemaElement child) {
          List&lt;XmlSchemaElement&gt; values = xsdElements.get(qName);
          if (values == null) {
              values = new ArrayList&lt;XmlSchemaElement&gt;();
          }
          values.add(child);
          xsdElements.put(qName, values);
      }
  }
  </code></pre>