title: How to move a node in XML using XSLT mapping with condition
body: <p>I've got an xml similar to this:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;MATMAS05&gt;
      &lt;IDOC BEGIN="1"&gt;
          &lt;EDI_DC40 SEGMENT="1"&gt;
              &lt;CREDAT&gt;20180822&lt;/CREDAT&gt;
              &lt;CRETIM&gt;180201&lt;/CRETIM&gt;
              &lt;SERIAL&gt;20180821134354&lt;/SERIAL&gt;
          &lt;/EDI_DC40&gt;
          &lt;E1MARAM SEGMENT="1"&gt;
              &lt;MSGFN&gt;005&lt;/MSGFN&gt;
              &lt;MATNR&gt;000000000002000010&lt;/MATNR&gt;
              &lt;E1MARA1 SEGMENT="1"&gt;
                  &lt;MAXC&gt;0.000&lt;/MAXC&gt;
                  &lt;MAXC_TOL&gt;0.0&lt;/MAXC_TOL&gt;
              &lt;/E1MARA1&gt;
              &lt;E1MAKTM SEGMENT="1"&gt;
                  &lt;MSGFN&gt;005&lt;/MSGFN&gt;
                  &lt;SPRAS&gt;E&lt;/SPRAS&gt;
              &lt;/E1MAKTM&gt;
              &lt;E1MARCM SEGMENT="1"&gt;
                  &lt;MSGFN&gt;009&lt;/MSGFN&gt;
                  &lt;WERKS&gt;3030&lt;/WERKS&gt;
                  &lt;E1MARDM SEGMENT="1"&gt;
                      &lt;MSGFN&gt;009&lt;/MSGFN&gt;
                      &lt;LGORT&gt;1000&lt;/LGORT&gt;
                  &lt;/E1MARDM&gt;
                  &lt;E1MPGDM SEGMENT="1"&gt;
                      &lt;MSGFN&gt;005&lt;/MSGFN&gt;
                  &lt;/E1MPGDM&gt;
              &lt;/E1MARCM&gt;
              &lt;E1MARCM SEGMENT="1"&gt;
                  &lt;MSGFN&gt;005&lt;/MSGFN&gt;
                  &lt;WERKS&gt;3040&lt;/WERKS&gt;
                  &lt;E1MARDM SEGMENT="1"&gt;
                      &lt;MSGFN&gt;005&lt;/MSGFN&gt;
                      &lt;LGORT&gt;1000&lt;/LGORT&gt;
                  &lt;/E1MARDM&gt;
                  &lt;E1MPGDM SEGMENT="1"&gt;
                      &lt;MSGFN&gt;005&lt;/MSGFN&gt;
                  &lt;/E1MPGDM&gt;
              &lt;/E1MARCM&gt;
              &lt;E1MARMM SEGMENT="1"&gt;
                  &lt;MSGFN&gt;005&lt;/MSGFN&gt;
                  &lt;MEINH&gt;EA&lt;/MEINH&gt;
              &lt;/E1MARMM&gt;
              &lt;E1MBEWM SEGMENT="1"&gt;
                  &lt;MSGFN&gt;009&lt;/MSGFN&gt;
                  &lt;BWKEY&gt;3030&lt;/BWKEY&gt;
              &lt;/E1MBEWM&gt;
              &lt;E1MBEWM SEGMENT="1"&gt;
                  &lt;MSGFN&gt;005&lt;/MSGFN&gt;
                  &lt;BWKEY&gt;3040&lt;/BWKEY&gt;
              &lt;/E1MBEWM&gt;
              &lt;E1MLANM SEGMENT="1"&gt;
                  &lt;MSGFN&gt;005&lt;/MSGFN&gt;
                  &lt;ALAND&gt;AU&lt;/ALAND&gt;
              &lt;/E1MLANM&gt;
          &lt;/E1MARAM&gt;
      &lt;/IDOC&gt;
  &lt;/MATMAS05&gt;
  </code></pre>
  
  <p>I need to move E1MBEWM nodes into appropriate E1MARCM node where BWKEY=WERKS</p>
  
  <p>So, basically it should be the output like that:</p>
  
  <pre><code>    &lt;?xml version="1.0"?&gt;
  &lt;MATMAS05&gt;
      &lt;IDOC BEGIN="1"&gt;
          &lt;EDI_DC40 SEGMENT="1"&gt;
              &lt;CREDAT&gt;20180822&lt;/CREDAT&gt;
              &lt;CRETIM&gt;180201&lt;/CRETIM&gt;
              &lt;SERIAL&gt;20180821134354&lt;/SERIAL&gt;
          &lt;/EDI_DC40&gt;
          &lt;E1MARAM SEGMENT="1"&gt;
              &lt;MSGFN&gt;005&lt;/MSGFN&gt;
              &lt;MATNR&gt;000000000002000010&lt;/MATNR&gt;
              &lt;E1MARA1 SEGMENT="1"&gt;
                  &lt;MAXC&gt;0.000&lt;/MAXC&gt;
                  &lt;MAXC_TOL&gt;0.0&lt;/MAXC_TOL&gt;
              &lt;/E1MARA1&gt;
              &lt;E1MAKTM SEGMENT="1"&gt;
                  &lt;MSGFN&gt;005&lt;/MSGFN&gt;
                  &lt;SPRAS&gt;E&lt;/SPRAS&gt;
              &lt;/E1MAKTM&gt;
              &lt;E1MARCM SEGMENT="1"&gt;
                  &lt;MSGFN&gt;009&lt;/MSGFN&gt;
                  &lt;WERKS&gt;3030&lt;/WERKS&gt;
                  &lt;E1MARDM SEGMENT="1"&gt;
                      &lt;MSGFN&gt;009&lt;/MSGFN&gt;
                      &lt;LGORT&gt;1000&lt;/LGORT&gt;
                  &lt;/E1MARDM&gt;
                  &lt;E1MPGDM SEGMENT="1"&gt;
                      &lt;MSGFN&gt;005&lt;/MSGFN&gt;
                  &lt;/E1MPGDM&gt;
                  &lt;E1MBEWM SEGMENT="1"&gt;
                     &lt;MSGFN&gt;009&lt;/MSGFN&gt;
                     &lt;BWKEY&gt;3030&lt;/BWKEY&gt;
                  &lt;/E1MBEWM&gt;
              &lt;/E1MARCM&gt;
              &lt;E1MARCM SEGMENT="1"&gt;
                  &lt;MSGFN&gt;005&lt;/MSGFN&gt;
                  &lt;WERKS&gt;3040&lt;/WERKS&gt;
                  &lt;E1MARDM SEGMENT="1"&gt;
                      &lt;MSGFN&gt;005&lt;/MSGFN&gt;
                      &lt;LGORT&gt;1000&lt;/LGORT&gt;
                  &lt;/E1MARDM&gt;
                  &lt;E1MPGDM SEGMENT="1"&gt;
                      &lt;MSGFN&gt;005&lt;/MSGFN&gt;
                  &lt;/E1MPGDM&gt;
                  &lt;E1MBEWM SEGMENT="1"&gt;
                     &lt;MSGFN&gt;005&lt;/MSGFN&gt;
                     &lt;BWKEY&gt;3040&lt;/BWKEY&gt;
                  &lt;/E1MBEWM&gt;
              &lt;/E1MARCM&gt;
              &lt;E1MARMM SEGMENT="1"&gt;
                  &lt;MSGFN&gt;005&lt;/MSGFN&gt;
                  &lt;MEINH&gt;EA&lt;/MEINH&gt;
              &lt;/E1MARMM&gt;
              &lt;E1MLANM SEGMENT="1"&gt;
                  &lt;MSGFN&gt;005&lt;/MSGFN&gt;
                  &lt;ALAND&gt;AU&lt;/ALAND&gt;
              &lt;/E1MLANM&gt;
          &lt;/E1MARAM&gt;
      &lt;/IDOC&gt;
  &lt;/MATMAS05&gt;
  </code></pre>
  
  <p>the possible issue is that sometimes E1MBEWM node might not be in incoming message.</p>
  
  <p>I'm trying to use XSLT something like that:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"&gt;
   &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt;
   &lt;xsl:strip-space elements="*"/&gt;
   &lt;xsl:template match="node()|@*" name="identity"&gt;
    &lt;xsl:copy&gt;
     &lt;xsl:apply-templates select="node()|@*"/&gt;
    &lt;/xsl:copy&gt;
   &lt;/xsl:template&gt;
   &lt;xsl:template match="E1MARCM/*[1]"&gt;
    &lt;xsl:copy-of select="/*/E1MBEWM[1]"/&gt;
    &lt;xsl:call-template name="identity"/&gt;
   &lt;/xsl:template&gt;
   &lt;xsl:template match="/*/E1MBEWM[1]"/&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>but it doesn't give result needed if there are several E1MARCM nodes. I understand that I need to use two for each cycles, but not sure how to implement it here.</p>
  
  <p>Can you please help?</p>