title: xslt- how to group repeating element's and combine child element and attributes
body: <p>How would I transform the following in order to combine all the conditions and measurements for each Dish? I'm struggling with combining repeating child elements.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Test&gt;
    &lt;Experiment id='1'&gt;
      &lt;Dish1&gt;
        &lt;Conditions pressure='x' temp='y'/&gt;
        &lt;Measurement timeStamp='8am' reading='y'/&gt;
      &lt;/Dish1&gt;
      &lt;Dish2&gt;
        &lt;Conditions pressure='x' temp='y'/&gt;
        &lt;Measurement timeStamp='8am' reading='y'/&gt;
      &lt;/Dish2&gt;
      &lt;Dish1&gt;
        &lt;Conditions pressure='x' temp='y'/&gt;
        &lt;Measurement timeStamp='2pm' reading='y'/&gt;
      &lt;/Dish1&gt;
      &lt;Dish2&gt;
        &lt;Conditions pressure='x' temp='y'/&gt;
        &lt;Measurement timeStamp='2pm' reading='y'/&gt;
      &lt;/Dish2&gt;
     &lt;/Experiment&gt;
     &lt;Experiment id='2'&gt;
      &lt;Dish1&gt;
        &lt;Conditions pressure='x' temp='y'/&gt;
        &lt;Measurement timeStamp='9am' reading='y'/&gt;
      &lt;/Dish1&gt;
      &lt;/Experiment&gt;
      &lt;Experiment id='2'&gt;
        &lt;Dish1&gt;
          ...
  &lt;/Test&gt;
  </code></pre>
  
  <p>Desired outcome:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Test&gt;
     &lt;Experiment id='1'&gt;
       &lt;Dish1&gt;
          &lt;Observation pressure='x' temp='y' timeStamp='8am' reading='y'/&gt;
          &lt;Observation pressure='x' temp='y' timeStamp='2pm' reading='y'/&gt;
       &lt;/Dish1&gt;
       &lt;Dish2&gt;
          &lt;Observation pressure='x' temp='y' timeStamp='8am' reading='y'/&gt;
          &lt;Observation pressure='x' temp='y' timeStamp='2pm' reading='y'/&gt;
       &lt;/Dish2&gt;
     &lt;/Experiment&gt;
     &lt;Experiment id='2'&gt;
       &lt;Dish1&gt;
           &lt;Observation pressure='x' temp='y' timeStamp='9am' reading='y'/&gt;
            ...
  </code></pre>
  
  <p>Please and thank you!</p>
  
  <p>Here's what I've tried so far:</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:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/&gt;
      &lt;xsl:template match="Experiment"&gt;
          &lt;xsl:copy&gt;
              &lt;xsl:for-each select="Dish1"&gt;
                  &lt;xsl:element name="Observation"&gt;
                      &lt;xsl:attribute name="pressure"&gt;&lt;xsl:value-of select="Conditions/@pressure"/&gt;&lt;/xsl:attribute&gt;
                      &lt;xsl:attribute name="temp"&gt;&lt;xsl:value-of select="Conditions/@temp"/&gt;&lt;/xsl:attribute&gt;
                      &lt;xsl:attribute name="TimeStamp"&gt;&lt;xsl:value-of select="Measurement/@TimeStamp"/&gt;&lt;/xsl:attribute&gt;
                      &lt;xsl:attribute name="reading"&gt;&lt;xsl:value-of select="Measurement/@reading"/&gt;&lt;/xsl:attribute&gt;
                  &lt;/xsl:element&gt;
              &lt;/xsl:for-each&gt;
          &lt;/xsl:copy&gt;
      &lt;/xsl:template&gt;
      &lt;!-- copy everthing not covered above--&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;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>While writing up my failed attempts I came up with the above transformation.  It seems to be working but I need to validate the output.  Any suggestions/improvements would be appreciated.  Thank you.</p>
  
  <p>...my transformation works only on the first Experiment.  When I add:</p>
  
  <pre><code>&lt;xsl:template match="Experiment"&gt;
    &lt;xsl:copy&gt;
    &lt;xsl:for-each select="Dish2"&gt;
    &lt;xsl:element name="Observation"&gt;
    &lt;xsl:attribute name="pressure"&gt;&lt;xsl:value-of select="Conditions/@pressure"/&gt;&lt;/xsl:attribute&gt;
    &lt;xsl:attribute name="temp"&gt;&lt;xsl:value-of select="Conditions/@temp"/&gt;&lt;/xsl:attribute&gt;
    &lt;xsl:attribute name="TimeStamp"&gt;&lt;xsl:value-of select="Measurement/@TimeStamp"/&gt;&lt;/xsl:attribute&gt;
    &lt;xsl:attribute name="reading"&gt;&lt;xsl:value-of select="Measurement/@reading"/&gt;&lt;/xsl:attribute&gt;
    &lt;/xsl:element&gt;
    &lt;/xsl:for-each&gt;
    &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>...it can't find any Dish2 elements. </p>
  
  <p>Thanks Ian Roberts, having me write up my failed attempts triggered an idea, which led to the working solution below.</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:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/&gt;
      &lt;xsl:template match="Experiment"&gt;
          &lt;xsl:copy&gt;
              &lt;xsl:for-each select="Dish1"&gt;
                  &lt;xsl:element name="Observation"&gt;
                      &lt;xsl:attribute name="pressure"&gt;&lt;xsl:value-of select="Conditions/@pressure"/&gt;&lt;/xsl:attribute&gt;
                      &lt;xsl:attribute name="temp"&gt;&lt;xsl:value-of select="Conditions/@temp"/&gt;&lt;/xsl:attribute&gt;
                      &lt;xsl:attribute name="TimeStamp"&gt;&lt;xsl:value-of select="Measurement/@TimeStamp"/&gt;&lt;/xsl:attribute&gt;
                      &lt;xsl:attribute name="reading"&gt;&lt;xsl:value-of select="Measurement/@reading"/&gt;&lt;/xsl:attribute&gt;
                  &lt;/xsl:element&gt;
              &lt;/xsl:for-each&gt;
              &lt;xsl:for-each select="Dish2"&gt;
                  &lt;xsl:element name="Observation"&gt;
                      &lt;xsl:attribute name="pressure"&gt;&lt;xsl:value-of select="Conditions/@pressure"/&gt;&lt;/xsl:attribute&gt;
                      &lt;xsl:attribute name="temp"&gt;&lt;xsl:value-of select="Conditions/@temp"/&gt;&lt;/xsl:attribute&gt;
                      &lt;xsl:attribute name="TimeStamp"&gt;&lt;xsl:value-of select="Measurement/@TimeStamp"/&gt;&lt;/xsl:attribute&gt;
                      &lt;xsl:attribute name="reading"&gt;&lt;xsl:value-of select="Measurement/@reading"/&gt;&lt;/xsl:attribute&gt;
                  &lt;/xsl:element&gt;
              &lt;/xsl:for-each&gt;
          &lt;/xsl:copy&gt;
      &lt;/xsl:template&gt;
      &lt;!-- copy everthing not covered above--&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;/xsl:stylesheet&gt;
  </code></pre>