title: Passing Map to XSLT and changing values by keys
body: <p>I have a <code>Map</code> with several entries that I want to apply on an XML document, using XSLT.</p>
  <p>Whenever it finds <code>TESTWORD</code> in the XML, take the following key and replace that with the appropriate value from the map. Also replace the name of that node.</p>
  <p><strong>Input:</strong></p>
  <pre><code>&lt;Company&gt;
     &lt;Employee&gt;
        &lt;FirstName&gt;Homer&lt;/FirstName&gt;
        &lt;LastName&gt;Simpson&lt;/LastName&gt;
        &lt;ContactNo&gt;1234567890&lt;/ContactNo&gt;
        &lt;Address&gt;
           &lt;City&gt;Springfield&lt;/City&gt;
           &lt;Note&gt;TESTWORD key1&lt;/Note&gt; &lt;!-- change this --&gt;
        &lt;/Address&gt;
     &lt;/Employee&gt;
     &lt;Employee&gt;
        &lt;FirstName&gt;Peter&lt;/FirstName&gt;
        &lt;LastName&gt;Griffin&lt;/LastName&gt;
        &lt;ContactNo&gt;0987654321&lt;/ContactNo&gt;
        &lt;Address&gt;
           &lt;City&gt;Quahog&lt;/City&gt;
           &lt;Note&gt;TESTWORD key2&lt;/Note&gt; &lt;!-- change this --&gt;
        &lt;/Address&gt;
     &lt;/Employee&gt;
  &lt;/Company&gt;
  </code></pre>
  <p><strong>Expected output:</strong></p>
  <pre><code>&lt;Company&gt;
     &lt;Employee&gt;
        &lt;FirstName&gt;Homer&lt;/FirstName&gt;
        &lt;LastName&gt;Simpson&lt;/LastName&gt;
        &lt;ContactNo&gt;1234567890&lt;/ContactNo&gt;
        &lt;Address&gt;
           &lt;City&gt;Springfield&lt;/City&gt;
           &lt;NewElem&gt;my value&lt;/NewElem&gt; &lt;!-- changed --&gt;
        &lt;/Address&gt;
     &lt;/Employee&gt;
     &lt;Employee&gt;
        &lt;FirstName&gt;Peter&lt;/FirstName&gt;
        &lt;LastName&gt;Griffin&lt;/LastName&gt;
        &lt;ContactNo&gt;0987654321&lt;/ContactNo&gt;
        &lt;Address&gt;
           &lt;City&gt;Quahog&lt;/City&gt;
           &lt;NewElem&gt;another value&lt;/NewElem&gt; &lt;!-- changed --&gt;
        &lt;/Address&gt;
     &lt;/Employee&gt;
  &lt;/Company&gt;
  </code></pre>
  <hr />
  <p><strong>Java:</strong></p>
  <pre><code>Map&lt;String, String&gt; map = new HashMap&lt;&gt;();
  map.put(&quot;key1&quot;, &quot;my value&quot;);
  map.put(&quot;key2&quot;, &quot;another value&quot;);
  
  try {
      TransformerFactory factory = TransformerFactory.newInstance();
      Source xslt = new StreamSource(new File(&quot;doc.xslt&quot;));
      Transformer transformer = factory.newTransformer(xslt);
      transformer.setParameter(&quot;map&quot;, map); // Passing map as parameter
  
      Source text = new StreamSource(new File(&quot;doc.xml&quot;));
      transformer.transform(text, new StreamResult(new File(&quot;newXml.xml&quot;)));
  } catch (URISyntaxException | TransformerException ex) {}
  </code></pre>
  <p><strong>XSLT:</strong></p>
  <pre><code>&lt;xsl:stylesheet
      xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot; version=&quot;2.0&quot;&gt;
      &lt;xsl:output indent=&quot;yes&quot; method=&quot;xml&quot; encoding=&quot;utf-8&quot; /&gt;
  
      &lt;xsl:key name=&quot;map&quot; /&gt; &lt;!-- the map with my keys and values --&gt;
  
      &lt;xsl:template match=&quot;node()|@*&quot;&gt;
          &lt;xsl:copy&gt;
              &lt;xsl:apply-templates select=&quot;node()|@*&quot; /&gt;
          &lt;/xsl:copy&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template match=&quot;Note[.='TESTWORD key1']&quot;&gt; &lt;!-- iterate all params --&gt;
          &lt;xsl:element name=&quot;NewElem&quot;&gt;
              &lt;xsl:value-of select=&quot;$key1&quot; /&gt; &lt;!-- replace all values --&gt;
          &lt;/xsl:element&gt;
      &lt;/xsl:template&gt;
  
      &lt;!--
      &lt;xsl:template match=&quot;Note[.='TESTWORD key2']&quot;&gt;
          &lt;xsl:element name=&quot;NewElem&quot;&gt;
              &lt;xsl:value-of select=&quot;$key2&quot; /&gt;
          &lt;/xsl:element&gt;
      &lt;/xsl:template&gt; 
      --&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  <p>Here you can check it: <a href="http://xsltransform.net/3N7GxDd" rel="nofollow noreferrer">http://xsltransform.net/3N7GxDd</a></p>