title: Plist to JUnit XML via XSL
body: <p>I am trying to convert Apples plist file into a JUnit XML format so Hudson/Jenkins can read the results. </p>
  
  <p>I have a plist file output similar to this:</p>
  
  <pre><code>&lt;plist&gt;&lt;dict&gt;&lt;array&gt;
          &lt;dict&gt;
              &lt;key&gt;LogType&lt;/key&gt;
              &lt;string&gt;Pass&lt;/string&gt;
              &lt;key&gt;Message&lt;/key&gt;
              &lt;string&gt;Test 1&lt;/string&gt;
              &lt;key&gt;Timestamp&lt;/key&gt;
              &lt;date&gt;2012-10-26T09:42:41Z&lt;/date&gt;
              &lt;key&gt;Type&lt;/key&gt;
              &lt;integer&gt;4&lt;/integer&gt;
           &lt;/dict&gt;
           &lt;dict&gt;.....&lt;/dict&gt;
           .
           .
           .
           &lt;dict&gt;
              &lt;key&gt;LogType&lt;/key&gt;
              &lt;string&gt;Pass&lt;/string&gt;
              &lt;key&gt;Message&lt;/key&gt;
              &lt;string&gt;Test 1&lt;/string&gt;
              &lt;key&gt;Timestamp&lt;/key&gt;
              &lt;date&gt;2012-10-26T09:43:27Z&lt;/date&gt;
              &lt;key&gt;Type&lt;/key&gt;
              &lt;integer&gt;5&lt;/integer&gt;
           &lt;/dict&gt;
           &lt;dict&gt;
              &lt;key&gt;LogType&lt;/key&gt;
              &lt;string&gt;Fail&lt;/string&gt;
              &lt;key&gt;Message&lt;/key&gt;
              &lt;string&gt;Inserting content to a group&lt;/string&gt;
              &lt;key&gt;Timestamp&lt;/key&gt;
              &lt;date&gt;2012-10-26T09:49:13Z&lt;/date&gt;
              &lt;key&gt;Type&lt;/key&gt;
              &lt;integer&gt;4&lt;/integer&gt;
          &lt;/dict&gt;
          &lt;dict&gt;.....&lt;/dict&gt;
          .
          .
          .
          &lt;dict&gt;
              &lt;key&gt;LogType&lt;/key&gt;
              &lt;string&gt;Error&lt;/string&gt;
              &lt;key&gt;Message&lt;/key&gt;
              &lt;string&gt;VerboseError: target.frontMostApp().mainWindow().buttons()[3] could not be tapped&lt;/string&gt;
              &lt;key&gt;Screenshot&lt;/key&gt;
              &lt;string&gt;&lt;/string&gt;
              &lt;key&gt;Timestamp&lt;/key&gt;
              &lt;date&gt;2012-10-26T09:50:12Z&lt;/date&gt;
              &lt;key&gt;Type&lt;/key&gt;
              &lt;integer&gt;3&lt;/integer&gt;
          &lt;/dict&gt;
          &lt;dict&gt;
              &lt;key&gt;LogType&lt;/key&gt;
              &lt;string&gt;Fail&lt;/string&gt;
              &lt;key&gt;Message&lt;/key&gt;
              &lt;string&gt;Inserting content to a group&lt;/string&gt;
              &lt;key&gt;Screenshot&lt;/key&gt;
              &lt;string&gt;&lt;/string&gt;
              &lt;key&gt;Timestamp&lt;/key&gt;
              &lt;date&gt;2012-10-26T09:50:13Z&lt;/date&gt;
              &lt;key&gt;Type&lt;/key&gt;
              &lt;integer&gt;7&lt;/integer&gt;
          &lt;/dict&gt;
  &lt;/array&gt;&lt;/dict&gt;
  &lt;/plist&gt;
  </code></pre>
  
  <p>which I need to convert to JUnit XML. Here is the output iI expect for the plist fiel above:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;testsuites&gt;
      &lt;testsuite&gt;
          &lt;testcase classname="Test 1" name="Test 1"/&gt;
          &lt;testcase classname="Inserting content to a group" name="Inserting content to a group"&gt;
              &lt;failure&gt;Inserting content to a group - VerboseError: target.frontMostApp().mainWindow().buttons()[3] could not be tapped&lt; &lt;/failure&gt;
          &lt;/testcase&gt;
      &lt;/testsuite&gt;
  &lt;/testsuites&gt;
  </code></pre>
  
  <p>Currently I have this XSL:</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;testsuites&gt;
              &lt;testsuite&gt;
                  &lt;xsl:for-each select="plist/dict/array/dict"&gt;
                      &lt;xsl:if test="integer = 4"&gt;
                          &lt;testcase&gt;
                              &lt;xsl:attribute name="classname"&gt;&lt;xsl:value-of select="string[2]" /&gt;&lt;/xsl:attribute&gt;
                              &lt;xsl:attribute name="name"&gt;&lt;xsl:value-of select="string[2]"/&gt;&lt;/xsl:attribute&gt;
                              &lt;xsl:if test="string[1] = 'Fail'"&gt;
                                  &lt;failure&gt;
                                      &lt;xsl:attribute name="type"&gt;&lt;xsl:value-of select="integer" /&gt;&lt;/xsl:attribute&gt;&lt;xsl:value-of select="string[2]" /&gt;                                           
                                  &lt;/failure&gt;
                              &lt;/xsl:if&gt;
                          &lt;/testcase&gt;
                      &lt;/xsl:if&gt;
                  &lt;/xsl:for-each&gt;
              &lt;/testsuite&gt;
          &lt;/testsuites&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>How can I edit the XSL above to find any error messages in between the two dict/string[2] = 'Test A' and insert the message into the value of <code>&lt;failure&gt;</code>? I am not sure how to do this because the Error message is contained in another <code>&lt;dict&gt;</code> node.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Ok I have broken this down into pseudo ish code:</p>
  
  <ol>
  <li><p>Count all the nodes with integer = 4.</p></li>
  <li><p>Find out the position of each of the nodes with integer = 4 and store in a variable</p></li>
  <li><p>Go through each node with integer = 4 and find any string[1] = 'Fail' before next node with integer = 4. </p>
  
  <ol>
  <li><p>If  any string[1] = 'Fail' then find and string[1] = 'Error' before next node with integer = 4  and after previous node with integer = 4.</p>
  
  <ol>
  <li>If any string[1] = 'Error', output <code>failure</code> with string[2] from current node and string[2] from previous node with integer = 4.</li>
  </ol></li>
  <li>Else output <code>failure</code> with string[2] from previous node with integer = 4.</li>
  </ol></li>
  </ol>
  
  <p>node referring to plist/dict/array/dict</p>
  
  <p>Is this possible with XSL?</p>