title: xslt - format new xml structure
body: <p><strong>Given XML:</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;RUNRESULT STATE=&quot;SUCCESS&quot; START=&quot;2020-09-07 08:48:11&quot; STOP=&quot;2020-09-07 08:48:13&quot; DATABASENUMBER=&quot;10&quot; TRANSFORMATION=&quot;0000036&quot;&gt;
    &lt;DETAIL&gt;
      &lt;RECORD SUBJECTID=&quot;70521192&quot; CLASSID=&quot;343&quot; GUNID=&quot;00000A00009J15ZII0&quot; FOLDERID=&quot;14858&quot; REFERENCE=&quot;pro repliku&quot; NAME=&quot;PRO VYVOLÁNÍ REPLIKACE DO MODULY&quot; CLASSNAME=&quot;Kmenová karta zboží / materiálu&quot; FOLDERNAME=&quot;Modula - pomocný&quot; TIMESTAMP=&quot;0000000061FBCCDF&quot; datum_vzniku=&quot;2020-08-17 10:55:25&quot; last_update=&quot;2020-09-07 08:47:29&quot; /&gt;
    &lt;/DETAIL&gt;
    &lt;storedProcedureResult&gt;
      &lt;items&gt;
        &lt;item&gt;
          &lt;RECORD SUBJECTID=&quot;70521192&quot; CLASSID=&quot;343&quot; GUNID=&quot;00000A00009J15ZII0&quot; FOLDERID=&quot;14858&quot; REFERENCE=&quot;pro repliku&quot; NAME=&quot;PRO VYVOLÁNÍ REPLIKACE DO MODULY&quot; CLASSNAME=&quot;Kmenová karta zboží / materiálu&quot; FOLDERNAME=&quot;Modula - pomocný&quot; TIMESTAMP=&quot;0000000061FBCCDF&quot; datum_vzniku=&quot;2020-08-17 10:55:25&quot; last_update=&quot;2020-09-07 08:47:29&quot; /&gt;
          &lt;Datastores&gt;
            &lt;Datastore TableName=&quot;invoiceHeader&quot;&gt;
              &lt;Definition&gt;
                &lt;Columns&gt;
                  &lt;Column Name=&quot;c_cs&quot; DataType=&quot;int&quot; /&gt;
                  &lt;Column Name=&quot;c_objed&quot; DataType=&quot;string&quot; /&gt;
                &lt;/Columns&gt;
              &lt;/Definition&gt;
              &lt;DataRows&gt;
                &lt;DataRow c_cs=&quot;68306186&quot; c_objed=&quot;ODH2001295&quot; /&gt;
                &lt;DataRow c_cs=&quot;68402059&quot; c_objed=&quot;ODH2001296&quot; /&gt;
  &lt;/DataRows&gt;
        &lt;/Datastore&gt;
  
  &lt;Datastore TableName=&quot;invoiceItems&quot;&gt;
          &lt;Definition&gt;
            &lt;Columns&gt;
              &lt;Column Name=&quot;c_cs&quot; DataType=&quot;int&quot; /&gt;
              &lt;Column Name=&quot;c_objed&quot; DataType=&quot;string&quot; /&gt;
              &lt;Column Name=&quot;odber_dodav&quot; DataType=&quot;string&quot; /&gt;
              &lt;Column Name=&quot;ORD_TIPOOP&quot; DataType=&quot;string&quot; /&gt;
              &lt;Column Name=&quot;zdroj_ref&quot; DataType=&quot;string&quot; /&gt;
              &lt;Column Name=&quot;poc&quot; DataType=&quot;decimal&quot; /&gt;
              &lt;Column Name=&quot;c_r&quot; DataType=&quot;int&quot; /&gt;
            &lt;/Columns&gt;
          &lt;/Definition&gt;
          &lt;DataRows&gt;
            &lt;DataRow c_cs=&quot;68306186&quot; c_objed=&quot;ODH2001295&quot; odber_dodav=&quot;Divize ND + SERVIS&quot; ORD_TIPOOP=&quot;P&quot; zdroj_ref=&quot;XXX1&quot; poc=&quot;1.000000&quot; c_r=&quot;13&quot; /&gt;
            &lt;DataRow c_cs=&quot;68306186&quot; c_objed=&quot;ODH2001295&quot; odber_dodav=&quot;Divize ND + SERVIS&quot; ORD_TIPOOP=&quot;P&quot; zdroj_ref=&quot;XXX2&quot; poc=&quot;1.000000&quot; c_r=&quot;15&quot; /&gt;
            &lt;DataRow c_cs=&quot;68306186&quot; c_objed=&quot;ODH2001295&quot; odber_dodav=&quot;Divize ND + SERVIS&quot; ORD_TIPOOP=&quot;P&quot; zdroj_ref=&quot;XXX3&quot; poc=&quot;4.000000&quot; c_r=&quot;17&quot; /&gt;
            &lt;DataRow c_cs=&quot;68306186&quot; c_objed=&quot;ODH2001295&quot; odber_dodav=&quot;Divize ND + SERVIS&quot; ORD_TIPOOP=&quot;P&quot; zdroj_ref=&quot;XXX4&quot; poc=&quot;2.000000&quot; c_r=&quot;18&quot; /&gt;
            &lt;DataRow c_cs=&quot;68306186&quot; c_objed=&quot;ODH2001295&quot; odber_dodav=&quot;Divize ND + SERVIS&quot; ORD_TIPOOP=&quot;P&quot; zdroj_ref=&quot;XXX5&quot; poc=&quot;1.000000&quot; c_r=&quot;19&quot; /&gt;
            &lt;DataRow c_cs=&quot;68306186&quot; c_objed=&quot;ODH2001296&quot; odber_dodav=&quot;Divize ND + SERVIS&quot; ORD_TIPOOP=&quot;P&quot; zdroj_ref=&quot;YYY1&quot; poc=&quot;2.000000&quot; c_r=&quot;21&quot; /&gt;
            &lt;DataRow c_cs=&quot;68306186&quot; c_objed=&quot;ODH2001296&quot; odber_dodav=&quot;Divize ND + SERVIS&quot; ORD_TIPOOP=&quot;P&quot; zdroj_ref=&quot;YYY2&quot; poc=&quot;30.000000&quot; c_r=&quot;22&quot; /&gt;
            &lt;DataRow c_cs=&quot;68306186&quot; c_objed=&quot;ODH2001296&quot; odber_dodav=&quot;Divize ND + SERVIS&quot; ORD_TIPOOP=&quot;P&quot; zdroj_ref=&quot;YYY3&quot; poc=&quot;1.000000&quot; c_r=&quot;23&quot; /&gt;
            &lt;DataRow c_cs=&quot;68306186&quot; c_objed=&quot;ODH2001296&quot; odber_dodav=&quot;Divize ND + SERVIS&quot; ORD_TIPOOP=&quot;P&quot; zdroj_ref=&quot;YYY4&quot; poc=&quot;1.000000&quot; c_r=&quot;24&quot; /&gt;
            &lt;DataRow c_cs=&quot;68306186&quot; c_objed=&quot;ODH2001296&quot; odber_dodav=&quot;Divize ND + SERVIS&quot; ORD_TIPOOP=&quot;P&quot; zdroj_ref=&quot;YYY5&quot; poc=&quot;1.000000&quot; c_r=&quot;25&quot; /&gt;
  &lt;/DataRows&gt;
            &lt;/Datastore&gt;
          &lt;/Datastores&gt;
        &lt;/item&gt;
      &lt;/items&gt;
    &lt;/storedProcedureResult&gt;
  &lt;/RUNRESULT&gt;
  </code></pre>
  <p><strong>Given XSLT:</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;xsl:stylesheet version=&quot;1.0&quot;
  xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
  &lt;xsl:template match=&quot;/&quot;&gt;
  
      &lt;xxx&gt;
          &lt;objednavka&gt;
              &lt;hlavicka&gt;
              &lt;xsl:for-each select=&quot;/RUNRESULT/storedProcedureResult/items/item/Datastores/Datastore[1]/DataRows/DataRow&quot;&gt;
                  &lt;c_objed&gt;
                      &lt;xsl:value-of select=&quot;@c_objed&quot;/&gt;
                  &lt;/c_objed&gt;
                  &lt;polozka&gt;
                  &lt;xsl:for-each select=&quot;/RUNRESULT/storedProcedureResult/items/item/Datastores/Datastore[2]/DataRows/DataRow&quot;&gt;
                      &lt;zdroj_ref&gt;
                          &lt;xsl:value-of select=&quot;@zdroj_ref&quot;/&gt;
                      &lt;/zdroj_ref&gt;
                  &lt;/xsl:for-each&gt;
              &lt;/polozka&gt;
                  &lt;/xsl:for-each&gt;
              &lt;/hlavicka&gt;
          &lt;/objednavka&gt;
      &lt;/xxx&gt;
  &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  <p><strong>Output looks like this:</strong></p>
  <pre><code>&lt;xxx&gt;
     &lt;objednavka&gt;
        &lt;hlavicka&gt;
           &lt;c_objed&gt;ODH2001295&lt;/c_objed&gt;
           &lt;polozka&gt;
              &lt;zdroj_ref&gt;XXX1&lt;/zdroj_ref&gt;
              &lt;zdroj_ref&gt;XXX2&lt;/zdroj_ref&gt;
              &lt;zdroj_ref&gt;XXX3&lt;/zdroj_ref&gt;
              &lt;zdroj_ref&gt;XXX4&lt;/zdroj_ref&gt;
              &lt;zdroj_ref&gt;XXX5&lt;/zdroj_ref&gt;
              &lt;zdroj_ref&gt;YYY1&lt;/zdroj_ref&gt;
              &lt;zdroj_ref&gt;YYY2&lt;/zdroj_ref&gt;
              &lt;zdroj_ref&gt;YYY3&lt;/zdroj_ref&gt;
              &lt;zdroj_ref&gt;YYY4&lt;/zdroj_ref&gt;
              &lt;zdroj_ref&gt;YYY5&lt;/zdroj_ref&gt;
           &lt;/polozka&gt;
         &lt;/hlavicka&gt;
  
         &lt;hlavicka&gt;
              &lt;c_objed&gt;ODH2001296&lt;/c_objed&gt;
           &lt;polozka&gt;
              &lt;zdroj_ref&gt;XXX1&lt;/zdroj_ref&gt;
              &lt;zdroj_ref&gt;XXX2&lt;/zdroj_ref&gt;
              &lt;zdroj_ref&gt;XXX3&lt;/zdroj_ref&gt;
              &lt;zdroj_ref&gt;XXX4&lt;/zdroj_ref&gt;
              &lt;zdroj_ref&gt;XXX5&lt;/zdroj_ref&gt;
              &lt;zdroj_ref&gt;YYY1&lt;/zdroj_ref&gt;
              &lt;zdroj_ref&gt;YYY2&lt;/zdroj_ref&gt;
              &lt;zdroj_ref&gt;YYY3&lt;/zdroj_ref&gt;
              &lt;zdroj_ref&gt;YYY4&lt;/zdroj_ref&gt;
              &lt;zdroj_ref&gt;YYY5&lt;/zdroj_ref&gt;
           &lt;/polozka&gt;
          &lt;/hlavicka&gt;
  </code></pre>
  <p><strong>Desired output:</strong></p>
  <pre><code>&lt;xxx&gt;
      &lt;objednavka&gt;
          &lt;hlavicka&gt;
              &lt;c_objed&gt;ODH2001295&lt;/c_objed&gt;
                  &lt;polozka&gt;
                      &lt;zdroj_ref&gt;XXX1&lt;/zdroj_ref&gt;
                      &lt;zdroj_ref&gt;XXX2&lt;/zdroj_ref&gt;
                      &lt;zdroj_ref&gt;XXX3&lt;/zdroj_ref&gt;
                      &lt;zdroj_ref&gt;XXX4&lt;/zdroj_ref&gt;
                      &lt;zdroj_ref&gt;XXX5&lt;/zdroj_ref&gt;
                  &lt;/polozka&gt;
          &lt;/hlavicka&gt;
          &lt;hlavicka&gt;
              &lt;c_objed&gt;ODH2001296&lt;/c_objed&gt;
                  &lt;polozka&gt;
                      &lt;zdroj_ref&gt;YYY1&lt;/zdroj_ref&gt;
                      &lt;zdroj_ref&gt;YYY2&lt;/zdroj_ref&gt;
                      &lt;zdroj_ref&gt;YYY3&lt;/zdroj_ref&gt;
                      &lt;zdroj_ref&gt;YYY4&lt;/zdroj_ref&gt;
                      &lt;zdroj_ref&gt;YYY5&lt;/zdroj_ref&gt;
                  &lt;/polozka&gt;
          &lt;/hlavicka&gt;
      &lt;/objednavka&gt;
  &lt;/xxx&gt;
  </code></pre>
  <p>I think I need to write some criteria in &lt;xsl:value-of select=&quot;@zdroj_ref&quot;/&gt; and it should be something like this</p>
  <pre><code>&lt;xsl:value-of select=&quot;@zdroj_ref if @c_objed(header) == @c_objed(items) &quot;/&gt;
  </code></pre>
  <p>Right now I'm getting always every items data for each header.</p>
  <p>Basically I want to display only a data that corresponds with header and items values.</p>