<?xml version="1.0" encoding="windows-1252"?>
<node id="994375" title="XPATH Looping" created="2012-09-18 18:17:58" updated="2012-09-18 18:17:58">
<type id="115">
perlquestion</type>
<author id="994365">
medirecpr</author>
<data>
<field name="doctext">
&lt;p&gt; Hello Monks!  Humbly asking for help!
I am trying to parse xml files with the following structure:
&lt;/p&gt; 

&lt;code&gt; 
&lt;?xml version="1.0"?&gt;
&lt;file&gt;
&lt;InterchangeLoop&gt;
        &lt;InterchangeControlHeader AuthorizationInformationQualifier1="00" AuthorizationInformation2="          " SecurityInformationQualifier3="00" SecurityInformation4="
   " InterchangeIDQualifier5="30" InterchangeSenderID6="660600000      " InterchangeIDQualifier7="30" InterchangeReceiverID8="660600001      " InterchangeDate9="120622" Intercha
ngeTime10="0355" InterchangeControlStandardsIdentifier11="U" InterchangeControlVersionNumber12="00401" InterchangeControlNumber13="000000251" AcknowledgmentRequested14="0" Usage
Indicator15="P" ComponentElementSeparator16=":"/&gt;
  &lt;FunctionalGroupLoop&gt;
        &lt;FunctionalGroupHeader FunctionalIdentifierCode1="HP" ApplicationSender_sCode2="660600000" ApplicationReceiver_sCode3="660600001" Date4="20120622" Time5="0355" GroupCont
rolNumber6="251" ResponsibleAgencyCode7="X" Version_Release_IndustryIdentifierCode8="004010X091A1"/&gt;
    &lt;Transaction835&gt;
        &lt;TransactionSetHeader TransactionSetIdentifierCode1="835" TransactionSetControlNumber2="0085"/&gt;
        &lt;FinancialInformation TransactionHandlingCode1="C" TotalActualProviderPaymentAmount2="619.54" CreditorDebitFlagCode3="C" PaymentMethodCode4="CHK" CheckIssueorEFTEffectiv
eDate16="20120619"/&gt;
        &lt;ReassociationTraceNumber TraceTypeCode1="1" CheckorEFTTraceNumber2="9049000" PayerIdentifier3="1660000000"/&gt;
        &lt;ReceiverIdentification ReferenceIdentificationQualifier1="EV" ReceiverIdentifier2="660600000"/&gt;
        &lt;ProductionDate DateTimeQualifier1="405" ProductionDate2="20120619"/&gt;
      &lt;PayerIdentificationLoop&gt;
        &lt;PayerIdentification EntityIdentifierCode1="PR" PayerName2="SOME CHOICE PAYER" IdentificationCodeQualifier3="XV" PayerIdentifier4="660000000"/&gt;
        &lt;PayerAddress PayerAddressLine1=" CARIBE"/&gt;
        &lt;PayerCity_State_ZIPCode PayerCityName1="SAN JUAN" PayerStateCode2="PR" PayerPostalZoneorZIPCode3="00926"/&gt;
      &lt;/PayerIdentificationLoop&gt;
      &lt;PayeeIdentificationLoop&gt;
        &lt;PayeeIdentification EntityIdentifierCode1="PE" PayeeName2="CORP. DE SALUD " IdentificationCodeQualifier3="XX" PayeeIdentificationCode4="1234000000"/&gt;
        &lt;PayeeAdditionalIdentification ReferenceIdentificationQualifier1="TJ" AdditionalPayeeIdentifier2="660600001"/&gt;
      &lt;/PayeeIdentificationLoop&gt;
      &lt;HeaderNumberLoop&gt;
        &lt;HeaderNumber AssignedNumber1="1"/&gt;
        &lt;ClaimPaymentInformationLoop&gt;
                &lt;ClaimPaymentInformation PatientControlNumber1="30290" ClaimStatusCode2="1" TotalClaimChargeAmount3="275.13" ClaimPaymentAmount4="241.28" ClaimFilingIndicatorCod
e6="13" PayerClaimControlNumber7="12220EEEEEEE"/&gt;
                &lt;PatientName EntityIdentifierCode1="QC" EntityTypeQualifier2="1" PatientLastName3="APELLIDO" PatientFirstName4="NAME" IdentificationCodeQualifier8="MI" PatientId
entifier9="030000000"/&gt;
                &lt;ServiceProviderName EntityIdentifierCode1="82" EntityTypeQualifier2="2" RenderingProviderLastorOrganizationName3="" IdentificationCodeQualifier8="XX" RenderingP
roviderIdentifier9="1234567890"/&gt;
          &lt;ServicePaymentInformationLoop&gt;
                &lt;ServicePaymentInformation LineItemChargeAmount2="275.13" LineItemProviderPaymentAmount3="241.28" UnitsofServicePaidCount5="1"&gt;
                        &lt;CompositeMedicalProcedureIdentifier1 Product_ServiceIDQualifier1="HC" Product_ServiceID2="99999" ProcedureModifier3="26"/&gt;
                &lt;/ServicePaymentInformation&gt;
                &lt;ServiceDate DateTimeQualifier1="472" ServiceDate2="20120614"/&gt;
                &lt;ServiceAdjustment ClaimAdjustmentGroupCode1="CO" AdjustmentReasonCode2="125" AdjustmentAmount3="15.69" AdjustmentReasonCode5="105" AdjustmentAmount6="18.16"/&gt;
                &lt;ServiceIdentification ReferenceIdentificationQualifier1="6R" ProviderIdentifier2="3999999"/&gt;
                &lt;HealthCareRemarkCodes CodeListQualifierCode1="HE" RemarkCode2="N381"/&gt;
      &lt;/ServicePaymentInformationLoop&gt;
      &lt;/ClaimPaymentInformationLoop&gt;
      &lt;/HeaderNumberLoop&gt;
      &lt;HeaderNumberLoop&gt;
        &lt;HeaderNumber AssignedNumber1="2"/&gt;
        &lt;ClaimPaymentInformationLoop&gt;
                &lt;ClaimPaymentInformation PatientControlNumber1="30289" ClaimStatusCode2="1" TotalClaimChargeAmount3="99.32" ClaimPaymentAmount4="92.37" ClaimFilingIndicatorCode6
