title: XSLT foreach repeating nodes to flat
body: <p>I have an XML schema that I need to extract values from but I am not having much luck.</p>
  
  <p>The souce schema is:</p>
  
  <pre><code>&lt;Reading&gt;
     &lt;State&gt;
        &lt;StateValues&gt;
           &lt;Name&gt;
           &lt;Value&gt;
        &lt;/StateValues&gt;
     &lt;/State&gt;
  &lt;Reading&gt;
  </code></pre>
  
  <p>An example of the XML would be:</p>
  
  <pre><code>&lt;Reading&gt;
     &lt;State&gt;
        &lt;StateValues&gt;
           &lt;Name&gt;Reading1&lt;/Name&gt;
           &lt;Value&gt;1234&lt;/Value&gt;
        &lt;/StateValues&gt;
        &lt;StateValues&gt;
           &lt;Name&gt;Reading2&lt;/Name&gt;
           &lt;Value&gt;2345&lt;/Value&gt;
        &lt;/StateValues&gt;
     &lt;/State&gt;
     &lt;State&gt;
        &lt;StateValues&gt;
           &lt;Name&gt;Reading3&lt;/Name&gt;
           &lt;Value&gt;4321&lt;/Value&gt;
        &lt;/StateValues&gt;
     &lt;/State&gt;
  &lt;Reading&gt;
  </code></pre>
  
  <p>I need a result that is:</p>
  
  <pre><code>&lt;Readings&gt;
    &lt;Reading1&gt;1234&lt;/Reading1&gt;
    &lt;Reading2&gt;2345&lt;/Reading2&gt;
    &lt;Reading3&gt;4321&lt;/Reading3&gt;
  &lt;/Readings&gt;
  </code></pre>
  
  <p>I have no control over the schemas.</p>
  
  <p>I have been trying to use a <code>xsl:foreach</code> with an <code>xsl:if</code> that checks the name but I just get the first value repeated 3 times.  Any tips? I've been trying to figure this out for 2 days and I am not an XSLT expert...</p>
  
  <p>Thanks.
  EDIT : Sorry for not posting my XSLT.  I had tried several versions and didn't think it would be useful.  Here it is:</p>
  
  <blockquote>
    <p>Blockquote</p>
  </blockquote>
  
  <pre><code>&lt;xsl:element name="ns0:Readings"&gt;
     &lt;xsl:element name="ns0:Current"&gt;
           &lt;xsl:for-each select="/*[local-name()='gatewayInterface' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Reading' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='State' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='StateValues' and namespace-uri()='http://biztalk.gateway.com']"&gt;               
              &lt;xsl:choose&gt;
                 &lt;xsl:when test="Name='TOTAL'"&gt;
              &lt;xsl:element name="ns0:Total"&gt;
                  &lt;xsl:value-of select="/*[local-name()='gatewayInterface' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Reading' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='State' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='StateValues' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Name' and namespace-uri()='http://biztalk.gateway.com']/text()='Total'" /&gt;
              &lt;/xsl:element&gt;
             &lt;/xsl:when&gt;
          &lt;/xsl:choose&gt;
          &lt;xsl:choose&gt;
             &lt;xsl:when test="/*[local-name()='gatewayInterface' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Reading' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='State' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='StateValues' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Name' and namespace-uri()='http://biztalk.gateway.com']/text()='Reading1'"&gt;
          &lt;xsl:element name="ns0:Reading1"&gt;
              &lt;xsl:value-of select="/*[local-name()='gatewayInterface' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Reading' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='State' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='StateValues' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Name' and namespace-uri()='http://biztalk.gateway.com']/text()" /&gt;
              &lt;/xsl:element&gt;
             &lt;/xsl:when&gt;
          &lt;/xsl:choose&gt;
          &lt;xsl:choose&gt;
             &lt;xsl:when test="/*[local-name()='gatewayInterface' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Reading' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='State' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='StateValues' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Name' and namespace-uri()='http://biztalk.gateway.com']/text() = 'Reading2'"&gt;
          &lt;xsl:element name="ns0:Reading2"&gt;
              &lt;xsl:value-of select="/*[local-name()='gatewayInterface' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Reading' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='State' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='StateValues' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Name' and namespace-uri()='http://biztalk.gateway.com']/text()" /&gt;
              &lt;/xsl:element&gt;
             &lt;/xsl:when&gt;
          &lt;/xsl:choose&gt;
          &lt;xsl:choose&gt;
             &lt;xsl:when test="/*[local-name()='gatewayInterface' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Reading' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='State' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='StateValues' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Name' and namespace-uri()='http://biztalk.gateway.com']/text() = 'Reading3'"&gt;
          &lt;xsl:element name="ns0:Reading3"&gt;
              &lt;xsl:value-of select="/*[local-name()='gatewayInterface' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Reading' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='State' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='StateValues' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Name' and namespace-uri()='http://biztalk.gateway.com']/text()" /&gt;
              &lt;/xsl:element&gt;
             &lt;/xsl:when&gt;
          &lt;/xsl:choose&gt;
          &lt;xsl:choose&gt;
             &lt;xsl:when test="/*[local-name()='gatewayInterface' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Reading' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='State' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='StateValues' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Name' and namespace-uri()='http://biztalk.gateway.com']/text() = 'Reading4'"&gt;
          &lt;xsl:element name="ns0:Reading4"&gt;
              &lt;xsl:value-of select="/*[local-name()='gatewayInterface' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Reading' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='State' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='StateValues' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Name' and namespace-uri()='http://biztalk.gateway.com']/text()" /&gt;
              &lt;/xsl:element&gt;
             &lt;/xsl:when&gt;
          &lt;/xsl:choose&gt;
       &lt;/xsl:for-each&gt;
     &lt;/xsl:element&gt;
  &lt;/xsl:element&gt;
  </code></pre>