Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

XPATH Looping

by medirecpr (Novice)
on Sep 18, 2012 at 22:17 UTC ( #994375=perlquestion: print w/ replies, xml ) Need Help??
medirecpr has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks! Humbly asking for help! I am trying to parse xml files with the following structure:

<?xml version="1.0"?> <file> <InterchangeLoop> <InterchangeControlHeader AuthorizationInformationQualifier1=" +00" AuthorizationInformation2=" " SecurityInformationQualifi +er3="00" SecurityInformation4=" " InterchangeIDQualifier5="30" InterchangeSenderID6="660600000 + " InterchangeIDQualifier7="30" InterchangeReceiverID8="660600001 + " InterchangeDate9="120622" Intercha ngeTime10="0355" InterchangeControlStandardsIdentifier11="U" Interchan +geControlVersionNumber12="00401" InterchangeControlNumber13="00000025 +1" AcknowledgmentRequested14="0" Usage Indicator15="P" ComponentElementSeparator16=":"/> <FunctionalGroupLoop> <FunctionalGroupHeader FunctionalIdentifierCode1="HP" Applicat +ionSender_sCode2="660600000" ApplicationReceiver_sCode3="660600001" D +ate4="20120622" Time5="0355" GroupCont rolNumber6="251" ResponsibleAgencyCode7="X" Version_Release_IndustryId +entifierCode8="004010X091A1"/> <Transaction835> <TransactionSetHeader TransactionSetIdentifierCode1="835" Tran +sactionSetControlNumber2="0085"/> <FinancialInformation TransactionHandlingCode1="C" TotalActual +ProviderPaymentAmount2="619.54" CreditorDebitFlagCode3="C" PaymentMet +hodCode4="CHK" CheckIssueorEFTEffectiv eDate16="20120619"/> <ReassociationTraceNumber TraceTypeCode1="1" CheckorEFTTraceNu +mber2="9049000" PayerIdentifier3="1660000000"/> <ReceiverIdentification ReferenceIdentificationQualifier1="EV" + ReceiverIdentifier2="660600000"/> <ProductionDate DateTimeQualifier1="405" ProductionDate2="2012 +0619"/> <PayerIdentificationLoop> <PayerIdentification EntityIdentifierCode1="PR" PayerName2="SO +ME CHOICE PAYER" IdentificationCodeQualifier3="XV" PayerIdentifier4=" +660000000"/> <PayerAddress PayerAddressLine1=" CARIBE"/> <PayerCity_State_ZIPCode PayerCityName1="SAN JUAN" PayerStateC +ode2="PR" PayerPostalZoneorZIPCode3="00926"/> </PayerIdentificationLoop> <PayeeIdentificationLoop> <PayeeIdentification EntityIdentifierCode1="PE" PayeeName2="CO +RP. DE SALUD " IdentificationCodeQualifier3="XX" PayeeIdentificationC +ode4="1234000000"/> <PayeeAdditionalIdentification ReferenceIdentificationQualifie +r1="TJ" AdditionalPayeeIdentifier2="660600001"/> </PayeeIdentificationLoop> <HeaderNumberLoop> <HeaderNumber AssignedNumber1="1"/> <ClaimPaymentInformationLoop> <ClaimPaymentInformation PatientControlNumber1="30290" + ClaimStatusCode2="1" TotalClaimChargeAmount3="275.13" ClaimPaymentAm +ount4="241.28" ClaimFilingIndicatorCod e6="13" PayerClaimControlNumber7="12220EEEEEEE"/> <PatientName EntityIdentifierCode1="QC" EntityTypeQual +ifier2="1" PatientLastName3="APELLIDO" PatientFirstName4="NAME" Ident +ificationCodeQualifier8="MI" PatientId entifier9="030000000"/> <ServiceProviderName EntityIdentifierCode1="82" Entity +TypeQualifier2="2" RenderingProviderLastorOrganizationName3="" Identi +ficationCodeQualifier8="XX" RenderingP roviderIdentifier9="1234567890"/> <ServicePaymentInformationLoop> <ServicePaymentInformation LineItemChargeAmount2="275. +13" LineItemProviderPaymentAmount3="241.28" UnitsofServicePaidCount5= +"1"> <CompositeMedicalProcedureIdentifier1 Product_ +ServiceIDQualifier1="HC" Product_ServiceID2="99999" ProcedureModifier +3="26"/> </ServicePaymentInformation> <ServiceDate DateTimeQualifier1="472" ServiceDate2="20 +120614"/> <ServiceAdjustment ClaimAdjustmentGroupCode1="CO" Adju +stmentReasonCode2="125" AdjustmentAmount3="15.69" AdjustmentReasonCod +e5="105" AdjustmentAmount6="18.16"/> <ServiceIdentification ReferenceIdentificationQualifie +r1="6R" ProviderIdentifier2="3999999"/> <HealthCareRemarkCodes CodeListQualifierCode1="HE" Rem +arkCode2="N381"/> </ServicePaymentInformationLoop> </ClaimPaymentInformationLoop> </HeaderNumberLoop> <HeaderNumberLoop> <HeaderNumber AssignedNumber1="2"/> <ClaimPaymentInformationLoop> <ClaimPaymentInformation PatientControlNumber1="30289" + ClaimStatusCode2="1" TotalClaimChargeAmount3="99.32" ClaimPaymentAmo +unt4="92.37" ClaimFilingIndicatorCode6 ="13" PayerClaimControlNumber7="12220EEEEEEE"/> <PatientName EntityIdentifierCode1="QC" EntityTypeQual +ifier2="1" PatientLastName3="APELLIDO" PatientFirstName4="NAME" Ident +ificationCodeQualifier8="MI" PatientId entifier9="030000000"/> <ServiceProviderName EntityIdentifierCode1="82" Entity +TypeQualifier2="2" RenderingProviderLastorOrganizationName3="" Identi +ficationCodeQualifier8="XX" RenderingP roviderIdentifier9="1234567890"/> <ServicePaymentInformationLoop> <ServicePaymentInformation LineItemChargeAmount2="84.8 +3" LineItemProviderPaymentAmount3="78.89" UnitsofServicePaidCount5="1 +"> <CompositeMedicalProcedureIdentifier1 Product_ +ServiceIDQualifier1="HC" Product_ServiceID2="99203" ProcedureModifier +3="25"/> </ServicePaymentInformation> <ServiceDate DateTimeQualifier1="472" ServiceDate2="20 +120611"/> <ServiceAdjustment ClaimAdjustmentGroupCode1="CO" Adju +stmentReasonCode2="105" AdjustmentAmount3="5.94"/> <ServiceIdentification ReferenceIdentificationQualifie +r1="6R" ProviderIdentifier2="3028901"/> </ServicePaymentInformationLoop> <ServicePaymentInformationLoop> <ServicePaymentInformation LineItemChargeAmount2="14.4 +9" LineItemProviderPaymentAmount3="13.48" UnitsofServicePaidCount5="1 +"> <CompositeMedicalProcedureIdentifier1 Product_ +ServiceIDQualifier1="HC" Product_ServiceID2="93000"/> </ServicePaymentInformation> <ServiceDate DateTimeQualifier1="472" ServiceDate2="20 +120611"/> <ServiceAdjustment ClaimAdjustmentGroupCode1="CO" Adju +stmentReasonCode2="105" AdjustmentAmount3="1.01"/> <ServiceIdentification ReferenceIdentificationQualifie +r1="6R" ProviderIdentifier2="3099999"/> </ServicePaymentInformationLoop> </ClaimPaymentInformationLoop> </HeaderNumberLoop> <HeaderNumberLoop> <HeaderNumber AssignedNumber1="3"/> <ClaimPaymentInformationLoop> <ClaimPaymentInformation PatientControlNumber1="30293" + ClaimStatusCode2="1" TotalClaimChargeAmount3="185.88" ClaimPaymentAm +ount4="165.57" ClaimFilingIndicatorCod e6="13" PayerClaimControlNumber7="12220EEEEEEE"/> <PatientName EntityIdentifierCode1="QC" EntityTypeQual +ifier2="1" PatientLastName3="APELLIDO" PatientFirstName4="NAME" Ident +ificationCodeQualifier8="MI" PatientId entifier9="030000000"/> <ServiceProviderName EntityIdentifierCode1="82" Entity +TypeQualifier2="2" RenderingProviderLastorOrganizationName3="" Identi +ficationCodeQualifier8="XX" RenderingP roviderIdentifier9="1234567890"/> <ServicePaymentInformationLoop> <ServicePaymentInformation LineItemChargeAmount2="178. +39" LineItemProviderPaymentAmount3="158.6" UnitsofServicePaidCount5=" +1"> <CompositeMedicalProcedureIdentifier1 Product_ +ServiceIDQualifier1="HC" Product_ServiceID2="99223"/> </ServicePaymentInformation> <ServiceDate DateTimeQualifier1="472" ServiceDate2="20 +120613"/> <ServiceAdjustment ClaimAdjustmentGroupCode1="CO" Adju +stmentReasonCode2="125" AdjustmentAmount3="7.85" AdjustmentReasonCode +5="105" AdjustmentAmount6="11.94"/> <ServiceIdentification ReferenceIdentificationQualifie +r1="6R" ProviderIdentifier2="3029999"/> <HealthCareRemarkCodes CodeListQualifierCode1="HE" Rem +arkCode2="N381"/> </ServicePaymentInformationLoop> <ServicePaymentInformationLoop> <ServicePaymentInformation LineItemChargeAmount2="7.49 +" LineItemProviderPaymentAmount3="6.97" UnitsofServicePaidCount5="1"> <CompositeMedicalProcedureIdentifier1 Product_ +ServiceIDQualifier1="HC" Product_ServiceID2="93010"/> </ServicePaymentInformation> <ServiceDate DateTimeQualifier1="472" ServiceDate2="20 +120613"/> <ServiceAdjustment ClaimAdjustmentGroupCode1="CO" Adju +stmentReasonCode2="105" AdjustmentAmount3=".52"/> <ServiceIdentification ReferenceIdentificationQualifie +r1="6R" ProviderIdentifier2="3029999"/> </ServicePaymentInformationLoop> </ClaimPaymentInformationLoop> </HeaderNumberLoop> <HeaderNumberLoop> <HeaderNumber AssignedNumber1="4"/> <ClaimPaymentInformationLoop> <ClaimPaymentInformation PatientControlNumber1="39999" + ClaimStatusCode2="1" TotalClaimChargeAmount3="71.87" ClaimPaymentAmo +unt4="66.84" ClaimFilingIndicatorCode6 ="13" PayerClaimControlNumber7="12170EAAAAA"/> <PatientName EntityIdentifierCode1="QC" EntityTypeQual +ifier2="1" PatientLastName3="LAST LASTS" PatientFirstName4="FIRSTSS" +IdentificationCodeQualifier8="MI" Pati entIdentifier9="031231234"/> <ServiceProviderName EntityIdentifierCode1="82" Entity +TypeQualifier2="2" RenderingProviderLastorOrganizationName3="" Identi +ficationCodeQualifier8="XX" RenderingP roviderIdentifier9="1234567890"/> <ServicePaymentInformationLoop> <ServicePaymentInformation LineItemChargeAmount2="57.3 +8" LineItemProviderPaymentAmount3="53.36" UnitsofServicePaidCount5="1 +"> <CompositeMedicalProcedureIdentifier1 Product_ +ServiceIDQualifier1="HC" Product_ServiceID2="99213" ProcedureModifier +3="25"/> </ServicePaymentInformation> <ServiceDate DateTimeQualifier1="472" ServiceDate2="20 +120611"/> <ServiceAdjustment ClaimAdjustmentGroupCode1="CO" Adju +stmentReasonCode2="105" AdjustmentAmount3="4.02"/> <ServiceIdentification ReferenceIdentificationQualifie +r1="6R" ProviderIdentifier2="3999901"/> </ServicePaymentInformationLoop> <ServicePaymentInformationLoop> <ServicePaymentInformation LineItemChargeAmount2="14.4 +9" LineItemProviderPaymentAmount3="13.48" UnitsofServicePaidCount5="1 +"> <CompositeMedicalProcedureIdentifier1 Product_ +ServiceIDQualifier1="HC" Product_ServiceID2="93000"/> </ServicePaymentInformation> <ServiceDate DateTimeQualifier1="472" ServiceDate2="20 +120611"/> <ServiceAdjustment ClaimAdjustmentGroupCode1="CO" Adju +stmentReasonCode2="105" AdjustmentAmount3="1.01"/> <ServiceIdentification ReferenceIdentificationQualifie +r1="6R" ProviderIdentifier2="3999902"/> </ServicePaymentInformationLoop> </ClaimPaymentInformationLoop> </HeaderNumberLoop> <HeaderNumberLoop> <HeaderNumber AssignedNumber1="5"/> <ClaimPaymentInformationLoop> <ClaimPaymentInformation PatientControlNumber1="36666" + ClaimStatusCode2="1" TotalClaimChargeAmount3="57.51" ClaimPaymentAmo +unt4="53.48" ClaimFilingIndicatorCode6 ="13" PayerClaimControlNumber7="12220ETTTTTT"/> <PatientName EntityIdentifierCode1="QC" EntityTypeQual +ifier2="1" PatientLastName3="LAST 1234" PatientFirstName4="First 1234 +" IdentificationCodeQualifier8="MI" Pa tientIdentifier9="031231289"/> <ServiceProviderName EntityIdentifierCode1="82" Entity +TypeQualifier2="2" RenderingProviderLastorOrganizationName3="" Identi +ficationCodeQualifier8="XX" RenderingP roviderIdentifier9="1234567890"/> <ServicePaymentInformationLoop> <ServicePaymentInformation LineItemChargeAmount2="57.5 +1" LineItemProviderPaymentAmount3="53.48" UnitsofServicePaidCount5="1 +"> <CompositeMedicalProcedureIdentifier1 Product_ +ServiceIDQualifier1="HC" Product_ServiceID2="91111" ProcedureModifier +3="26"/> </ServicePaymentInformation> <ServiceDate DateTimeQualifier1="472" ServiceDate2="20 +120613"/> <ServiceAdjustment ClaimAdjustmentGroupCode1="CO" Adju +stmentReasonCode2="105" AdjustmentAmount3="4.03"/> <ServiceIdentification ReferenceIdentificationQualifie +r1="6R" ProviderIdentifier2="3666601"/> </ServicePaymentInformationLoop> </ClaimPaymentInformationLoop> </HeaderNumberLoop> <TransactionSetTrailer TransactionSegmentCount1="65" Transacti +onSetControlNumber2="0085"/> </Transaction835> <FunctionalGroupTrailer NumberofTransactionSetsIncluded1="1" G +roupControlNumber2="251"/> </FunctionalGroupLoop> <InterchangeControlTrailer NumberofIncludedFunctionalGroups1=" +1" InterchangeControlNumber2="000000251"/> </InterchangeLoop> </file>

Using the following code:

#!/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->new(filename => $file); my $icncount = 1; my $transactioncount = 1; my $claimpaymentcount = 1; my $servicepaymentcount = 1; my $tm = (localtime); foreach my $InterchangeControlHeader ($xp->find('//InterchangeControlH +eader')->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->find('//@InterchangeCon +trolNumber13')->string_value . "\n"; foreach my $Transaction835 ($xp->find('//Transaction835')->get_nod +elist){ # print $transactioncount . ') ' . 'Transaction Set Control Numb +er : ' . $Transaction835->find('TransactionSetHeader/@TransactionSetC +ontrolNumber2')->string_value . "\n"; print 'Check/EFT Amount: ' . $Transaction835->find('Financ +ialInformation/@TotalActualProviderPaymentAmount2')->string_value . " +\n"; print 'Check/EFT Credit/Debit Type: ' . $Transaction835->f +ind('FinancialInformation/@CreditorDebitFlagCode3')->string_value . " +\n"; print 'Check/EFT Type: ' . $Transaction835->find('Financia +lInformation/@PaymentMethodCode4')->string_value . "\n"; print 'Check/EFT Payment Date: ' . $Transaction835->find(' +FinancialInformation/@CheckIssueorEFTEffectiveDate16')->string_value +. "\n"; print 'Check/EFT Number: ' . $Transaction835->find('Reasso +ciationTraceNumber/@CheckorEFTTraceNumber2')->string_value . "\n"; print 'Production Date: ' . $Transaction835->find('Product +ionDate/@ProductionDate2')->string_value . "\n"; print 'Payer Name: ' . $Transaction835->find('//PayerIdent +ificationLoop/PayerIdentification/@PayerName2')->string_value . "\n"; print 'Payer Address: ' . $Transaction835->find('//PayerId +entificationLoop/PayerAddress/@PayerAddressLine1')->string_value . " +"; print $Transaction835->find('//PayerIdentificationLoop/Pay +erCity_State_ZIPCode/@PayerCityName1')->string_value . " "; print $Transaction835->find('//PayerIdentificationLoop/Pay +erCity_State_ZIPCode/@PayerStateCode2')->string_value . " "; print $Transaction835->find('//PayerIdentificationLoop/Pay +erCity_State_ZIPCode/@PayerPostalZoneorZIPCode3')->string_value . "\n +"; print 'Payee Name: ' . $Transaction835->find('//PayeeIdent +ificationLoop/PayeeIdentification/@PayeeName2')->string_value . "\n"; print 'Payee ICN: ' . $Transaction835->find('//PayeeIdenti +ficationLoop/PayeeIdentification/@PayeeIdentificationCode4')->string_ +value . "\n"; foreach my $ClaimPaymentInformationLoop ($xp->find('//Clai +mPaymentInformationLoop')->get_nodelist){ print "\n"; print 'Invoice: ' . $ClaimPaymentInformationLoop->find +('ClaimPaymentInformation/@PatientControlNumber1')->string_value . "\ +n"; print '------------------------------------' . "\n"; print 'Claim Status: ' . $ClaimPaymentInformationLoop- +>find('ClaimPaymentInformation/@ClaimStatusCode2')->string_value . "\ +n"; print 'Billed Amount: ' . $ClaimPaymentInformationLoop +->find('ClaimPaymentInformation/@TotalClaimChargeAmount3')->string_va +lue . "\n"; print 'Insurance Payment Amount: ' . $ClaimPaymentInfo +rmationLoop->find('ClaimPaymentInformation/@ClaimPaymentAmount4')->st +ring_value . "\n"; print 'Deductible Amount: ? ' . "\n"; print 'Payer ICN: ' . $ClaimPaymentInformationLoop->fi +nd('ClaimPaymentInformation/@PayerClaimControlNumber7')->string_value + . "\n"; print 'Name Patient: ' . $ClaimPaymentInformationLoop- +>find('PatientName/@PatientFirstName4')->string_value . " "; print $ClaimPaymentInformationLoop->find('PatientName/ +@PatientLastName3')->string_value . "\n"; $claimpaymentcount++; foreach my $ServicePaymentInformationLoop ($xp->find(' +//ServicePaymentInformation')->get_nodelist){ print 'CPT Code: ' . $ServicePaymentInformationLoo +p->find('CompositeMedicalProcedureIdentifier1/@Product_ServiceID2')-> +string_value . "\n"; print 'CPT Modifier: ' . $ServicePaymentInformatio +nLoop->find('CompositeMedicalProcedureIdentifier1/@ProcedureModifier3 +')->string_value . "\n"; print 'CPT Billed Amount: ' . $ServicePaymentInfor +mationLoop->find('@LineItemChargeAmount2')->string_value . "\n"; print 'CPT Allow Amount: ' . $ServicePaymentInform +ationLoop->find('@LineItemProviderPaymentAmount3')->string_value . "\ +n"; print 'Service Payment Count: ' . $servicepaymentc +ount . "\n"; $servicepaymentcount++; } $transactioncount++; } $icncount++; } }

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.

Claim Info1 Corresponding Service Loop Info1 Claim Info2 Corresponding Service Loop Info2

Kindest Regards,

Per previous posts, I have included the version of the script that is working correctly, per Choroba's input:

#!/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->new(filename => $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, ">$FileName") or die "cannot open file for reading: $ +!"; foreach my $InterchangeControlHeader ($xp->find('//InterchangeControlH +eader')->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->find('//@Interchang +eControlNumber13')->string_value . "\n"; foreach my $Transaction835 ($xp->find('//Transaction835')->get +_nodelist){ # print $transactioncount . ') ' . 'Transaction Set Cont +rol Number : ' . $Transaction835->find('TransactionSetHeader/@Transac +tionSetControlNumber2')->string_value . "\n"; print FILEHANDLE 'Check/EFT Amount: ' . $Trans +action835->find('FinancialInformation/@TotalActualProviderPaymentAmou +nt2')->string_value . "\n"; print FILEHANDLE 'Check/EFT Credit/Debit Type: ' . $Tr +ansaction835->find('FinancialInformation/@CreditorDebitFlagCode3')->s +tring_value . "\n"; print FILEHANDLE 'Check/EFT Type: ' . $Transac +tion835->find('FinancialInformation/@PaymentMethodCode4')->string_val +ue . "\n"; print FILEHANDLE 'Check/EFT Payment Date: ' . $Transac +tion835->find('FinancialInformation/@CheckIssueorEFTEffectiveDate16') +->string_value . "\n"; print FILEHANDLE 'Check/EFT Number: ' . $Transaction83 +5->find('ReassociationTraceNumber/@CheckorEFTTraceNumber2')->string_v +alue . "\n"; print FILEHANDLE 'Production Date: ' . $Transaction835 +->find('ProductionDate/@ProductionDate2')->string_value . "\n"; print FILEHANDLE 'Payer Name: ' . $Transaction835->fin +d('//PayerIdentificationLoop/PayerIdentification/@PayerName2')->strin +g_value . "\n"; print FILEHANDLE 'Payer Address: ' . $Transaction835-> +find('//PayerIdentificationLoop/PayerAddress/@PayerAddressLine1')->st +ring_value . " "; print FILEHANDLE $Transaction835->find('//PayerIdentif +icationLoop/PayerCity_State_ZIPCode/@PayerCityName1')->string_value . + " "; print FILEHANDLE $Transaction835->find('//PayerIdentif +icationLoop/PayerCity_State_ZIPCode/@PayerStateCode2')->string_value +. " "; print FILEHANDLE $Transaction835->find('//PayerIde +ntificationLoop/PayerCity_State_ZIPCode/@PayerPostalZoneorZIPCode3')- +>string_value . "\n"; print FILEHANDLE 'Payee Name: ' . $Transaction +835->find('//PayeeIdentificationLoop/PayeeIdentification/@PayeeName2' +)->string_value . "\n"; print FILEHANDLE 'Payee ICN: ' . $Transaction835->find +('//PayeeIdentificationLoop/PayeeIdentification/@PayeeIdentificationC +ode4')->string_value . "\n"; print FILEHANDLE 'Provider Adjustment: ' . $Tr +ansaction835->find('//ProviderAdjustment/@ProviderAdjustmentAmount4') +->string_value . "\n"; print FILEHANDLE 'Provider Adjustment Reason C +ode: ' . $Transaction835->find('//ProviderAdjustment/AdjustmentIdenti +fier3/@AdjustmentReasonCode1')->string _value . "\n"; foreach my $ClaimPaymentInformationLoop ($xp-> +find('.//ClaimPaymentInformationLoop', $Transaction835)->get_nodelist +){ print FILEHANDLE "\n"; print FILEHANDLE 'Invoice: ' . $ClaimP +aymentInformationLoop->find('ClaimPaymentInformation/@PatientControlN +umber1')->string_value . "\n"; print FILEHANDLE '-------------------- +----------------' . "\n"; print FILEHANDLE 'Claim Status: ' . $C +laimPaymentInformationLoop->find('ClaimPaymentInformation/@ClaimStatu +sCode2')->string_value . "\n"; print FILEHANDLE 'Billed Amount: ' . $ClaimPaymentInfo +rmationLoop->find('ClaimPaymentInformation/@TotalClaimChargeAmount3') +->string_value . "\n"; print FILEHANDLE 'Insurance Payment Amount: ' . $Claim +PaymentInformationLoop->find('ClaimPaymentInformation/@ClaimPaymentAm +ount4')->string_value . "\n"; print FILEHANDLE 'Deductible Amount: ? ' . "\n"; print FILEHANDLE 'Payer ICN: ' . $ClaimPaymentInformat +ionLoop->find('ClaimPaymentInformation/@PayerClaimControlNumber7')->s +tring_value . "\n"; print FILEHANDLE 'Name Patient: ' . $ClaimPaymentInfor +mationLoop->find('PatientName/@PatientFirstName4')->string_value . " +"; print FILEHANDLE $ClaimPaymentInformat +ionLoop->find('PatientName/@PatientLastName3')->string_value . "\n"; #$claimpaymentcount++; my $MOARemarkCode1 = $ClaimPaymentInfo +rmationLoop->find('OutpatientAdjudicationInformation/@RemarkCode1')-> +string_value; my $MOARemarkCode2 = $ClaimPaymentInformationLoop->fin +d('OutpatientAdjudicationInformation/@RemarkCode2')->string_value; my $MOARemarkCode3 = $ClaimPaymentInformationLoop->fin +d('OutpatientAdjudicationInformation/@RemarkCode3')->string_value; my $MOARemarkCode4 = $ClaimPaymentInformationLoop->fin +d('OutpatientAdjudicationInformation/@RemarkCode4')->string_value; my $MOARemarkCode5 = $ClaimPaymentInformationLoop->fin +d('OutpatientAdjudicationInformation/@RemarkCode5')->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 = $ClaimPaymentInformati +onLoop->find('InpatientAdjudicationInformation/@CoveredDaysorVisitsCo +unt1')->string_value; my $PPSOperatingOutlierAmount2 = $ClaimPaymentInformat +ionLoop->find('InpatientAdjudicationInformation/@PPSOperatingOutlierA +mount2')->string_value; my $LifetimePsychiatricDaysCount3 = $ClaimPaymentInfor +mationLoop->find('InpatientAdjudicationInformation/@LifetimePsychiatr +icDaysCount3')->string_value; my $ClaimDRGAmount4 = $ClaimPaymentInformationLoop->fi +nd('InpatientAdjudicationInformation/@ClaimDRGAmount4')->string_value +; my $RemarkCode5 = $ClaimPaymentInformationLoop->find(' +InpatientAdjudicationInformation/@RemarkCode5')->string_value; my $ClaimDisproportionateShareAmount6 += $ClaimPaymentInformationLoop->find('InpatientAdjudicationInformatio +n/@ClaimDisproportionateShareAmount6') ->string_value; my $ClaimMSPPass_throughAmount7 = $Cla +imPaymentInformationLoop->find('InpatientAdjudicationInformation/@Cla +imMSPPass_throughAmount7')->string_val ue; my $ClaimPPSCapitalAmount8 = $ClaimPay +mentInformationLoop->find('InpatientAdjudicationInformation/@ClaimPPS +CapitalAmount8')->string_value; my $PPS_CapitalFSPDRGAmount9 = $ClaimP +aymentInformationLoop->find('InpatientAdjudicationInformation/@PPS_Ca +pitalFSPDRGAmount')->string_value; my $PPS_CapitalHSPDRGAmount10 = $Claim +PaymentInformationLoop->find('InpatientAdjudicationInformation/@PPS_C +apitalHSPDRGAmount10')->string_value; my $PPS_CapitalDSHDRGAmount11 = $Claim +PaymentInformationLoop->find('InpatientAdjudicationInformation/@PPS_C +apitalDSHDRGAmount11')->string_value; my $OldCapitalAmount12 = $ClaimPayment +InformationLoop->find('InpatientAdjudicationInformation/@OldCapitalAm +ount12')->string_value; my $PPS_CapitalIMEamount13 = $ClaimPay +mentInformationLoop->find('InpatientAdjudicationInformation/@PPS_Capi +talIMEamount13')->string_value; my $PPS_OperatingHospitalSpecificDRGAm +ount14 = $ClaimPaymentInformationLoop->find('InpatientAdjudicationInf +ormation/@PPS_OperatingHospitalSpecifi cDRGAmount14')->string_value; my $CostReportDayCount15 = $ClaimPayme +ntInformationLoop->find('InpatientAdjudicationInformation/@CostReport +DayCount15')->string_value; my $PPS_OperatingFederalSpecificDRGAmo +unt16 = $ClaimPaymentInformationLoop->find('InpatientAdjudicationInfo +rmation/@PPS_OperatingFederalSpecificD RGAmount16')->string_value; my $ClaimPPSCapitalOutlierAmount17 = $ +ClaimPaymentInformationLoop->find('InpatientAdjudicationInformation/@ +ClaimPPSCapitalOutlierAmount17')->stri ng_value; my $ClaimIndirectTeachingAmount18 = $C +laimPaymentInformationLoop->find('InpatientAdjudicationInformation/@C +laimIndirectTeachingAmount18')->string _value; my $NonpayableProfessionalComponentAmo +unt19 = $ClaimPaymentInformationLoop->find('InpatientAdjudicationInfo +rmation/@NonpayableProfessionalCompone ntAmount19')->string_value; my $RemarkCode20 = $ClaimPaymentInform +ationLoop->find('InpatientAdjudicationInformation/@RemarkCode20')->st +ring_value; my $RemarkCode21 = $ClaimPaymentInform +ationLoop->find('InpatientAdjudicationInformation/@RemarkCode21')->st +ring_value; my $RemarkCode22 = $ClaimPaymentInform +ationLoop->find('InpatientAdjudicationInformation/@RemarkCode22')->st +ring_value; my $RemarkCode23 = $ClaimPaymentInform +ationLoop->find('InpatientAdjudicationInformation/@RemarkCode23')->st +ring_value; my $PPS_CapitalExceptionAmount24 = $Cl +aimPaymentInformationLoop->find('InpatientAdjudicationInformation/@PP +S_CapitalExceptionAmount24')->string_v alue; if ($CoveredDaysorVisitsCount1 ne '') +{ print FILEHANDLE 'MOA01: ' . $CoveredDaysorVisitsC +ount1 . "\n"; } if ($PPSOperatingOutlierAmount2 ne '') { print FILEHANDLE 'MOA02: ' . $PPSOperatingOutlierA +mount2 . "\n"; } if ($LifetimePsychiatricDaysCount3 ne '') { print FILEHANDLE 'MIA03: ' . $LifetimePsychiatricD +aysCount3 . "\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: ' . $OldCapitalAmount +12 . "\n"; } if ($PPS_CapitalIMEamount13 ne '') { print FILEHANDLE 'MIA:13 ' . $PPS_CapitalIMEamount +13 . "\n"; } if ($PPS_OperatingHospitalSpecificDRGAmount14 ne '') { print FILEHANDLE 'MIA14: ' . $PPS_OperatingHospita +lSpecificDRGAmount14 . "\n"; } if ($CostReportDayCount15 ne '') { print FILEHANDLE 'MIA15: ' . $Cost +ReportDayCount15 . "\n"; } if ($PPS_OperatingFederalSpecificDRGAmount16 ne '') { print FILEHANDLE 'MIA16: ' . $PPS_OperatingFederal +SpecificDRGAmount16 . "\n"; } if ($ClaimPPSCapitalOutlierAmount17 ne '') { print FILEHANDLE 'MIA17: ' . $ClaimPPSCapitalOutli +erAmount17 . "\n"; } if ($ClaimIndirectTeachingAmount18 ne '') { print FILEHANDLE 'MIA18: ' . $ClaimIndirectTeachin +gAmount18 . "\n"; } if ($NonpayableProfessionalComponentAm +ount19 ne '') { print FILEHANDLE 'MIA19: ' . $NonpayableProfession +alComponentAmount19 . "\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_CapitalException +Amount24 . "\n"; } print FILEHANDLE "\n"; print FILEHANDLE 'CPT Details' . "\n"; print FILEHANDLE '-----------' . "\n"; my $servicepaymentcount = "1"; foreach my $ServicePaymentInformationL +oop ($xp->find('.//ServicePaymentInformationLoop', $ClaimPaymentInfor +mationLoop)->get_nodelist){ print FILEHANDLE '(' . $servic +epaymentcount . ')' . 'CPT Code: ' . $ServicePaymentInformationLoop-> +find('ServicePaymentInformation/Compos iteMedicalProcedureIdentifier1/@Product_ServiceID2')->string_value . " +\n"; print FILEHANDLE '(' . $servic +epaymentcount . ')' . 'CPT Modifier: ' . $ServicePaymentInformationLo +op->find('ServicePaymentInformation/Co mpositeMedicalProcedureIdentifier1/@ProcedureModifier3')->string_value + . "\n"; print FILEHANDLE '(' . $servicepaymentcount . ')' +. 'CPT Billed Amount: ' . $ServicePaymentInformationLoop->find('Servi +cePaymentInformation/@LineItemChargeAmount2')->string_value . "\n"; print FILEHANDLE '(' . $servicepaymentcount . ')' +. 'CPT Allow Amount: ' . $ServicePaymentInformationLoop->find('Servic +ePaymentInformation/@LineItemProviderP aymentAmount3')->string_value . "\n"; print FILEHANDLE '(' . $servic +epaymentcount . ')' . 'CPT Service Date: ' . $ServicePaymentInformati +onLoop->find('ServiceDate/@ServiceDate 2')->string_value . "\n"; my $RemarkCode2 = $ServicePaym +entInformationLoop->find('HealthCareRemarkCodes/@RemarkCode2')->strin +g_value; if ($RemarkCode2 ne '') { print FILEHANDLE '(' . + $servicepaymentcount . ')' . 'CPT Healtch Care Remark Code: ' . $Rem +arkCode2 . "\n"; } my $adjustmentgroupcount = "1" +; foreach my $ServiceAdjustment +($xp->find('.//ServiceAdjustment', $ServicePaymentInformationLoop)->g +et_nodelist){ print FILEHANDLE ' + (' . $servicepaymentcount . '.' . $adjustmentgroupcount . ')' . 'Ad +justment Group Code: ' . $ServiceAdjus tment->find('@ClaimAdjustmentGroupCode1')->string_value . "\n"; print FILEHANDLE ' + (' . $servicepaymentcount . '.' . $adjustmentgroupcount . ')' . 'Ad +justment Group Message: ?' . "\n"; my $AdjustmentReasonCo +de2 = $ServiceAdjustment->find('@AdjustmentReasonCode2')->string_valu +e; my $AdjustmentAmount3 += $ServiceAdjustment->find('@AdjustmentAmount3')->string_value; my $AdjustmentReasonCo +de5 = $ServiceAdjustment->find('@AdjustmentReasonCode5')->string_valu +e; my $AdjustmentAmount6 += $ServiceAdjustment->find('@AdjustmentAmount6')->string_value; my $AdjustmentReasonCo +de8 = $ServiceAdjustment->find('@AdjustmentReasonCode8')->string_valu +e; my $AdjustmentAmount9 += $ServiceAdjustment->find('@AdjustmentAmount9')->string_value; my $adjustmentreasonco +decount = "1"; if ($AdjustmentReasonC +ode2 ne '') { print FILEHAND +LE ' (' . $servicepaymentcount . '.' . $adjustmentgroupc +ount . '.' . $adjustmentreasoncodecoun t . ')' . 'Adjustment Reason Code: ' . $AdjustmentReasonCode2 . "\n"; print FILEHANDLE ' (' . $ +servicepaymentcount . '.' . $adjustmentgroupcount . '.' . $adjustment +reasoncodecount . ')' . 'Adjustment Re ason Message: ?' . "\n"; print FILEHANDLE ' + (' . $servicepaymentcount . '.' . $adjustmentgroupcount . ' +.' . $adjustmentreasoncodecount . ')' . 'Adjustment Group Code Amount: ' . $AdjustmentAmount3 . "\n"; $adjustmentrea +soncodecount++; } if ($AdjustmentReasonC +ode5 ne '') { print FILEHANDLE ' (' . $ +servicepaymentcount . '.' . $adjustmentgroupcount . '.' . $adjustment +reasoncodecount . ')' . 'Adjustment Re ason Code: ' . $AdjustmentReasonCode5 . "\n"; print FILEHANDLE ' (' . $servicep +aymentcount . '.' . $adjustmentgroupcount . '.' . $adjustmentreasonco +decount . ')' . 'Adjustment Reason Mes sage: ?' . "\n"; print FILEHANDLE ' (' . $ +servicepaymentcount . '.' . $adjustmentgroupcount . '.' . $adjustment +reasoncodecount . ')' . 'Adjustment Gr oup Code Amount: ' . $AdjustmentAmount6 . "\n"; $adjustmentrea +soncodecount++; } if ($AdjustmentReasonCode8 ne '') { print FILEHAND +LE ' (' . $servicepaymentcount . '.' . $adjustmentgroupc +ount . '.' . $adjustmentreasoncodecoun t . ')' . 'Adjustment Reason Code: ' . $AdjustmentReasonCode8 . "\n"; print FILEHAND +LE ' (' . $servicepaymentcount . '.' . $adjustmentgroupc +ount . '.' . $adjustmentreasoncodecoun t . ')' . 'Adjustment Reason Message: ?' . "\n"; print FILEHAND +LE ' (' . $servicepaymentcount . '.' . $adjustmentgroupc +ount . '.' . $adjustmentreasoncodecoun t . ')' . 'Adjustment Group Code Amount: ' . $AdjustmentAmount9 . "\n" +; $adjustmentrea +soncodecount++; } 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, "<", "$FileName") or die "$!"; print MAIL <MESSAGE>; close (MESSAGE); close (MAIL); print $FileName . "\n"; move($FileName, $LogFiles); #unlink glob('/home/jramirez/PayerDownload/SES/XMLTEMP/*.xml');

Comment on XPATH Looping
Select or Download Code
Re: XPATH Looping
by Anonymous Monk on Sep 18, 2012 at 23:12 UTC
    Please make the script compilable, it won't run as is
Re: XPATH Looping
by kcott (Abbot) on Sep 19, 2012 at 05:38 UTC

    G'day medirecpr,

    Welcome to the monastery.

    I doubt that the code you posted is part of whatever script "works fine". You have two instances of:

    find('...

    which should probably be

    find('...')

    Please post a minimal example of code that reproduces your problem. These guidelines will help you in deciding how best to do this: How do I post a question effectively?

    -- Ken

      Thank you for responding! I did not read before posting. I am copying the script for your review. I can also remove all PHI from the XML and copy it as well. Regards,

        choroba++ has provided the solution below.

        Here's a couple of links you may find useful:

        XML Path Language (XPath)
        This shows the syntax from the W3C Recommendation. Note the difference between //path and .//path.
        XML::XPath - MANIFEST
        This has numerous examples of usage. In the past, I've found the code in the t/ directory particularly useful.

        -- Ken

      I have updated the code and XML example. <<This may be double posted>> Still learning my way around the website. I really appreciate the help. I was not expecting forum members to actually run the code and troubleshoot. More looking for direction. I'll take the troubleshooting though. Kindest regards,

        GOT IT!!!! I figured it out. I'll post the code as it is now and working. Going through the process of explaining the issue really helped. Kindest regards, and thanks to kcott!

Re: XPATH Looping
by choroba (Abbot) on Sep 19, 2012 at 23:11 UTC
    You are searching for nodes with //. It starts from the top and seraches everywhere. That is not what you want. Start your XPath expressions (except the first one) with .// and add the second argument to find - the context node. The loop variable of the nearest higher loop should be the context node. The loop entrances will look like this:
    for my $Transaction835 ($xp->find('//Transaction835')->get_nodelist) { ... for my $ClaimPaymentInformationLoop ($xp->find('.//ClaimPaymentInf +ormationLoop', $Transaction835)->get_nodelist) { ... for my $ServicePaymentInformationLoop ($xp->find('.//ServicePa +ymentInformation', $ClaimPaymentInformationLoop)->get_nodelist) {
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Yes! That was it! Got it to work as expected. Thank you Monk choroba. Will be working on this all day, also update post with latest code. Kindest regards,

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://994375]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2014-08-01 04:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (256 votes), past polls