="13" PayerClaimControlNumber7="12220EEEEEEE"/&gt;
                &lt;PatientName EntityIdentifierCode1="QC" EntityTypeQualifier2="1" PatientLastName3="APELLIDO" PatientFirstName4="NAME" IdentificationCodeQualifier8="MI" PatientId
entifier9="030000000"/&gt;
                &lt;ServiceProviderName EntityIdentifierCode1="82" EntityTypeQualifier2="2" RenderingProviderLastorOrganizationName3="" IdentificationCodeQualifier8="XX" RenderingP
roviderIdentifier9="1234567890"/&gt;
          &lt;ServicePaymentInformationLoop&gt;
                &lt;ServicePaymentInformation LineItemChargeAmount2="84.83" LineItemProviderPaymentAmount3="78.89" UnitsofServicePaidCount5="1"&gt;
                        &lt;CompositeMedicalProcedureIdentifier1 Product_ServiceIDQualifier1="HC" Product_ServiceID2="99203" ProcedureModifier3="25"/&gt;
                &lt;/ServicePaymentInformation&gt;
                &lt;ServiceDate DateTimeQualifier1="472" ServiceDate2="20120611"/&gt;
                &lt;ServiceAdjustment ClaimAdjustmentGroupCode1="CO" AdjustmentReasonCode2="105" AdjustmentAmount3="5.94"/&gt;
                &lt;ServiceIdentification ReferenceIdentificationQualifier1="6R" ProviderIdentifier2="3028901"/&gt;
          &lt;/ServicePaymentInformationLoop&gt;
          &lt;ServicePaymentInformationLoop&gt;
                &lt;ServicePaymentInformation LineItemChargeAmount2="14.49" LineItemProviderPaymentAmount3="13.48" UnitsofServicePaidCount5="1"&gt;
                        &lt;CompositeMedicalProcedureIdentifier1 Product_ServiceIDQualifier1="HC" Product_ServiceID2="93000"/&gt;
                &lt;/ServicePaymentInformation&gt;
                &lt;ServiceDate DateTimeQualifier1="472" ServiceDate2="20120611"/&gt;
                &lt;ServiceAdjustment ClaimAdjustmentGroupCode1="CO" AdjustmentReasonCode2="105" AdjustmentAmount3="1.01"/&gt;
                &lt;ServiceIdentification ReferenceIdentificationQualifier1="6R" ProviderIdentifier2="3099999"/&gt;
      &lt;/ServicePaymentInformationLoop&gt;
      &lt;/ClaimPaymentInformationLoop&gt;
      &lt;/HeaderNumberLoop&gt;
      &lt;HeaderNumberLoop&gt;
        &lt;HeaderNumber AssignedNumber1="3"/&gt;
        &lt;ClaimPaymentInformationLoop&gt;
                &lt;ClaimPaymentInformation PatientControlNumber1="30293" ClaimStatusCode2="1" TotalClaimChargeAmount3="185.88" ClaimPaymentAmount4="165.57" ClaimFilingIndicatorCod
e6="13" PayerClaimControlNumber7="12220EEEEEEE"/&gt;
                &lt;PatientName EntityIdentifierCode1="QC" EntityTypeQualifier2="1" PatientLastName3="APELLIDO" PatientFirstName4="NAME" IdentificationCodeQualifier8="MI" PatientId
entifier9="030000000"/&gt;
                &lt;ServiceProviderName EntityIdentifierCode1="82" EntityTypeQualifier2="2" RenderingProviderLastorOrganizationName3="" IdentificationCodeQualifier8="XX" RenderingP
roviderIdentifier9="1234567890"/&gt;
          &lt;ServicePaymentInformationLoop&gt;
                &lt;ServicePaymentInformation LineItemChargeAmount2="178.39" LineItemProviderPaymentAmount3="158.6" UnitsofServicePaidCount5="1"&gt;
                        &lt;CompositeMedicalProcedureIdentifier1 Product_ServiceIDQualifier1="HC" Product_ServiceID2="99223"/&gt;
                &lt;/ServicePaymentInformation&gt;
                &lt;ServiceDate DateTimeQualifier1="472" ServiceDate2="20120613"/&gt;
                &lt;ServiceAdjustment ClaimAdjustmentGroupCode1="CO" AdjustmentReasonCode2="125" AdjustmentAmount3="7.85" AdjustmentReasonCode5="105" AdjustmentAmount6="11.94"/&gt;
                &lt;ServiceIdentification ReferenceIdentificationQualifier1="6R" ProviderIdentifier2="3029999"/&gt;
                &lt;HealthCareRemarkCodes CodeListQualifierCode1="HE" RemarkCode2="N381"/&gt;
          &lt;/ServicePaymentInformationLoop&gt;
          &lt;ServicePaymentInformationLoop&gt;
                &lt;ServicePaymentInformation LineItemChargeAmount2="7.49" LineItemProviderPaymentAmount3="6.97" UnitsofServicePaidCount5="1"&gt;
                        &lt;CompositeMedicalProcedureIdentifier1 Product_ServiceIDQualifier1="HC" Product_ServiceID2="93010"/&gt;
                &lt;/ServicePaymentInformation&gt;
                &lt;ServiceDate DateTimeQualifier1="472" ServiceDate2="20120613"/&gt;
                &lt;ServiceAdjustment ClaimAdjustmentGroupCode1="CO" AdjustmentReasonCode2="105" AdjustmentAmount3=".52"/&gt;
                &lt;ServiceIdentification ReferenceIdentificationQualifier1="6R" ProviderIdentifier2="3029999"/&gt;
      &lt;/ServicePaymentInformationLoop&gt;
      &lt;/ClaimPaymentInformationLoop&gt;
      &lt;/HeaderNumberLoop&gt;
      &lt;HeaderNumberLoop&gt;
        &lt;HeaderNumber AssignedNumber1="4"/&gt;
        &lt;ClaimPaymentInformationLoop&gt;
                &lt;ClaimPaymentInformation PatientControlNumber1="39999" ClaimStatusCode2="1" TotalClaimChargeAmount3="71.87" ClaimPaymentAmount4="66.84" ClaimFilingIndicatorCode6
