title: Remove unused elements from XML schema using XSLT
body: <p>I'm looking for a way (if it's even possible) of using an XSL transform of an XSD document to remove unused elements. This comes up a lot in my job where a company will define an XSD with absolutely everything in it, but then they will want to create a cut-down version for a single root element within it.</p>
  
  <p>To explain further, I might have an XSD like the following:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"&gt;
      &lt;xs:element name="RootElement"&gt;
          &lt;xs:complexType&gt;
              &lt;xs:sequence&gt;
                  &lt;xs:element ref="ChildElement"/&gt;
              &lt;/xs:sequence&gt;
          &lt;/xs:complexType&gt;
      &lt;/xs:element&gt;
      &lt;xs:element name="ChildElement"/&gt;
      &lt;xs:element name="UnusedElement"/&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>What I would like to be able to do is to set up an XSL where I provide the starting element (in this case <code>RootElement</code>) and it will copy over all dependent elements but omit the unused ones. In the above example, if I passed in RootElement I'd expect to see <code>RootElement</code> and <code>ChildElement</code> included but <code>UnusedElement</code> omitted.</p>
  
  <p>(When I say "provide the starting element", I'm quite happy to crack open the stylesheet and type <code>xsl:template match="RootElement"</code> where required.)</p>
  
  <p>This would obviously have to be recursive, so would navigate the entire structure defined below the starting element, and any element in that schema that was not used would be discarded.</p>
  
  <p>(Of course, it would be even better if it could do the same in any imported schemas!)</p>
  
  <p>I've searched Google extensively and can't find anything on this - I'm not sure if that means it's not possible or not.</p>
  
  <p>Thanks!</p>
  
  <p><strong>Edit</strong>: Actually I probably should clarify and say that I would like to remove unused elements AND types, so it would follow both <code>ref="childElement"</code> and <code>type="someType"</code> links.</p>