title: XSLT 2.0, templates and node/value sets
body: <p>XSLT newbie (so I apologize in advance)</p>
  
  <p>I have an XML file containing payment data. I am trying to transform it into an xml format used by a bank. The XML needs to be different if it's a non USD payment vs a USD payment. Multiple payments are possible in a given file
  here is a sample of the source XML (simplified, one transaction)</p>
  
  <pre><code>&lt;Payments&gt;
   &lt;Payment&gt;
    &lt;Payment_Detail&gt;
       &lt;Payment_Info&gt;
          &lt;ns:PAYMENT_Payment_Memo&gt;Payment Info&lt;/ns:PAYMENT_Payment_Memo&gt;
          &lt;ns:PAYMENT_Payment_Amount&gt;22222&lt;/ns:PAYMENT_Payment_Amount&gt;
          &lt;ns:PAYMENT_Currency&gt;US Dollars&lt;/ns:PAYMENT_Currency&gt;
          &lt;ns:PAYMENT_Currency_Code&gt;USD&lt;/ns:PAYMENT_Currency_Code&gt;
          &lt;ns:PAYMENT_Check_or_Advice_Number&gt;0&lt;/ns:PAYMENT_Check_or_Advice_Number&gt;
       &lt;/Payment_Info&gt;
       &lt;Payer_Data&gt;
          &lt;ns:PAYER_Company_Name&gt;Company, LLP&lt;/ns:PAYER_Company_Name&gt;
          &lt;ns:PAYER_Address_Line_1&gt;Main st&lt;/ns:PAYER_Address_Line_1&gt;
          &lt;ns:PAYER_Address_Line_2&gt;Line 2&lt;/ns:PAYER_Address_Line_2&gt;
          &lt;ns:PAYER_City&gt;Cleveland&lt;/ns:PAYER_City&gt;
          &lt;ns:PAYER_Zip_Code&gt;11111&lt;/ns:PAYER_Zip_Code&gt;
          &lt;ns:PAYER_Country&gt;US&lt;/ns:PAYER_Country&gt;
          &lt;ns:PAYER_Account_Number&gt;1111-11111111&lt;/ns:PAYER_Account_Number&gt;
          &lt;ns:PAYER_Currency&gt;USD&lt;/ns:PAYER_Currency&gt;
       &lt;/Payer_Data&gt;
       &lt;Payee_Data&gt;
          &lt;ns:PAYEE_Name&gt;Name&lt;/ns:PAYEE_Name&gt;
          &lt;ns:PAYEE_Bank_Name&gt;Bank Name&lt;/ns:PAYEE_Bank_Name&gt;
          &lt;ns:PAYEE_Bank_Account_Name&gt;Account Name&lt;/ns:PAYEE_Bank_Account_Name&gt;
          &lt;ns:PAYEE_Account_Number&gt;1111111&lt;/ns:PAYEE_Account_Number&gt;
          &lt;ns:PAYEE_Masked_Account_Number&gt;**********&lt;/ns:PAYEE_Masked_Account_Number&gt;
          &lt;ns:PAYEE_Account_Type&gt;DD&lt;/ns:PAYEE_Account_Type&gt;
          &lt;ns:PAYEE_Routing_Transit_Number&gt;22222&lt;/ns:PAYEE_Routing_Transit_Number&gt;
          &lt;ns:PAYEE_Bank_Country&gt;US&lt;/wpc:PAYEE_Bank_Country&gt;
       &lt;/Payee_Data&gt;
    &lt;/Payment_Detail&gt;
   &lt;/Payment&gt;
  &lt;/Payments&gt;   
  </code></pre>
  
  <p>my thought is/was to use a different template based on the currency. The problem I am running into is figuring out which template to call when there are multiple payments (a node set vs a single node)</p>
  
  <p>here is my XSL (also simplified)</p>
  
  <pre><code>&lt;xsl:variable name="paymentCurrency"
     select="/Payment/Payment_Detail/Payment_Info/ns:PAYMENT_Currency_Code"/&gt;
  
  
          &lt;xsl:choose&gt; 
             &lt;xsl:when test="$paymentCurrency = 'USD'"&gt;
  
                     &lt;xsl:call-template name="Payment-Domestic"/&gt; 
  
             &lt;/xsl:when&gt;    
             &lt;xsl:otherwise&gt;
  
                 &lt;xsl:call-template name="Payment-Foreign"/&gt;
  
             &lt;/xsl:otherwise&gt;
  
         &lt;/xsl:choose&gt;
  
  
           &lt;xsl:template name="Payment-Domestic"&gt;
             &lt;xsl:for-each select="/Payments/Payment"&gt;
                 &lt;paymentrec&gt;
                     &lt;recordType&gt;2&lt;/recordType&gt;
                     &lt;!-- insert other domestic payment fields --&gt;
              &lt;/xsl:for-each&gt;
          &lt;/xsl:template&gt;
  
           &lt;xsl:template name="Payment-Foreign"&gt;
             &lt;xsl:for-each select="/Payments/Payment"&gt;
                 &lt;paymentrec&gt;
                     &lt;recordType&gt;5&lt;/recordType&gt;
                     &lt;!-- insert other non US payment fields --&gt;
              &lt;/xsl:for-each&gt;
          &lt;/xsl:template&gt;
  </code></pre>
  
  <p>It works when there is  a single payment in the input file, but multiple entries set the variable to a node/value set and that will never match.</p>
  
  <p>what is the best way to call the different templates?
  should I not use the for-each? (treating XSLT like an imperative language isn't the smartest idea either, I know)</p>
  
  <p>Thanks</p>
  
  <hr>
  
  <p>EDIT to answer a comment / question
  XSLT 2.0, sample output (simplified below)</p>
  
  <p>(Foreign payment)</p>
  
  <pre><code>&lt;paymentrec&gt;
    &lt;recordType&gt;5&lt;/recordType&gt;
    &lt;payerAccNum&gt;1111-11111111&lt;/payerAccNum&gt;
    &lt;Currency/&gt;
    (other fields)
  &lt;/paymentrec&gt;
  </code></pre>
  
  <p>(USD payment)</p>
  
  <pre><code>&lt;paymentrec&gt;
    &lt;recordType&gt;2&lt;/recordType&gt;
    &lt;payerAccNum&gt;1111-11111111&lt;/payerAccNum&gt;
    &lt;payAmount/&gt;
    (other fields)
  &lt;/paymentrec&gt;
  </code></pre>
  
  <p>it also leads me to another question:
  If I have 3-4 fields to add as a header to domestic payments (But not foreign). I have a domestic-pay template but if I call it inside the "domestic" template, it duplicates the header. If I apply it when I call the "payment" template it appears in the foreign payment (Which I don't want) </p>