="13" PayerClaimControlNumber7="12170EAAAAA"/&gt;
                &lt;PatientName EntityIdentifierCode1="QC" EntityTypeQualifier2="1" PatientLastName3="LAST LASTS" PatientFirstName4="FIRSTSS" IdentificationCodeQualifier8="MI" Pati
entIdentifier9="031231234"/&gt;
                &lt;ServiceProviderName EntityIdentifierCode1="82" EntityTypeQualifier2="2" RenderingProviderLastorOrganizationName3="" IdentificationCodeQualifier8="XX" RenderingP
roviderIdentifier9="1234567890"/&gt;
          &lt;ServicePaymentInformationLoop&gt;
                &lt;ServicePaymentInformation LineItemChargeAmount2="57.38" LineItemProviderPaymentAmount3="53.36" UnitsofServicePaidCount5="1"&gt;
                        &lt;CompositeMedicalProcedureIdentifier1 Product_ServiceIDQualifier1="HC" Product_ServiceID2="99213" ProcedureModifier3="25"/&gt;
                &lt;/ServicePaymentInformation&gt;
                &lt;ServiceDate DateTimeQualifier1="472" ServiceDate2="20120611"/&gt;
                &lt;ServiceAdjustment ClaimAdjustmentGroupCode1="CO" AdjustmentReasonCode2="105" AdjustmentAmount3="4.02"/&gt;
                &lt;ServiceIdentification ReferenceIdentificationQualifier1="6R" ProviderIdentifier2="3999901"/&gt;
          &lt;/ServicePaymentInformationLoop&gt;
          &lt;ServicePaymentInformationLoop&gt;
                &lt;ServicePaymentInformation LineItemChargeAmount2="14.49" LineItemProviderPaymentAmount3="13.48" UnitsofServicePaidCount5="1"&gt;
                        &lt;CompositeMedicalProcedureIdentifier1 Product_ServiceIDQualifier1="HC" Product_ServiceID2="93000"/&gt;
                &lt;/ServicePaymentInformation&gt;
                &lt;ServiceDate DateTimeQualifier1="472" ServiceDate2="20120611"/&gt;
                &lt;ServiceAdjustment ClaimAdjustmentGroupCode1="CO" AdjustmentReasonCode2="105" AdjustmentAmount3="1.01"/&gt;
                &lt;ServiceIdentification ReferenceIdentificationQualifier1="6R" ProviderIdentifier2="3999902"/&gt;
      &lt;/ServicePaymentInformationLoop&gt;
      &lt;/ClaimPaymentInformationLoop&gt;
      &lt;/HeaderNumberLoop&gt;
      &lt;HeaderNumberLoop&gt;
        &lt;HeaderNumber AssignedNumber1="5"/&gt;
        &lt;ClaimPaymentInformationLoop&gt;
                &lt;ClaimPaymentInformation PatientControlNumber1="36666" ClaimStatusCode2="1" TotalClaimChargeAmount3="57.51" ClaimPaymentAmount4="53.48" ClaimFilingIndicatorCode6
="13" PayerClaimControlNumber7="12220ETTTTTT"/&gt;
                &lt;PatientName EntityIdentifierCode1="QC" EntityTypeQualifier2="1" PatientLastName3="LAST 1234" PatientFirstName4="First 1234" IdentificationCodeQualifier8="MI" Pa
tientIdentifier9="031231289"/&gt;
                &lt;ServiceProviderName EntityIdentifierCode1="82" EntityTypeQualifier2="2" RenderingProviderLastorOrganizationName3="" IdentificationCodeQualifier8="XX" RenderingP
roviderIdentifier9="1234567890"/&gt;
          &lt;ServicePaymentInformationLoop&gt;
                &lt;ServicePaymentInformation LineItemChargeAmount2="57.51" LineItemProviderPaymentAmount3="53.48" UnitsofServicePaidCount5="1"&gt;
                        &lt;CompositeMedicalProcedureIdentifier1 Product_ServiceIDQualifier1="HC" Product_ServiceID2="91111" ProcedureModifier3="26"/&gt;
                &lt;/ServicePaymentInformation&gt;
                &lt;ServiceDate DateTimeQualifier1="472" ServiceDate2="20120613"/&gt;
                &lt;ServiceAdjustment ClaimAdjustmentGroupCode1="CO" AdjustmentReasonCode2="105" AdjustmentAmount3="4.03"/&gt;
                &lt;ServiceIdentification ReferenceIdentificationQualifier1="6R" ProviderIdentifier2="3666601"/&gt;
    &lt;/ServicePaymentInformationLoop&gt;
    &lt;/ClaimPaymentInformationLoop&gt;
    &lt;/HeaderNumberLoop&gt;
        &lt;TransactionSetTrailer TransactionSegmentCount1="65" TransactionSetControlNumber2="0085"/&gt;
  &lt;/Transaction835&gt;
        &lt;FunctionalGroupTrailer NumberofTransactionSetsIncluded1="1" GroupControlNumber2="251"/&gt;
&lt;/FunctionalGroupLoop&gt;
        &lt;InterchangeControlTrailer NumberofIncludedFunctionalGroups1="1" InterchangeControlNumber2="000000251"/&gt;
&lt;/InterchangeLoop&gt;
&lt;/file&gt;

&lt;/code&gt;

&lt;p&gt; 
Using the following code:&lt;/p&gt; 

&lt;code&gt;
          #!/usr/bin/perl -w
use strict;
use warnings;
use XML::XPath;
use MIME::Lite::TT;
use File::Copy;
#use Time::localTime;

my $file = 'test.xml';
my $xp = XML::XPath-&gt;new(filename =&gt; $file);
my $icncount = 1;
my $transactioncount = 1;
my $claimpaymentcount = 1;
my $servicepaymentcount = 1;
my $tm = (localtime);

