title: XSLT transformation - adding missing siblings if not present
body: <p>I am doing a transformation in XSLT. I have to add siblings if missing.</p>
  
  <p>I am transforming XML given by the following XSD:</p>
  
  <pre><code>&lt;xs:complexType name="parent"&gt;
      &lt;xs:sequence&gt;
          &lt;xs:element name="a" type="xs:string" minOccurs="0"/&gt;
          ... more optional elements...
          &lt;xs:element name="x" type="xs:string" minOccurs="0"/&gt;
          &lt;xs:element name="y" type="xs:string" minOccurs="0"/&gt;
          &lt;xs:element name="z" type="xs:string"/&gt;
      &lt;/xs:sequence&gt;
  &lt;/xs:comlexType&gt;
  </code></pre>
  
  <p>to an XML given by the following XSD</p>
  
  <pre><code>&lt;xs:complexType name="parent"&gt;
      &lt;xs:sequence&gt;
          &lt;xs:element name="a" type="xs:string" minOccurs="0"/&gt;
          ... more optional elements...
          &lt;xs:element name="x" type="xs:string"/&gt;
          &lt;xs:element name="y" type="xs:string"/&gt;
          &lt;xs:element name="z" type="xs:string"/&gt;
      &lt;/xs:sequence&gt;
  &lt;/xs:comlexType&gt;
  </code></pre>
  
  <p>which means that elements x and y are going from optional to required and should be added if missing. This means that my XSLT transformation should handle the following 4 cases:</p>
  
  <p><strong>Case 1</strong> (nothing should be added):</p>
  
  <pre><code>&lt;parent&gt;
      &lt;a&gt;foo&lt;/a&gt;
      ...
      &lt;x&gt;foo&lt;/x&gt;
      &lt;y&gt;foo&lt;/y&gt;
      &lt;z&gt;foo&lt;/z&gt;
   &lt;/parent&gt;
  </code></pre>
  
  <p><strong>Case 2</strong> (x should be added)</p>
  
  <pre><code>&lt;parent&gt;
      &lt;a&gt;foo&lt;/a&gt;
      ...
      &lt;y&gt;foo&lt;/y&gt;
      &lt;z&gt;foo&lt;/z&gt;
   &lt;/parent&gt;
  </code></pre>
  
  <p><strong>Case 3</strong> (y should be added)</p>
  
  <pre><code>&lt;parent&gt;
      &lt;a&gt;foo&lt;/a&gt;
      ...
      &lt;x&gt;foo&lt;/x&gt;
      &lt;z&gt;foo&lt;/z&gt;
   &lt;/parent&gt;
  </code></pre>
  
  <p><strong>Case 4</strong> (x and y should be added)</p>
  
  <pre><code>&lt;parent&gt;
      &lt;a&gt;foo&lt;/a&gt;
      ...
      &lt;z&gt;foo&lt;/z&gt;
   &lt;/parent&gt;
  </code></pre>
  
  <p>I have tried the following XSLT that does not work as intended:</p>
  
  <pre><code>&lt;xsl:template match="parent[not(x)]/y"&gt;
      &lt;xsl:element name="x"&gt;foo&lt;/xsl:element&gt;
      &lt;xsl:call-template name="identity"/&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="parent[not(x)]/z"&gt;
      &lt;xsl:element name="x"&gt;foo&lt;/xsl:element&gt;
      &lt;xsl:call-template name="identity"/&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="parent[not(y)]/z"&gt;
      &lt;xsl:element name="y"&gt;foo&lt;/xsl:element&gt;
      &lt;xsl:call-template name="identity"/&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>The results for the above cases are as follows:</p>
  
  <p><strong>Case 1</strong> OK</p>
  
  <pre><code>&lt;parent&gt;
      &lt;a&gt;foo&lt;/a&gt;
      ...
      &lt;x&gt;foo&lt;/x&gt;
      &lt;y&gt;foo&lt;/y&gt;
      &lt;z&gt;foo&lt;/z&gt;
   &lt;/parent&gt;
  </code></pre>
  
  <p><strong>Case 2</strong> OK</p>
  
  <pre><code>&lt;parent&gt;
      &lt;a&gt;foo&lt;/a&gt;
      ...
      &lt;x&gt;foo&lt;/x&gt;
      &lt;y&gt;foo&lt;/y&gt;
      &lt;z&gt;foo&lt;/z&gt;
   &lt;/parent&gt;
  </code></pre>
  
  <p><strong>Case 3</strong> Not OK</p>
  
  <pre><code>&lt;parent&gt;
      &lt;a&gt;foo&lt;/a&gt;
      ...
      &lt;x&gt;foo&lt;/x&gt;
      &lt;y&gt;foo&lt;/y&gt;
      &lt;x&gt;foo&lt;/x&gt;
      &lt;z&gt;foo&lt;/z&gt;
   &lt;/parent&gt;
  </code></pre>
  
  <p><strong>Case 4</strong> Not OK</p>
  
  <pre><code>&lt;parent&gt;
      &lt;a&gt;foo&lt;/a&gt;
      ...
      &lt;y&gt;foo&lt;/y&gt;
      &lt;z&gt;foo&lt;/z&gt;
   &lt;/parent&gt;
  </code></pre>