title: XSLT 1.0 transformation under certain conditions
body: <p>I have an xml, which is the merged output of several subsystem queries. Now I must pick a lot of informations. For example:</p>
  
  <p>/Merge/response/BenutzerDaten/Deaktiviert</p>
  
  <p>If /Merge/response/results/result/dn ends with ",o=test,c=de" and there is only one result ending with the pattern:</p>
  
  <p>/Merge/response/results/result/attributes/identnummer</p>
  
  <p>If /Merge/response/results/result/dn ends with ",DC=TEST,DC=DE":</p>
  
  <p>/Merge/response/results/result/attributes/postalCode</p>
  
  <p>Whats the best way to do this?</p>
  
  <p>Here is a reduced sample of my soure xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Merge&gt;
      &lt;response&gt;
          &lt;results&gt;
              &lt;result&gt;
                  &lt;dn&gt;cn=user1,ou=members,ou=group1,o=test,c=de&lt;/dn&gt;
                  &lt;attributes&gt;
                      &lt;identnummer&gt;re3232&lt;/identnummer&gt;
                      &lt;uid&gt;user1&lt;/uid&gt;
                      [...]
                      &lt;adminuid&gt;admin3&lt;/adminuid&gt;
                  &lt;/attributes&gt;
              &lt;/result&gt;
          &lt;/results&gt;
      &lt;/response&gt;
      &lt;response&gt;
          &lt;results&gt;
              &lt;result&gt;
                  &lt;dn&gt;CN=user1,OU=Users,DC=TEST,DC=DE&lt;/dn&gt;
                  &lt;attributes&gt;
                      &lt;postalCode&gt;12345&lt;/postalCode&gt;
                      &lt;company&gt;My Company&lt;/company&gt;
                      [...]
                      &lt;streetAddress&gt;XSLT Road 1&lt;/streetAddress&gt;
                  &lt;/attributes&gt;
              &lt;/result&gt;
          &lt;/results&gt;
      &lt;/response&gt;
      &lt;response&gt;
          &lt;secDN&gt;principalName=user1,cn=Users,secDomain=Default&lt;/secDN&gt;
          &lt;Benutzerdaten&gt;
              &lt;Deaktiviert&gt;false&lt;/Deaktiviert&gt;
              &lt;Gesperrt&gt;false&lt;/Gesperrt&gt;
              [...]
              &lt;PasswortGueltig&gt;true&lt;/PasswortGueltig&gt;
          &lt;/Benutzerdaten&gt;
      &lt;/response&gt;
      &lt;Nutzdaten&gt;
          &lt;Identifikation&gt;
              &lt;AdminID&gt;admin3&lt;/AdminID&gt;
              &lt;BenutzerID&gt;user1&lt;/BenutzerID&gt;
          &lt;/Identifikation&gt;
          &lt;Benutzerdaten&gt;
              &lt;Datentyp&gt;Account-Daten&lt;/Datentyp&gt;
              &lt;Datentyp&gt;Anwendungs-Daten&lt;/Datentyp&gt;
              &lt;Datentyp&gt;Benutzer-Daten&lt;/Datentyp&gt;
          &lt;/Benutzerdaten&gt;
      &lt;/Nutzdaten&gt;
  &lt;/Merge&gt;
  </code></pre>
  
  <p>The expected result should look like:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;UserData&gt;
      &lt;user&gt;user1&lt;/user&gt;
      &lt;source1&gt;
          &lt;identnummer&gt;re3232&lt;/identnummer&gt;
      &lt;/source1&gt;
      &lt;source2&gt;
          &lt;postalCode&gt;12345&lt;/postalCode&gt;
      &lt;/source2&gt;
      &lt;source3&gt;
          &lt;Deaktiviert&gt;false&lt;/Deaktiviert&gt;
      &lt;/source3&gt;
  &lt;/UserData&gt;
  </code></pre>
  
  <p>And here is my unsuccessful try:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    &lt;xsl:template match="/"&gt;
      &lt;xsl:choose&gt;
        &lt;xsl:when test="/Merge/response/Benutzerdaten and count(/Merge/response/results/result[dn = '*,o=test,c=de') = 1"  and count(/Merge/response/results/result[dn = '*,DC=TEST,DC=DE') = 1" &gt;
          &lt;UserData&gt;
            &lt;user&gt;
              &lt;xsl:value-of select="/Merge/Nutzdaten/Identifikation/BenutzerID"/&gt;
            &lt;/user&gt;
            &lt;xsl:if test="/Merge/response/Benutzerdaten/*"&gt;
              &lt;Account&gt;
                &lt;xsl:copy-of select="/Merge/response/Benutzerdaten/*"/&gt;
              &lt;/Account&gt;
            &lt;/xsl:if&gt;
            &lt;xsl:if test="substring(/Merge/response/results/result/dn, string-length(/Merge/response/results/result/dn) -11) = ',o=test,c=de'"&gt;
              &lt;source1&gt;
                  &lt;postalCode&gt;
                      &lt;xsl:value-of select="/Merge/response/results/result/attributes/portalCode"&gt;&lt;/xsl:value-of&gt;
                  &lt;/postalCode&gt;
              &lt;/source1&gt;
            &lt;/xsl:if&gt;
            &lt;xsl:if test="substring(/Merge/response/results/result/dn, string-length(/Merge/response/results/result/dn) -12) = ',DC=TEST,DC=DE'"&gt;
              &lt;source2&gt;
                  &lt;postalCode&gt;
                      &lt;xsl:value-of select="/Merge/response/results/result/attributes/portalCode"&gt;&lt;/xsl:value-of&gt;
                  &lt;/postalCode&gt;
              &lt;/source2&gt;
            &lt;/xsl:if&gt;
            &lt;source3&gt;
              &lt;Deaktiviert&gt;
                  &lt;xsl:value-of select="/Merge/response/BenutzerDaten/Deaktiviert"&gt;&lt;/xsl:value-of&gt;
              &lt;/Deaktiviert&gt;
            &lt;/source3&gt;
          &lt;/UserData&gt;
        &lt;/xsl:when&gt;
        &lt;xsl:otherwise&gt;
          &lt;response&gt;
            &lt;failure&gt;
              &lt;xsl:choose&gt;
                &lt;xsl:when test="count(/Merge/response/results/result[dn = '*,o=test,c=de') &gt; 1"&gt;
                  &lt;message&gt;more than one result from source1&lt;/message&gt;
                &lt;/xsl:when&gt;
                &lt;xsl:otherwise&gt;
                  &lt;message&gt;The requested object does not exist.&lt;/message&gt;
                &lt;/xsl:otherwise&gt;
              &lt;/xsl:choose&gt;
            &lt;/failure&gt;
          &lt;/response&gt;
        &lt;/xsl:otherwise&gt;
      &lt;/xsl:choose&gt;
    &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>