foreach my $InterchangeControlHeader ($xp-&gt;find('//InterchangeControlHeader')-&gt;get_nodelist){
    print '=================================================================' . "\n";
    print 'Batch X12 File: ' . $file . "\n";
    print 'Reading Date: ' . $tm . "\n";
    print 'User Id: Generated by MediClear' . "\n";
    print 'Last Modified: N/A   ' . "\n";
    print '=================================================================' . "\n";
#   print 'Functional Group Count: ' . $icncount . "\n";
#   print 'ICN: ' . $InterchangeControlHeader-&gt;find('//@InterchangeControlNumber13')-&gt;string_value . "\n";

    foreach my $Transaction835 ($xp-&gt;find('//Transaction835')-&gt;get_nodelist){
#       print $transactioncount . ') ' . 'Transaction Set Control Number : ' . $Transaction835-&gt;find('TransactionSetHeader/@TransactionSetControlNumber2')-&gt;string_value . "\n";
            print 'Check/EFT Amount: ' . $Transaction835-&gt;find('FinancialInformation/@TotalActualProviderPaymentAmount2')-&gt;string_value . "\n";
            print 'Check/EFT Credit/Debit Type: ' . $Transaction835-&gt;find('FinancialInformation/@CreditorDebitFlagCode3')-&gt;string_value . "\n";
            print 'Check/EFT Type: ' . $Transaction835-&gt;find('FinancialInformation/@PaymentMethodCode4')-&gt;string_value . "\n";
            print 'Check/EFT Payment Date: ' . $Transaction835-&gt;find('FinancialInformation/@CheckIssueorEFTEffectiveDate16')-&gt;string_value . "\n";
            print 'Check/EFT Number: ' . $Transaction835-&gt;find('ReassociationTraceNumber/@CheckorEFTTraceNumber2')-&gt;string_value . "\n";
            print 'Production Date: ' . $Transaction835-&gt;find('ProductionDate/@ProductionDate2')-&gt;string_value . "\n";
            print 'Payer Name: ' . $Transaction835-&gt;find('//PayerIdentificationLoop/PayerIdentification/@PayerName2')-&gt;string_value . "\n";
            print 'Payer Address: ' . $Transaction835-&gt;find('//PayerIdentificationLoop/PayerAddress/@PayerAddressLine1')-&gt;string_value . " ";
            print $Transaction835-&gt;find('//PayerIdentificationLoop/PayerCity_State_ZIPCode/@PayerCityName1')-&gt;string_value . " ";
            print $Transaction835-&gt;find('//PayerIdentificationLoop/PayerCity_State_ZIPCode/@PayerStateCode2')-&gt;string_value . " ";
            print $Transaction835-&gt;find('//PayerIdentificationLoop/PayerCity_State_ZIPCode/@PayerPostalZoneorZIPCode3')-&gt;string_value . "\n";
            print 'Payee Name: ' . $Transaction835-&gt;find('//PayeeIdentificationLoop/PayeeIdentification/@PayeeName2')-&gt;string_value . "\n";
            print 'Payee ICN: ' . $Transaction835-&gt;find('//PayeeIdentificationLoop/PayeeIdentification/@PayeeIdentificationCode4')-&gt;string_value . "\n";

            foreach my $ClaimPaymentInformationLoop ($xp-&gt;find('//ClaimPaymentInformationLoop')-&gt;get_nodelist){
                print "\n";
                print 'Invoice: ' . $ClaimPaymentInformationLoop-&gt;find('ClaimPaymentInformation/@PatientControlNumber1')-&gt;string_value . "\n";
                print '------------------------------------' . "\n";
                print 'Claim Status: ' . $ClaimPaymentInformationLoop-&gt;find('ClaimPaymentInformation/@ClaimStatusCode2')-&gt;string_value . "\n";
                print 'Billed Amount: ' . $ClaimPaymentInformationLoop-&gt;find('ClaimPaymentInformation/@TotalClaimChargeAmount3')-&gt;string_value . "\n";
                print 'Insurance Payment Amount: ' . $ClaimPaymentInformationLoop-&gt;find('ClaimPaymentInformation/@ClaimPaymentAmount4')-&gt;string_value . "\n";
                print 'Deductible Amount: ? ' . "\n";
                print 'Payer ICN: ' . $ClaimPaymentInformationLoop-&gt;find('ClaimPaymentInformation/@PayerClaimControlNumber7')-&gt;string_value . "\n";
                print 'Name Patient: ' . $ClaimPaymentInformationLoop-&gt;find('PatientName/@PatientFirstName4')-&gt;string_value . " ";
                print $ClaimPaymentInformationLoop-&gt;find('PatientName/@PatientLastName3')-&gt;string_value . "\n";
$claimpaymentcount++;

                foreach my $ServicePaymentInformationLoop ($xp-&gt;find('//ServicePaymentInformation')-&gt;get_nodelist){
                    print 'CPT Code: ' . $ServicePaymentInformationLoop-&gt;find('CompositeMedicalProcedureIdentifier1/@Product_ServiceID2')-&gt;string_value . "\n";
                    print 'CPT Modifier: ' . $ServicePaymentInformationLoop-&gt;find('CompositeMedicalProcedureIdentifier1/@ProcedureModifier3')-&gt;string_value . "\n";
                    print 'CPT Billed Amount: ' . $ServicePaymentInformationLoop-&gt;find('@LineItemChargeAmount2')-&gt;string_value . "\n";
                    print 'CPT Allow Amount: ' . $ServicePaymentInformationLoop-&gt;find('@LineItemProviderPaymentAmount3')-&gt;string_value . "\n";
                    print 'Service Payment Count: ' . $servicepaymentcount . "\n";
                    $servicepaymentcount++;
                }
            $transactioncount++;
            }
    $icncount++;
    }
}

&lt;/code&gt;
&lt;p&gt;
The script works fine in working through each isntance of the "ClaimPaymentInformationLoop" node, but when it gets to the "ServicePaymentInformationLoop" node, the result is that it prints out all the "ServicePaymentInformationLoop" elements in the XML for each of the "ClaimPaymentInformationLoop" nodes.

The desired result is that the elements in the ClaimLoop are printed, followed by the corresponding elements in the ServiceLoop.
&lt;/p&gt;
&lt;code&gt;
Claim Info1
  Corresponding Service Loop Info1
Claim Info2
  Corresponding Service Loop Info2
&lt;/code&gt;

&lt;p&gt; Kindest Regards, &lt;/p&gt;

&lt;p&gt; Per previous posts, I have included the version of the script that is working correctly, per Choroba's input:&lt;/p&gt;

