title: How to Programmatically Update and Add a Complex Type with a Sequence to an XSD
body: <p>I'm trying to programatically update an an existing XSD in java that looks like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xs:schema xmlns="com/company/common" xmlns:xs="http://www.w3.org/2001/XMLSchema"
      targetNamespace="com/company/common/" elementFormDefault="qualified"&gt;
      &lt;xs:include schemaLocation="DerivedAttributes.xsd" /&gt;
      &lt;xs:element name="MyXSD" type="MyXSD" /&gt;
      &lt;xs:complexType name="Container1"&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="element1" type="element1" minOccurs="0"
                  maxOccurs="unbounded" /&gt;
              &lt;xs:element name="element2" type="element2" minOccurs="0"
                  maxOccurs="unbounded" /&gt;
          &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
      &lt;xs:complexType name="Container2"&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="element3" type="Type1" minOccurs="0" /&gt;
              &lt;xs:element name="element4" type="Type2" minOccurs="0" /&gt;
          &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>I am able to add a new element to Container 1 very easily with DOM and XPath like this:</p>
  
  <pre><code>Document doc = DocumentBuilderFactory.newInstance()
              .newDocumentBuilder().parse(
                      new InputSource("test.xsd"));
  
      // use xpath to find node to add to
      XPath xPath = XPathFactory.newInstance().newXPath();
      NodeList nodes = (NodeList) xPath
              .evaluate(
                      "/schema/complexType[@name=\"Container1\"]/sequence",
                      doc.getDocumentElement(), XPathConstants.NODESET);
  
      // create element to add
      org.w3c.dom.Element newElement = doc.createElement("xs:element");
      newElement.setAttribute("name", "element5");
      newElement.setAttribute("type", "type5");
      newElement.setAttribute("minOccurs", "0");
      newElement.setAttribute("manOccurs", "unbounded");
  
      nodes.item(0).appendChild(newElement);
  
      // output
      TransformerFactory.newInstance().newTransformer().transform(
              new DOMSource(doc.getDocumentElement()),
              new StreamResult(System.out));
  </code></pre>
  
  <p>And I am able to get this result:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xs:schema xmlns="com/company/common" xmlns:xs="http://www.w3.org/2001/XMLSchema"
      targetNamespace="com/company/common/" elementFormDefault="qualified"&gt;
      &lt;xs:include schemaLocation="DerivedAttributes.xsd" /&gt;
      &lt;xs:element name="MyXSD" type="MyXSD" /&gt;
      &lt;xs:complexType name="Container1"&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="element1" type="element1" minOccurs="0"
                  maxOccurs="unbounded" /&gt;
              &lt;xs:element name="element2" type="element2" minOccurs="0"
                  maxOccurs="unbounded" /&gt;
              &lt;xs:element name="element3" type="element2" minOccurs="0"
                  maxOccurs="unbounded" /&gt;
          &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
      &lt;xs:complexType name="Container2"&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="element3" type="Type1" minOccurs="0" /&gt;
              &lt;xs:element name="element2" type="Type2" minOccurs="0" /&gt;
          &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
      &lt;xs:complexType name="Container3"&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="element4" type="Type1" minOccurs="0" /&gt;
          &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>So my question is how can I add a new complex type named "Container 3"...with a sequence...that contains "element 5" using the same DOM apprach so it looks like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xs:schema xmlns="com/company/common" xmlns:xs="http://www.w3.org/2001/XMLSchema"
      targetNamespace="com/company/common/" elementFormDefault="qualified"&gt;
      &lt;xs:include schemaLocation="DerivedAttributes.xsd" /&gt;
      &lt;xs:element name="MyXSD" type="MyXSD" /&gt;
      &lt;xs:complexType name="Container1"&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="element1" type="element1" minOccurs="0"
                  maxOccurs="unbounded" /&gt;
              &lt;xs:element name="element2" type="element2" minOccurs="0"
                  maxOccurs="unbounded" /&gt;
          &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
      &lt;xs:complexType name="Container2"&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="element3" type="Type1" minOccurs="0" /&gt;
              &lt;xs:element name="element4" type="Type2" minOccurs="0" /&gt;
          &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
      &lt;xs:complexType name="Container3"&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="element5" type="Type1" minOccurs="0" /&gt;
          &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>Right now im using a DOM parser that adds a new complex type...But im not sure how to create a complex type that also has sequence with an element. This is what I have so far...</p>
  
  <pre><code>Document doc = DocumentBuilderFactory.newInstance()
              .newDocumentBuilder().parse(
                      new InputSource("test.xsd"));
  
      // use xpath to find node to add to
      XPath xPath = XPathFactory.newInstance().newXPath();
      NodeList nodes = (NodeList) xPath.evaluate("/schema", doc
              .getDocumentElement(), XPathConstants.NODESET);
  
      // create element to add
      org.w3c.dom.Element newElement = doc.createElement("xs:complexType");
      newElement.setAttribute("name", "Container3");
  
      nodes.item(0).appendChild(newElement);
  
      // output
      TransformerFactory.newInstance().newTransformer().transform(
              new DOMSource(doc.getDocumentElement()),
              new StreamResult(System.out));
  </code></pre>
  
  <p>Thanks!</p>