title: Dynamically create and update an .xsd schema in PHP?
body: <p>I have a .xsd schema that defines both simple and complex types - a sample is below. I need to be able to load this .xsd file, add more elements to it and return it as a string (which will eventually be sent via an api to a 3rd party service). The website is built on Drupal, so the code needs to be in PHP.</p>
  
  <p>Below is a sample of the schema (.xsd)</p>
  
  <p>Are there any tools or libraries that will make easier? I see lots of xml utilities in php like SimpleXML, but in the examples I looked at they already have the .xsd file and are parsing and creating objects and setting values in an xml document based on a given schema. I want to programatically change and update a schema .xsd file via php?Any tips? or libraries or appraoches would be greatly appreciated. I'm about to start wrtiing the code in a less than ideal way (but it will work), which just rebuilds the entire .xsd each time and has multiple loops using strings to add new elements, but that's not the best solution.
  I'm hoping there is a tool that may work something like this:</p>
  
  <pre><code>$newschemaObject = MakeDynmaicObject(oldschema.xsd);
  $newschema-&gt;add(simple, text, "New Text Field", required);
  $stringtosend = $newschema-&gt;buildXSD();
  </code></pre>
  
  <p>Sample of schema.xsd</p>
  
  <pre><code>&lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
   &lt;xsd:element name="metadata"&gt;
      &lt;xsd:complexType&gt;
    &lt;xsd:sequence&gt;
      &lt;xsd:element name="DrupalEntityId" minOccurs="0" maxOccurs="1" type="textType"&gt;
        &lt;xsd:annotation&gt;
          &lt;xsd:documentation/&gt;
          &lt;xsd:appinfo&gt;
            &lt;label&gt;Drupal Entity ID&lt;/label&gt;
            &lt;key&gt;Drupal Entity ID&lt;/key&gt;
            &lt;searchable&gt;true&lt;/searchable&gt;
            &lt;timeControl&gt;false&lt;/timeControl&gt;
            &lt;description/&gt;
          &lt;/xsd:appinfo&gt;
        &lt;/xsd:annotation&gt;
      &lt;/xsd:element&gt;
      &lt;xsd:element id="md_631560E7-14AD-5327-732B-8268813A0553" name="SampleMultiValueTextField" minOccurs="0" maxOccurs="unbounded" type="textType"&gt;
        &lt;xsd:annotation&gt;
          &lt;xsd:documentation/&gt;
          &lt;xsd:appinfo&gt;
            &lt;label&gt;Sample Multi Value Text Field&lt;/label&gt;
            &lt;key&gt;Sample Multi Value Text Field&lt;/key&gt;
            &lt;searchable&gt;true&lt;/searchable&gt;
            &lt;timeControl&gt;false&lt;/timeControl&gt;
            &lt;description&gt;This is a sample multivalue text field&lt;/description&gt;
          &lt;/xsd:appinfo&gt;
        &lt;/xsd:annotation&gt;
      &lt;/xsd:element&gt;
   ...
  &lt;/xsd:schema&gt;
  </code></pre>
  
  <p>The current version of my code uses a lot of strings like this:</p>
  
  <pre><code>$xsd .= '&lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;';
  $xsd .= '  &lt;xsd:element name="metadata"&gt;';
  </code></pre>
  
  <p>to generate the above schema, but it's not flexible or very programmatic. any advice on being able to update the.xsd and set new elements in a object oriented way would be amazing!!!!</p>