&lt;code&gt;
#!/usr/bin/perl -w
use strict;
use warnings;
use XML::XPath;
use MIME::Lite::TT;
use File::Copy;
#use Time::localTime;

my $file = 'test.xml';
my $xp = XML::XPath-&gt;new(filename =&gt; $file);
my $title='835 REPORT';
my $to='jarel@medirecpr.COM, jafet.medirec@gmail.com';
my $from='mediclear@MEDIRECPR.COM';
my $subject='MediClear - 835 REPORT EMAIL MESSAGE';

my $icncount = 1;
my $transactioncount = 1;
my $claimpaymentcount = 1;
#my $servicepaymentcount = 1;
my $tm = (localtime);
my $FileName = "$file\_835_Report.LOG";
my $LogFiles='/home/jramirez/PayerDownload/LogFiles/';

open(FILEHANDLE, "&gt;$FileName") or die "cannot open file for reading: $!";

foreach my $InterchangeControlHeader ($xp-&gt;find('//InterchangeControlHeader')-&gt;get_nodelist){
        print FILEHANDLE '=================================================================' . "\n";
        print FILEHANDLE 'Batch X12 File: ' . $file . "\n";
        print FILEHANDLE 'Reading Date: ' . $tm . "\n";
        print FILEHANDLE 'User Id: Generated by MediClear' . "\n";
        print FILEHANDLE 'Last Modified: N/A    ' . "\n";
    print FILEHANDLE '=================================================================' . "\n";
#       print 'Functional Group Count: ' . $icncount . "\n";
#       print 'ICN: ' . $InterchangeControlHeader-&gt;find('//@InterchangeControlNumber13')-&gt;string_value . "\n";

        foreach my $Transaction835 ($xp-&gt;find('//Transaction835')-&gt;get_nodelist){
#               print $transactioncount . ') ' . 'Transaction Set Control Number : ' . $Transaction835-&gt;find('TransactionSetHeader/@TransactionSetControlNumber2')-&gt;string_value
. "\n";
                        print FILEHANDLE 'Check/EFT Amount: ' . $Transaction835-&gt;find('FinancialInformation/@TotalActualProviderPaymentAmount2')-&gt;string_value . "\n";
                print FILEHANDLE 'Check/EFT Credit/Debit Type: ' . $Transaction835-&gt;find('FinancialInformation/@CreditorDebitFlagCode3')-&gt;string_value . "\n";
                        print FILEHANDLE 'Check/EFT Type: ' . $Transaction835-&gt;find('FinancialInformation/@PaymentMethodCode4')-&gt;string_value . "\n";
                print FILEHANDLE 'Check/EFT Payment Date: ' . $Transaction835-&gt;find('FinancialInformation/@CheckIssueorEFTEffectiveDate16')-&gt;string_value . "\n";
                print FILEHANDLE 'Check/EFT Number: ' . $Transaction835-&gt;find('ReassociationTraceNumber/@CheckorEFTTraceNumber2')-&gt;string_value . "\n";
                print FILEHANDLE 'Production Date: ' . $Transaction835-&gt;find('ProductionDate/@ProductionDate2')-&gt;string_value . "\n";
                print FILEHANDLE 'Payer Name: ' . $Transaction835-&gt;find('//PayerIdentificationLoop/PayerIdentification/@PayerName2')-&gt;string_value . "\n";
                print FILEHANDLE 'Payer Address: ' . $Transaction835-&gt;find('//PayerIdentificationLoop/PayerAddress/@PayerAddressLine1')-&gt;string_value . " ";
                print FILEHANDLE $Transaction835-&gt;find('//PayerIdentificationLoop/PayerCity_State_ZIPCode/@PayerCityName1')-&gt;string_value . " ";
                print FILEHANDLE $Transaction835-&gt;find('//PayerIdentificationLoop/PayerCity_State_ZIPCode/@PayerStateCode2')-&gt;string_value . " ";
                    print FILEHANDLE $Transaction835-&gt;find('//PayerIdentificationLoop/PayerCity_State_ZIPCode/@PayerPostalZoneorZIPCode3')-&gt;string_value . "\n";
                        print FILEHANDLE 'Payee Name: ' . $Transaction835-&gt;find('//PayeeIdentificationLoop/PayeeIdentification/@PayeeName2')-&gt;string_value . "\n";
                print FILEHANDLE 'Payee ICN: ' . $Transaction835-&gt;find('//PayeeIdentificationLoop/PayeeIdentification/@PayeeIdentificationCode4')-&gt;string_value . "\n";
                        print FILEHANDLE 'Provider Adjustment: ' . $Transaction835-&gt;find('//ProviderAdjustment/@ProviderAdjustmentAmount4')-&gt;string_value . "\n";
                        print FILEHANDLE 'Provider Adjustment Reason Code: ' . $Transaction835-&gt;find('//ProviderAdjustment/AdjustmentIdentifier3/@AdjustmentReasonCode1')-&gt;string
_value . "\n";

                        foreach my $ClaimPaymentInformationLoop ($xp-&gt;find('.//ClaimPaymentInformationLoop', $Transaction835)-&gt;get_nodelist){
                                print FILEHANDLE "\n";
                                print FILEHANDLE 'Invoice: ' . $ClaimPaymentInformationLoop-&gt;find('ClaimPaymentInformation/@PatientControlNumber1')-&gt;string_value . "\n";
                                print FILEHANDLE '------------------------------------' . "\n";
                                print FILEHANDLE 'Claim Status: ' . $ClaimPaymentInformationLoop-&gt;find('ClaimPaymentInformation/@ClaimStatusCode2')-&gt;string_value . "\n";
                print FILEHANDLE 'Billed Amount: ' . $ClaimPaymentInformationLoop-&gt;find('ClaimPaymentInformation/@TotalClaimChargeAmount3')-&gt;string_value . "\n";
                print FILEHANDLE 'Insurance Payment Amount: ' . $ClaimPaymentInformationLoop-&gt;find('ClaimPaymentInformation/@ClaimPaymentAmount4')-&gt;string_value . "\n";
                print FILEHANDLE 'Deductible Amount: ? ' . "\n";
                print FILEHANDLE 'Payer ICN: ' . $ClaimPaymentInformationLoop-&gt;find('ClaimPaymentInformation/@PayerClaimControlNumber7')-&gt;string_value . "\n";
                print FILEHANDLE 'Name Patient: ' . $ClaimPaymentInformationLoop-&gt;find('PatientName/@PatientFirstName4')-&gt;string_value . " ";
                                print FILEHANDLE $ClaimPaymentInformationLoop-&gt;find('PatientName/@PatientLastName3')-&gt;string_value . "\n";
                #$claimpaymentcount++;

                                my $MOARemarkCode1 = $ClaimPaymentInformationLoop-&gt;find('OutpatientAdjudicationInformation/@RemarkCode1')-&gt;string_value;
                my $MOARemarkCode2 = $ClaimPaymentInformationLoop-&gt;find('OutpatientAdjudicationInformation/@RemarkCode2')-&gt;string_value;
                my $MOARemarkCode3 = $ClaimPaymentInformationLoop-&gt;find('OutpatientAdjudicationInformation/@RemarkCode3')-&gt;string_value;
                my $MOARemarkCode4 = $ClaimPaymentInformationLoop-&gt;find('OutpatientAdjudicationInformation/@RemarkCode4')-&gt;string_value;
                my $MOARemarkCode5 = $ClaimPaymentInformationLoop-&gt;find('OutpatientAdjudicationInformation/@RemarkCode5')-&gt;string_value;

                                if ($MOARemarkCode1 ne '') {
                                        print FILEHANDLE 'MOA01: ' . $MOARemarkCode1 . "\n";
                                }
                                if ($MOARemarkCode2 ne '') {
                                        print FILEHANDLE 'MOA02: ' . $MOARemarkCode2 . "\n";
                                }
                                if ($MOARemarkCode3 ne '') {
                                        print FILEHANDLE 'MOA03: ' . $MOARemarkCode3 . "\n";
                                }
                                if ($MOARemarkCode4 ne '') {
                                        print FILEHANDLE 'MOA04: ' . $MOARemarkCode4 . "\n";
                                }
                                if ($MOARemarkCode5 ne '') {
                                        print FILEHANDLE 'MOA05: ' . $MOARemarkCode5 . "\n";
                                }

                my $CoveredDaysorVisitsCount1 = $ClaimPaymentInformationLoop-&gt;find('InpatientAdjudicationInformation/@CoveredDaysorVisitsCount1')-&gt;string_value;
                my $PPSOperatingOutlierAmount2 = $ClaimPaymentInformationLoop-&gt;find('InpatientAdjudicationInformation/@PPSOperatingOutlierAmount2')-&gt;string_value;
                my $LifetimePsychiatricDaysCount3 = $ClaimPaymentInformationLoop-&gt;find('InpatientAdjudicationInformation/@LifetimePsychiatricDaysCount3')-&gt;string_value;
                my $ClaimDRGAmount4 = $ClaimPaymentInformationLoop-&gt;find('InpatientAdjudicationInformation/@ClaimDRGAmount4')-&gt;string_value;
                my $RemarkCode5 = $ClaimPaymentInformationLoop-&gt;find('InpatientAdjudicationInformation/@RemarkCode5')-&gt;string_value;
                                my $ClaimDisproportionateShareAmount6 = $ClaimPaymentInformationLoop-&gt;find('InpatientAdjudicationInformation/@ClaimDisproportionateShareAmount6')
-&gt;string_value;
                                my $ClaimMSPPass_throughAmount7 = $ClaimPaymentInformationLoop-&gt;find('InpatientAdjudicationInformation/@ClaimMSPPass_throughAmount7')-&gt;string_val
ue;
                                my $ClaimPPSCapitalAmount8 = $ClaimPaymentInformationLoop-&gt;find('InpatientAdjudicationInformation/@ClaimPPSCapitalAmount8')-&gt;string_value;
                                my $PPS_CapitalFSPDRGAmount9 = $ClaimPaymentInformationLoop-&gt;find('InpatientAdjudicationInformation/@PPS_CapitalFSPDRGAmount')-&gt;string_value;
                                my $PPS_CapitalHSPDRGAmount10 = $ClaimPaymentInformationLoop-&gt;find('InpatientAdjudicationInformation/@PPS_CapitalHSPDRGAmount10')-&gt;string_value;
                                my $PPS_CapitalDSHDRGAmount11 = $ClaimPaymentInformationLoop-&gt;find('InpatientAdjudicationInformation/@PPS_CapitalDSHDRGAmount11')-&gt;string_value;
                                my $OldCapitalAmount12 = $ClaimPaymentInformationLoop-&gt;find('InpatientAdjudicationInformation/@OldCapitalAmount12')-&gt;string_value;
                                my $PPS_CapitalIMEamount13 = $ClaimPaymentInformationLoop-&gt;find('InpatientAdjudicationInformation/@PPS_CapitalIMEamount13')-&gt;string_value;
                                my $PPS_OperatingHospitalSpecificDRGAmount14 = $ClaimPaymentInformationLoop-&gt;find('InpatientAdjudicationInformation/@PPS_OperatingHospitalSpecifi
cDRGAmount14')-&gt;string_value;
                                my $CostReportDayCount15 = $ClaimPaymentInformationLoop-&gt;find('InpatientAdjudicationInformation/@CostReportDayCount15')-&gt;string_value;
                                my $PPS_OperatingFederalSpecificDRGAmount16 = $ClaimPaymentInformationLoop-&gt;find('InpatientAdjudicationInformation/@PPS_OperatingFederalSpecificD
RGAmount16')-&gt;string_value;
                                my $ClaimPPSCapitalOutlierAmount17 = $ClaimPaymentInformationLoop-&gt;find('InpatientAdjudicationInformation/@ClaimPPSCapitalOutlierAmount17')-&gt;stri
ng_value;
                                my $ClaimIndirectTeachingAmount18 = $ClaimPaymentInformationLoop-&gt;find('InpatientAdjudicationInformation/@ClaimIndirectTeachingAmount18')-&gt;string
_value;
                                my $NonpayableProfessionalComponentAmount19 = $ClaimPaymentInformationLoop-&gt;find('InpatientAdjudicationInformation/@NonpayableProfessionalCompone
ntAmount19')-&gt;string_value;
                                my $RemarkCode20 = $ClaimPaymentInformationLoop-&gt;find('InpatientAdjudicationInformation/@RemarkCode20')-&gt;string_value;
                                my $RemarkCode21 = $ClaimPaymentInformationLoop-&gt;find('InpatientAdjudicationInformation/@RemarkCode21')-&gt;string_value;
                                my $RemarkCode22 = $ClaimPaymentInformationLoop-&gt;find('InpatientAdjudicationInformation/@RemarkCode22')-&gt;string_value;
                                my $RemarkCode23 = $ClaimPaymentInformationLoop-&gt;find('InpatientAdjudicationInformation/@RemarkCode23')-&gt;string_value;
                                my $PPS_CapitalExceptionAmount24 = $ClaimPaymentInformationLoop-&gt;find('InpatientAdjudicationInformation/@PPS_CapitalExceptionAmount24')-&gt;string_v
alue;

                                if ($CoveredDaysorVisitsCount1 ne '') {
                    print FILEHANDLE 'MOA01: ' . $CoveredDaysorVisitsCount1 . "\n";
                }
                if ($PPSOperatingOutlierAmount2 ne '') {
                    print FILEHANDLE 'MOA02: ' . $PPSOperatingOutlierAmount2 . "\n";
                }
                if ($LifetimePsychiatricDaysCount3 ne '') {
                    print FILEHANDLE 'MIA03: ' . $LifetimePsychiatricDaysCount3 . "\n";
                }
                if ($ClaimDRGAmount4 ne '') {
                        print FILEHANDLE 'MIA04: ' . $ClaimDRGAmount4 . "\n";
                    }
                    if ($RemarkCode5 ne '') {
                        print FILEHANDLE 'MIA05: ' . $RemarkCode5 . "\n";
                    }
                                if ($ClaimDisproportionateShareAmount6 ne '') {
                                        print FILEHANDLE 'MIA06: ' . $ClaimDisproportionateShareAmount6 . "\n";
                                }
                                if ($ClaimMSPPass_throughAmount7 ne '') {
                                        print FILEHANDLE 'MIA07: ' . $ClaimMSPPass_throughAmount7 . "\n";
                                }
                                if ($ClaimPPSCapitalAmount8 ne '') {
                                        print FILEHANDLE 'MIA08: ' . $ClaimPPSCapitalAmount8 . "\n";
                                }
                                if ($PPS_CapitalFSPDRGAmount9 ne '') {
                                        print FILEHANDLE 'MIA09: ' . $PPS_CapitalFSPDRGAmount9 . "\n";
                                }
                                if ($PPS_CapitalHSPDRGAmount10 ne '') {
                                        print FILEHANDLE 'MIA10: ' . $PPS_CapitalHSPDRGAmount10 . "\n";
                                }
                                if ($PPS_CapitalDSHDRGAmount11 ne '') {
                                        print FILEHANDLE 'MIA11: ' . $PPS_CapitalDSHDRGAmount11 . "\n";
                                }
                                if ($OldCapitalAmount12 ne '') {
                        print FILEHANDLE 'MIA12: ' . $OldCapitalAmount12 . "\n";
                                }
                if ($PPS_CapitalIMEamount13 ne '') {
                    print FILEHANDLE 'MIA:13 ' . $PPS_CapitalIMEamount13 . "\n";
                }
                if ($PPS_OperatingHospitalSpecificDRGAmount14 ne '') {
                    print FILEHANDLE 'MIA14: ' . $PPS_OperatingHospitalSpecificDRGAmount14 . "\n";
                }
                                if ($CostReportDayCount15 ne '') {
                                    print FILEHANDLE 'MIA15: ' . $CostReportDayCount15 . "\n";
                                }
                if ($PPS_OperatingFederalSpecificDRGAmount16 ne '') {
                    print FILEHANDLE 'MIA16: ' . $PPS_OperatingFederalSpecificDRGAmount16 . "\n";
                }
                if ($ClaimPPSCapitalOutlierAmount17 ne '') {
                    print FILEHANDLE 'MIA17: ' . $ClaimPPSCapitalOutlierAmount17 . "\n";
                }
                if ($ClaimIndirectTeachingAmount18 ne '') {
                    print FILEHANDLE 'MIA18: ' . $ClaimIndirectTeachingAmount18 . "\n";
                }
                                if ($NonpayableProfessionalComponentAmount19 ne '') {
                    print FILEHANDLE 'MIA19: ' . $NonpayableProfessionalComponentAmount19 . "\n";
                }
                if ($RemarkCode20 ne '') {
                        print FILEHANDLE 'MIA20: ' . $RemarkCode20 . "\n";
                    }
                if ($RemarkCode21 ne '') {
                    print FILEHANDLE 'MIA21: ' . $RemarkCode21 . "\n";
                }
                if ($RemarkCode22 ne '') {
                    print FILEHANDLE 'MIA22: ' . $RemarkCode22 . "\n";
                }
                if ($RemarkCode23 ne '') {
                    print FILEHANDLE 'MIA23: ' . $RemarkCode23 . "\n";
                }
                if ($PPS_CapitalExceptionAmount24  ne '') {
                    print FILEHANDLE 'MIA24: ' . $PPS_CapitalExceptionAmount24 . "\n";
                }

                                print FILEHANDLE "\n";
                            print FILEHANDLE 'CPT Details' . "\n";
                                print FILEHANDLE '-----------' . "\n";
                                my $servicepaymentcount = "1";


                                foreach my $ServicePaymentInformationLoop ($xp-&gt;find('.//ServicePaymentInformationLoop', $ClaimPaymentInformationLoop)-&gt;get_nodelist){

                                        print FILEHANDLE '(' . $servicepaymentcount . ')' . 'CPT Code: ' . $ServicePaymentInformationLoop-&gt;find('ServicePaymentInformation/Compos
iteMedicalProcedureIdentifier1/@Product_ServiceID2')-&gt;string_value . "\n";
                                        print FILEHANDLE '(' . $servicepaymentcount . ')' . 'CPT Modifier: ' . $ServicePaymentInformationLoop-&gt;find('ServicePaymentInformation/Co
mpositeMedicalProcedureIdentifier1/@ProcedureModifier3')-&gt;string_value . "\n";
                    print FILEHANDLE '(' . $servicepaymentcount . ')' . 'CPT Billed Amount: ' . $ServicePaymentInformationLoop-&gt;find('ServicePaymentInformation/@LineItemChargeAmount2')-&gt;string_value . "\n";
                    print FILEHANDLE '(' . $servicepaymentcount . ')' . 'CPT Allow Amount: ' . $ServicePaymentInformationLoop-&gt;find('ServicePaymentInformation/@LineItemProviderP
aymentAmount3')-&gt;string_value . "\n";
                                        print FILEHANDLE '(' . $servicepaymentcount . ')' . 'CPT Service Date: ' . $ServicePaymentInformationLoop-&gt;find('ServiceDate/@ServiceDate
2')-&gt;string_value . "\n";

                                        my $RemarkCode2 = $ServicePaymentInformationLoop-&gt;find('HealthCareRemarkCodes/@RemarkCode2')-&gt;string_value;

                                        if ($RemarkCode2 ne '') {
                                                print FILEHANDLE '(' . $servicepaymentcount . ')' . 'CPT Healtch Care Remark Code: ' . $RemarkCode2 . "\n";
                                        }

                                        my $adjustmentgroupcount = "1";

                                        foreach my $ServiceAdjustment ($xp-&gt;find('.//ServiceAdjustment', $ServicePaymentInformationLoop)-&gt;get_nodelist){
                                                print FILEHANDLE '      (' . $servicepaymentcount . '.' . $adjustmentgroupcount . ')' . 'Adjustment Group Code: ' . $ServiceAdjus
tment-&gt;find('@ClaimAdjustmentGroupCode1')-&gt;string_value . "\n";
                                                print FILEHANDLE '      (' . $servicepaymentcount . '.' . $adjustmentgroupcount . ')' . 'Adjustment Group Message: ?' . "\n";

                                                my $AdjustmentReasonCode2 = $ServiceAdjustment-&gt;find('@AdjustmentReasonCode2')-&gt;string_value;
                                                my $AdjustmentAmount3 = $ServiceAdjustment-&gt;find('@AdjustmentAmount3')-&gt;string_value;
                                                my $AdjustmentReasonCode5 = $ServiceAdjustment-&gt;find('@AdjustmentReasonCode5')-&gt;string_value;
                                                my $AdjustmentAmount6 = $ServiceAdjustment-&gt;find('@AdjustmentAmount6')-&gt;string_value;
                                                my $AdjustmentReasonCode8 = $ServiceAdjustment-&gt;find('@AdjustmentReasonCode8')-&gt;string_value;
                                                my $AdjustmentAmount9 = $ServiceAdjustment-&gt;find('@AdjustmentAmount9')-&gt;string_value;
                                                my $adjustmentreasoncodecount = "1";

                                                if ($AdjustmentReasonCode2 ne '') {
                                                        print FILEHANDLE '              (' . $servicepaymentcount . '.' . $adjustmentgroupcount . '.' . $adjustmentreasoncodecoun
t . ')' . 'Adjustment Reason Code: ' . $AdjustmentReasonCode2 . "\n";
                                print FILEHANDLE '              (' . $servicepaymentcount . '.' . $adjustmentgroupcount . '.' . $adjustmentreasoncodecount . ')' . 'Adjustment Re
ason Message: ?' . "\n";
                                                    print FILEHANDLE '          (' . $servicepaymentcount . '.' . $adjustmentgroupcount . '.' . $adjustmentreasoncodecount . ')'
. 'Adjustment Group Code Amount: ' . $AdjustmentAmount3 . "\n";
                                                        $adjustmentreasoncodecount++;
                                                }
                                                if ($AdjustmentReasonCode5 ne '') {
                                print FILEHANDLE '              (' . $servicepaymentcount . '.' . $adjustmentgroupcount . '.' . $adjustmentreasoncodecount . ')' . 'Adjustment Re
ason Code: ' . $AdjustmentReasonCode5 . "\n";
                            print FILEHANDLE '          (' . $servicepaymentcount . '.' . $adjustmentgroupcount . '.' . $adjustmentreasoncodecount . ')' . 'Adjustment Reason Mes
sage: ?' . "\n";
                                print FILEHANDLE '              (' . $servicepaymentcount . '.' . $adjustmentgroupcount . '.' . $adjustmentreasoncodecount . ')' . 'Adjustment Gr
oup Code Amount: ' . $AdjustmentAmount6 . "\n";
                                                        $adjustmentreasoncodecount++;
                                                }
                        if ($AdjustmentReasonCode8 ne '') {
                                                        print FILEHANDLE '              (' . $servicepaymentcount . '.' . $adjustmentgroupcount . '.' . $adjustmentreasoncodecoun
t . ')' . 'Adjustment Reason Code: ' . $AdjustmentReasonCode8 . "\n";
                                                        print FILEHANDLE '              (' . $servicepaymentcount . '.' . $adjustmentgroupcount . '.' . $adjustmentreasoncodecoun
t . ')' . 'Adjustment Reason Message: ?' . "\n";
                                                        print FILEHANDLE '              (' . $servicepaymentcount . '.' . $adjustmentgroupcount . '.' . $adjustmentreasoncodecoun
t . ')' . 'Adjustment Group Code Amount: ' . $AdjustmentAmount9 . "\n";
                                                        $adjustmentreasoncodecount++;
                                                }
                                                print FILEHANDLE "\n";
                                                $adjustmentgroupcount++;
                                        }
                                        $servicepaymentcount++;
                                }
                        $transactioncount++;
                        }
        $icncount++;
        }
}

close (FILEHANDLE);

open (MAIL, "|/usr/sbin/sendmail -t");
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";

open (MESSAGE, "&lt;", "$FileName") or die "$!";
print MAIL &lt;MESSAGE&gt;;
close (MESSAGE);
close (MAIL);
print $FileName . "\n";
move($FileName, $LogFiles);

#unlink glob('/home/jramirez/PayerDownload/SES/XMLTEMP/*.xml');

&lt;/code&gt;</field>
</data>
</node>
