title: XSLT 3.0 - Removing and Replacing nodes (with Saxon 9.7)
body: <p>I have an input XML like following (requirements are highlighted as comments):</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;Aggregated_Data&gt;
  
      &lt;References&gt;
          &lt;Reference&gt;
              &lt;Type&gt;Company_Reference_ID&lt;/Type&gt;
              &lt;Value&gt;Dest Company&lt;/Value&gt;
          &lt;/Reference&gt;
          &lt;Reference&gt;
              &lt;Type&gt;ABC_Reference_ID&lt;/Type&gt;
              &lt;Value&gt;XYZ&lt;/Value&gt;
          &lt;/Reference&gt;
      &lt;/References&gt;
  
  
      &lt;wd:root xmlns:wd=&quot;urn:com.workday/bsvc&quot;&gt;
          &lt;wd:Calculated_Field_Data xmlns:wd=&quot;urn:com.workday/bsvc&quot;
              xmlns:env=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;&gt;
              &lt;wd:Calculated_Field_Reference_ID&gt;Ref&lt;/wd:Calculated_Field_Reference_ID&gt;
              &lt;wd:Name&gt;NameABC&lt;/wd:Name&gt;
             
              &lt;wd:Conditional_Expression_Calculated_Field_Data&gt;
                  &lt;wd:Conditional_Expression_Calculated_Field_Condition_Data&gt;
                      &lt;wd:Condition_Item_Data&gt;
                          &lt;wd:Order&gt;dd&lt;/wd:Order&gt;
                          &lt;wd:And_Or_Reference&gt;
                              &lt;wd:ID wd:type=&quot;WID&quot;&gt;da4e1c34446c11de98360015c5e6daf6&lt;/wd:ID&gt;
                              &lt;wd:ID wd:type=&quot;And_Or_Operator_Name&quot;&gt;And&lt;/wd:ID&gt;
                          &lt;/wd:And_Or_Reference&gt; 
  
                          &lt;!-- Need to remove every wd:ID node except the last one (in this case last one is &quot;&lt;wd:ID wd:type=&quot;Company_Reference_ID&quot;&gt;ABC&lt;/wd:ID&gt;&quot;) and replace last one's value from the value from References node with the same type (In this case type = Company_Reference_ID) --&gt;
                          &lt;wd:Filter_Instances_Reference&gt;
                              &lt;wd:ID wd:type=&quot;WID&quot;&gt;e50d309a6d8540e3b533219cfa2c330b&lt;/wd:ID&gt;
                              &lt;wd:ID wd:type=&quot;Organization_Reference_ID&quot;&gt;ABC&lt;/wd:ID&gt;
                              &lt;wd:ID wd:type=&quot;Company_Reference_ID&quot;&gt;ABC&lt;/wd:ID&gt;
                          &lt;/wd:Filter_Instances_Reference&gt;                        
                         
                      &lt;/wd:Condition_Item_Data&gt;
                  &lt;/wd:Conditional_Expression_Calculated_Field_Condition_Data&gt;
              &lt;/wd:Conditional_Expression_Calculated_Field_Data&gt;
          &lt;/wd:Calculated_Field_Data&gt;
          
          &lt;!-- Has multiple &lt;wd:Calculated_Field_Data&gt; nodes --&gt;
      &lt;/wd:root&gt;
  
  &lt;/Aggregated_Data&gt;
  </code></pre>
  <p>Expected output:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;Aggregated_Data&gt;
  
      &lt;References&gt;
          &lt;Reference&gt;
              &lt;Type&gt;Company_Reference_ID&lt;/Type&gt;
              &lt;Value&gt;Dest Company&lt;/Value&gt;
          &lt;/Reference&gt;
          &lt;Reference&gt;
              &lt;Type&gt;ABC_Reference_ID&lt;/Type&gt;
              &lt;Value&gt;XYZ&lt;/Value&gt;
          &lt;/Reference&gt;
      &lt;/References&gt;
  
  
      &lt;wd:root xmlns:wd=&quot;urn:com.workday/bsvc&quot;&gt;
          &lt;wd:Calculated_Field_Data xmlns:wd=&quot;urn:com.workday/bsvc&quot;
              xmlns:env=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;&gt;
              &lt;wd:Calculated_Field_Reference_ID&gt;Ref&lt;/wd:Calculated_Field_Reference_ID&gt;
              &lt;wd:Name&gt;NameABC&lt;/wd:Name&gt;
             
              &lt;wd:Conditional_Expression_Calculated_Field_Data&gt;
                  &lt;wd:Conditional_Expression_Calculated_Field_Condition_Data&gt;
                      &lt;wd:Condition_Item_Data&gt;
                          &lt;wd:Order&gt;dd&lt;/wd:Order&gt;
                          &lt;wd:And_Or_Reference&gt;
                              &lt;wd:ID wd:type=&quot;WID&quot;&gt;da4e1c34446c11de98360015c5e6daf6&lt;/wd:ID&gt;
                              &lt;wd:ID wd:type=&quot;And_Or_Operator_Name&quot;&gt;And&lt;/wd:ID&gt;
                          &lt;/wd:And_Or_Reference&gt; 
  
                          &lt;!-- Notice that after removing all wd:ID nodes except last, the value of last node was replaced from the value from References node of its type --&gt;
                          &lt;wd:Filter_Instances_Reference&gt;                                
                              &lt;wd:ID wd:type=&quot;Company_Reference_ID&quot;&gt;Dest Company&lt;/wd:ID&gt;
                          &lt;/wd:Filter_Instances_Reference&gt;                        
                         
                      &lt;/wd:Condition_Item_Data&gt;
                  &lt;/wd:Conditional_Expression_Calculated_Field_Condition_Data&gt;
              &lt;/wd:Conditional_Expression_Calculated_Field_Data&gt;
          &lt;/wd:Calculated_Field_Data&gt;
          
          &lt;!-- Has multiple &lt;wd:Calculated_Field_Data&gt; nodes --&gt;
      &lt;/wd:root&gt;
  
  &lt;/Aggregated_Data&gt;
  </code></pre>
  <p>To achieve this I have created the following XSLT:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;xsl:stylesheet version=&quot;3.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
      xmlns:wd=&quot;urn:com.workday/bsvc&quot;
      xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;
      xmlns:map=&quot;http://www.w3.org/2005/xpath-functions/map&quot;
      xmlns:env=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;
      exclude-result-prefixes=&quot;#all&quot;&gt;
      &lt;xsl:output method=&quot;xml&quot; indent=&quot;yes&quot; /&gt;
      &lt;xsl:strip-space elements=&quot;*&quot; /&gt;
      
      &lt;xsl:mode streamable=&quot;yes&quot; on-no-match=&quot;shallow-copy&quot; use-accumulators=&quot;#all&quot;/&gt;
      
       &lt;xsl:accumulator name=&quot;RefType&quot; as=&quot;xs:string?&quot; initial-value=&quot;()&quot; streamable=&quot;yes&quot;&gt;
          &lt;xsl:accumulator-rule match=&quot;References/Reference/Type/text()&quot;
              select=&quot;normalize-space()&quot;/&gt;
      &lt;/xsl:accumulator&gt;
  
  
      &lt;xsl:accumulator name=&quot;RefMap&quot; as=&quot;map(xs:string,xs:string)&quot; streamable=&quot;yes&quot;
          initial-value=&quot;map{}&quot;&gt;
          &lt;xsl:accumulator-rule match=&quot;References/Reference/Value/text()&quot;
              select=&quot;map:put($value, accumulator-before('RefType'), normalize-space())&quot;/&gt;
      &lt;/xsl:accumulator&gt;
      
      
      
     
      
      &lt;!-- removes all nodes except last one --&gt;
      &lt;xsl:template match=&quot;wd:Filter_Instances_Reference/wd:ID[position() != last()]&quot;/&gt;
      
      &lt;!-- replaces last node with value from accumulator from References node --&gt;
      &lt;xsl:template match=&quot;wd:Filter_Instances_Reference/wd:ID[position() = last()]&quot;&gt;
          &lt;xsl:variable name=&quot;type&quot; select=&quot;./@wd:type&quot;/&gt;
          &lt;wd:ID&gt;
              &lt;xsl:attribute name=&quot;wd:type&quot;&gt;&lt;xsl:value-of select=&quot;$type&quot;/&gt;&lt;/xsl:attribute&gt;
              
              &lt;!-- Accumulator map value goes here --&gt;
              &lt;xsl:value-of select=&quot;accumulator-before('RefMap')($type)&quot;/&gt;
          &lt;/wd:ID&gt;
      &lt;/xsl:template&gt;
      
     
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  <p><strong>THE ISSUE:</strong> Since my system uses lower version of Saxon (9.7 I believe), the above XSLT is throwing a streaming error:
  <em>Template rule is declared streamable but it does not satisfy the streamability rules: <strong>The match pattern is not motionless</strong></em></p>
  <p>I suspect the reason for this is I have used predicates in the match expression to identify position of wd:ID nodes in both the templates. However, when I execute the same XSLT in a Saxon processor with higher version (10.6), I get the expected output, linked <a href="https://xsltfiddle.liberty-development.net/aB9NL5/1" rel="nofollow noreferrer">here</a>.</p>
  <p><strong>I just wanted to know if there is any other way to achieve my requirement without using predicates to compare positions of the nodes or any other better way to achieve my requirement in lower XSLT 3.0 saxon environments. Any help is appreciated!</strong></p>