title: XSLT: Add element definition into XSD after last simpleType and before first complexType
body: <p>I am trying to create a XSLT 2.0 stylesheet that transforms my XSD to the same XSD, but extended with some extra elements. I am trying to add an element after the last simpleType declared and before the first complexType which contains the attribute <code>myAtt</code> (if there is a complex, otherwise after the last simpleType)</p>
  
  <p><strong>MyXSD:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.myspace.com/myxsd" targetNamespace="http://www.myspace.com/myxsd" elementFormDefault="qualified"&gt;
      &lt;xs:include schemaLocation="myxsd-simpletypes.xsd"/&gt;
      &lt;xs:element name="myComplexTypeA" type="myComplexTypeAType"/&gt;
      &lt;xs:complexType name="myXmlType"&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="mySimpleTypeA" type="mySimpleTypeAType" minOccurs="0"/&gt;
              &lt;xs:element name="mySimpleTypeB" type="mySimpleTypeBType" minOccurs="0"/&gt;
              &lt;xs:element name="mySimpleTypeC" type="mySimpleTypeCType" minOccurs="0"/&gt;
              &lt;xs:element name="myComplexTypeB" type="myComplexTypeBType"&gt;
                  &lt;xs:annotation&gt;
                      &lt;xs:documentation&gt;My first complex type&lt;/xs:documentation&gt;
                  &lt;/xs:annotation&gt;
              &lt;/xs:element&gt;
          &lt;/xs:sequence&gt;
          &lt;xs:attribute name="myAtt" type="stringType" use="required" fixed="myValue"/&gt;
      &lt;/xs:complexType&gt;
      &lt;xs:complexType name="myComplexTypeBType"&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="mySimpleTypeA" type="mySimpleTypeAType" minOccurs="0"/&gt;
              &lt;xs:element name="mySimpleTypeB" type="mySimpleTypeBType" minOccurs="0"/&gt;
              &lt;xs:element name="mySimpleTypeC" type="mySimpleTypeCType" minOccurs="0"/&gt;
              &lt;xs:element name="myComplexTypeC" type="myComplexTypeCType" minOccurs="0" maxOccurs="100"/&gt;
              &lt;xs:element name="myComplexTypeD" type="myComplexTypeDType" minOccurs="0" maxOccurs="unbounded"/&gt;
          &lt;/xs:sequence&gt;
          &lt;xs:attribute name="myAtt" type="stringType" use="required" fixed="myValue"/&gt;
      &lt;/xs:complexType&gt;
      &lt;xs:complexType name="myComplexTypeCType"&gt;
          &lt;xs:annotation&gt;
              &lt;xs:documentation&gt;Some documentation&lt;/xs:documentation&gt;
          &lt;/xs:annotation&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="mySimpleTypeA" type="mySimpleTypeAType" minOccurs="0"/&gt;
              &lt;xs:element name="mySimpleTypeB" type="mySimpleTypeBType" minOccurs="0"/&gt;
              &lt;xs:element name="mySimpleTypeC" type="mySimpleTypeCType" minOccurs="0"/&gt;
          &lt;/xs:sequence&gt;
          &lt;xs:attribute name="myAtt" type="stringType" use="required" fixed="myValue"/&gt;
      &lt;/xs:complexType&gt;
      &lt;xs:complexType name="myComplexTypeDType"&gt;
          &lt;xs:annotation&gt;
              &lt;xs:documentation&gt;Some documentation&lt;/xs:documentation&gt;
          &lt;/xs:annotation&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="mySimpleTypeA" type="mySimpleTypeAType" minOccurs="0"/&gt;
              &lt;xs:element name="mySimpleTypeB" type="mySimpleTypeBType" minOccurs="0"/&gt;
              &lt;xs:element name="mySimpleTypeC" type="mySimpleTypeCType" minOccurs="0"/&gt;
          &lt;/xs:sequence&gt;
          &lt;xs:attribute name="myAtt" type="stringType" use="required" fixed="myValue"/&gt;
      &lt;/xs:complexType&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p><strong>MyXSLT</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;xsl:stylesheet version="2.0" exclude-result-prefixes="xs xdt err fn" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes" xmlns:err="http://www.w3.org/2005/xqt-errors"&gt;
      &lt;xsl:output method="xml" indent="yes"/&gt;
  
      &lt;!-- copy all nodes and attributes--&gt;
      &lt;xsl:template match="@*|node()"&gt;
          &lt;xsl:copy&gt;
              &lt;xsl:apply-templates select="@*|node()"/&gt;
          &lt;/xsl:copy&gt;
      &lt;/xsl:template&gt;
  
      &lt;!-- For each complexType do this --&gt;
      &lt;xsl:template match="/xs:schema/xs:complexType/xs:sequence"&gt;
          &lt;xsl:element name="xs:sequence"&gt;
              &lt;xsl:for-each select="xs:element"&gt;
                  &lt;xsl:copy&gt;
                      &lt;xsl:apply-templates select="@*|node()"/&gt;
                  &lt;/xsl:copy&gt;
  
                  &lt;!-- Only add element after last simpleType and before first complexType (if complexType exists) --&gt;
                  &lt;xsl:if test="?????"&gt;
                      &lt;xsl:element name="xs:element"&gt;
                          &lt;xsl:attribute name="maxOccurs"&gt;unbounded&lt;/xsl:attribute&gt;
                          &lt;xsl:attribute name="minOccurs"&gt;0&lt;/xsl:attribute&gt;
                          &lt;xsl:attribute name="name"&gt;myAddition&lt;/xsl:attribute&gt;
                          &lt;xsl:attribute name="type"&gt;myAdditionType&lt;/xsl:attribute&gt;
                      &lt;/xsl:element&gt;
                  &lt;/xsl:if&gt;
              &lt;/xsl:for-each&gt;
          &lt;/xsl:element&gt;
      &lt;/xsl:template&gt;
  
      &lt;!-- After last complexType in file add definition of myAddition --&gt;
      &lt;xsl:template match="/xs:schema/xs:complexType[last()]"&gt;
          &lt;xsl:copy&gt;
              &lt;xsl:apply-templates select="@*|node()"/&gt;
          &lt;/xsl:copy&gt;
          &lt;xsl:element name="xs:complexType"&gt;
              &lt;xsl:attribute name="name"&gt;myAdditionType&lt;/xsl:attribute&gt;
              &lt;xsl:element name="xs:sequence"&gt;
                  &lt;xsl:element name="xs:element"&gt;
                      &lt;xsl:attribute name="minOccurs"&gt;0&lt;/xsl:attribute&gt;
                      &lt;xsl:attribute name="name"&gt;myAdditionA&lt;/xsl:attribute&gt;
                      &lt;xsl:attribute name="type"&gt;myAdditionAType&lt;/xsl:attribute&gt;
                  &lt;/xsl:element&gt;
                  &lt;xsl:element name="xs:element"&gt;
                      &lt;xsl:attribute name="minOccurs"&gt;0&lt;/xsl:attribute&gt;
                      &lt;xsl:attribute name="name"&gt;myAdditionBValue&lt;/xsl:attribute&gt;
                      &lt;xsl:attribute name="type"&gt;myAdditionBType&lt;/xsl:attribute&gt;
                  &lt;/xsl:element&gt;
              &lt;/xsl:element&gt;
              &lt;xsl:element name="xs:attribute"&gt;
                  &lt;xsl:attribute name="fixed"&gt;myValue&lt;/xsl:attribute&gt;
                  &lt;xsl:attribute name="name"&gt;myAtt&lt;/xsl:attribute&gt;
                  &lt;xsl:attribute name="type"&gt;stringType&lt;/xsl:attribute&gt;
                  &lt;xsl:attribute name="use"&gt;required&lt;/xsl:attribute&gt;
              &lt;/xsl:element&gt;
          &lt;/xsl:element&gt;
          &lt;xsl:element name="xs:simpleType"&gt;
              &lt;xsl:attribute name="name"&gt;myAdditionAType&lt;/xsl:attribute&gt;
              &lt;xsl:element name="xs:restriction"&gt;
                  &lt;xsl:attribute name="base"&gt;AType&lt;/xsl:attribute&gt;
                  &lt;xsl:element name="xs:maxLength"&gt;
                      &lt;xsl:attribute name="value"&gt;10&lt;/xsl:attribute&gt;
                  &lt;/xsl:element&gt;
              &lt;/xsl:element&gt;
          &lt;/xsl:element&gt;
          &lt;xsl:element name="xs:simpleType"&gt;
              &lt;xsl:attribute name="name"&gt;myAdditionBType&lt;/xsl:attribute&gt;
              &lt;xsl:element name="xs:restriction"&gt;
                  &lt;xsl:attribute name="base"&gt;AType&lt;/xsl:attribute&gt;
              &lt;/xsl:element&gt;
          &lt;/xsl:element&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p><strong>Result I get now:</strong>
  None, because it is not finished yet!</p>
  
  <p><strong>Wanted result:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.myspace.com/myxsd" targetNamespace="http://www.myspace.com/myxsd" elementFormDefault="qualified"&gt;
      &lt;xs:include schemaLocation="myxsd-simpletypes.xsd"/&gt;
      &lt;xs:element name="myComplexTypeA" type="myComplexTypeAType"/&gt;
      &lt;xs:complexType name="myXmlType"&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="mySimpleTypeA" type="mySimpleTypeAType" minOccurs="0"/&gt;
              &lt;xs:element name="mySimpleTypeB" type="mySimpleTypeBType" minOccurs="0"/&gt;
              &lt;xs:element name="mySimpleTypeC" type="mySimpleTypeCType" minOccurs="0"/&gt;
              &lt;xs:element name="myAddition" type="myAdditionType" minOccurs="0" maxOccurs="unbounded"/&gt;
              &lt;xs:element name="myComplexTypeB" type="myComplexTypeBType"&gt;
                  &lt;xs:annotation&gt;
                      &lt;xs:documentation&gt;My first complex type&lt;/xs:documentation&gt;
                  &lt;/xs:annotation&gt;
              &lt;/xs:element&gt;
          &lt;/xs:sequence&gt;
          &lt;xs:attribute name="myAtt" type="stringType" use="required" fixed="myValue"/&gt;
      &lt;/xs:complexType&gt;
      &lt;xs:complexType name="myComplexTypeBType"&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="mySimpleTypeA" type="mySimpleTypeAType" minOccurs="0"/&gt;
              &lt;xs:element name="mySimpleTypeB" type="mySimpleTypeBType" minOccurs="0"/&gt;
              &lt;xs:element name="mySimpleTypeC" type="mySimpleTypeCType" minOccurs="0"/&gt;
              &lt;xs:element name="myAddition" type="myAdditionType" minOccurs="0" maxOccurs="unbounded"/&gt;
              &lt;xs:element name="myComplexTypeC" type="myComplexTypeCType" minOccurs="0" maxOccurs="100"/&gt;
              &lt;xs:element name="myComplexTypeD" type="myComplexTypeDType" minOccurs="0" maxOccurs="unbounded"/&gt;
          &lt;/xs:sequence&gt;
          &lt;xs:attribute name="myAtt" type="stringType" use="required" fixed="myValue"/&gt;
      &lt;/xs:complexType&gt;
      &lt;xs:complexType name="myComplexTypeCType"&gt;
          &lt;xs:annotation&gt;
              &lt;xs:documentation&gt;Some documentation&lt;/xs:documentation&gt;
          &lt;/xs:annotation&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="mySimpleTypeA" type="mySimpleTypeAType" minOccurs="0"/&gt;
              &lt;xs:element name="mySimpleTypeB" type="mySimpleTypeBType" minOccurs="0"/&gt;
              &lt;xs:element name="mySimpleTypeC" type="mySimpleTypeCType" minOccurs="0"/&gt;
              &lt;xs:element name="myAddition" type="myAdditionType" minOccurs="0" maxOccurs="unbounded"/&gt;
          &lt;/xs:sequence&gt;
          &lt;xs:attribute name="myAtt" type="stringType" use="required" fixed="myValue"/&gt;
      &lt;/xs:complexType&gt;
      &lt;xs:complexType name="myComplexTypeDType"&gt;
          &lt;xs:annotation&gt;
              &lt;xs:documentation&gt;Some documentation&lt;/xs:documentation&gt;
          &lt;/xs:annotation&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="mySimpleTypeA" type="mySimpleTypeAType" minOccurs="0"/&gt;
              &lt;xs:element name="mySimpleTypeB" type="mySimpleTypeBType" minOccurs="0"/&gt;
              &lt;xs:element name="mySimpleTypeC" type="mySimpleTypeCType" minOccurs="0"/&gt;
              &lt;xs:element name="myAddition" type="myAdditionType" minOccurs="0" maxOccurs="unbounded"/&gt;
          &lt;/xs:sequence&gt;
          &lt;xs:attribute name="myAtt" type="stringType" use="required" fixed="myValue"/&gt;
      &lt;/xs:complexType&gt;
      &lt;xs:complexType name="myAdditionType"&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="myAdditionA" type="myAdditionAType" minOccurs="0"/&gt;
              &lt;xs:element name="myAdditionB" type="myAdditionBType" minOccurs="0"/&gt;
          &lt;/xs:sequence&gt;
          &lt;xs:attribute name="myAtt" type="stringType" use="required" fixed="myValue"/&gt;
      &lt;/xs:complexType&gt;
      &lt;xs:simpleType name="myAdditionAType"&gt;
          &lt;xs:restriction base="AType"&gt;
              &lt;xs:maxLength value="10"/&gt;
          &lt;/xs:restriction&gt;
      &lt;/xs:simpleType&gt;
      &lt;xs:simpleType name="myAdditionBType"&gt;
          &lt;xs:restriction base="AType"/&gt;
      &lt;/xs:simpleType&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>Can anyone help me into the right direction?</p>