title: XSLT: Change an attribute based on a condition from another attribute
body: <p>Although there are many similar questions, none of them seem to answer my requirements.</p>
  
  <p>I would like to change the <em>from</em> attribute of the element <em>invoice:transport</em>  within my XML based on the <em>ean_party</em> attribute of the element <em>invoice:insurance</em>.</p>
  
  <p>The input XML looks like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;invoice:response xmlns:invoice="http://www.forum-datenaustausch.ch/invoice" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" language="de" modus="production" xsi:schemaLocation="http://www.forum-datenaustausch.ch/invoice generalInvoiceResponse_440.xsd"&gt;
      &lt;invoice:processing&gt;
          &lt;!--original "from" attribute--&gt;
          &lt;invoice:transport from="7601003000123" to="7601003000456"&gt;
              &lt;invoice:via sequence_id="1" via="7601003000789"/&gt;
          &lt;/invoice:transport&gt;
      &lt;/invoice:processing&gt;
      &lt;invoice:payload copy="false" credit_advice="false" response_timestamp="1515671999" storno="false" type="invoice"&gt;
          &lt;invoice:invoice request_date="2018-01-01T00:00:00" request_id="999999" request_timestamp="1515671999"/&gt;
          &lt;invoice:body&gt;
              &lt;invoice:insurance ean_party="7601003000999"/&gt;
          &lt;/invoice:body&gt;
      &lt;/invoice:payload&gt;
  &lt;/invoice:response&gt;
  </code></pre>
  
  <p>The output XML should look like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;invoice:response xmlns:invoice="http://www.forum-datenaustausch.ch/invoice" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" language="de" modus="production" xsi:schemaLocation="http://www.forum-datenaustausch.ch/invoice generalInvoiceResponse_440.xsd"&gt;
      &lt;invoice:processing&gt;
          &lt;!--replaced "from" attribute with ean_party attribute--&gt;
          &lt;invoice:transport from="7601003000999" to="7601003000456"&gt;
              &lt;invoice:via sequence_id="1" via="7601003000789"/&gt;
          &lt;/invoice:transport&gt;
      &lt;/invoice:processing&gt;
      &lt;invoice:payload copy="false" credit_advice="false" response_timestamp="1515671999" storno="false" type="invoice"&gt;
          &lt;invoice:invoice request_date="2018-01-01T00:00:00" request_id="999999" request_timestamp="1515671999"/&gt;
          &lt;invoice:body&gt;
              &lt;!--ean_party attribute that matches my condition--&gt;
              &lt;invoice:insurance ean_party="7601003000999"/&gt;
          &lt;/invoice:body&gt;
      &lt;/invoice:payload&gt;
  &lt;/invoice:response&gt;
  </code></pre>
  
  <p>I have tried adapting the XSLT from another post, but I didn't work:</p>
  
  <pre><code>&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"&gt;
      &lt;xsl:output method="xml" omit-xml-declaration="yes"/&gt;
      &lt;!--declare condition for transport block--&gt;
      &lt;xsl:variable name="mandant"&gt;
          &lt;xsl:choose&gt;
              &lt;xsl:when test="//transport = '7601003000123'"&gt;
                  &lt;xsl:value-of select="'7601003000999'"/&gt;
              &lt;/xsl:when&gt;
              &lt;xsl:otherwise&gt;
                  &lt;xsl:value-of select="//transport"/&gt;
              &lt;/xsl:otherwise&gt;
          &lt;/xsl:choose&gt;        
      &lt;/xsl:variable&gt;
      &lt;!--dynamic template for replacing current value to condition value--&gt;    
      &lt;xsl:template name="value-to-replace"&gt;
          &lt;xsl:param name="param.str"/&gt;
          &lt;xsl:param name="param.target"/&gt;
          &lt;xsl:param name="param.replacement"/&gt;
          &lt;xsl:choose&gt;
              &lt;xsl:when test="contains($param.str, $param.target)"&gt;                    
                  &lt;xsl:value-of select="concat(substring-before($param.str, $param.target), $param.replacement, substring-after($param.str, $param.target))"/&gt;              
              &lt;/xsl:when&gt;
              &lt;xsl:otherwise&gt;
                  &lt;xsl:value-of select="$param.str"/&gt;
              &lt;/xsl:otherwise&gt;
          &lt;/xsl:choose&gt;
      &lt;/xsl:template&gt;
  
      &lt;!--replace value in transport block with mandant variable condition--&gt;
      &lt;xsl:template match="@from"&gt;
          &lt;xsl:attribute name="{name()}"&gt;                                    
              &lt;xsl:call-template name="value-to-replace"&gt;                       
                  &lt;xsl:with-param name="param.str" select="."/&gt;                       
                  &lt;xsl:with-param name="param.target" select="//transport"/&gt;                       
                  &lt;xsl:with-param name="param.replacement" select="$mandant"/&gt;                   
              &lt;/xsl:call-template&gt;
          &lt;/xsl:attribute&gt;                      
      &lt;/xsl:template&gt;             
      &lt;!--copy all nodes--&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>Thanks in adavance!</p>