通过unix在XML文件上添加注释标签

通过unix在XML文件上添加注释标签

输入 XML 文件:

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<Dcn xmlns="http://schemas.test.com/2013/ClaimRequest" xsi:schemaLocation="http://schemas.test.com/2013/ClaimRequest gdhjf/profiles/Redirect/dhd/Desktop/dhd_Prof.XSD" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Header sendingSystemId="XYC" receivingSystemId="ABC" version="2.0" createDateTime="2019-01-19T04:27:15" batchingControlNumber="AZ042018" transType="ClaimRequest" dcn="1901959351"/>
  <Body>
    <ClaimRequest>
      <Claims>
        <ProfessionalClaim paymentIndicator="P" claimProcessedDateTime="20180409120000102" claimEndDate="2018-04-02" claimStartDate="2018-04-02" sourceSystemId="abcd" claimActionCode="00">
          <PayToProvider id="54321"/>
          <Patient eligSysId="dhhgs1" clientId="VBV"/>
          <ClaimLines>
            <ClaimLine sourceSystemClaimLineNumber="1" />
            <ClaimLine sourceSystemClaimLineNumber="2" />
            <ClaimLine sourceSystemClaimLineNumber="3" />
          </ClaimLines>
          <ClaimTrailer claimDetailLineCount="3"/>
        </ProfessionalClaim>
         <ProfessionalClaim paymentIndicator="P" claimProcessedDateTime="20180430120000277" claimEndDate="2018-04-12" claimStartDate="2018-04-12" sourceSystemId="abcd" claimActionCode="00">
          <PayToProvider id="54673"/>
          <Patient eligSysId="dhhgs1" clientId="VBV"/>
          <ClaimLines>
            <ClaimLine sourceSystemClaimLineNumber="1" />
          </ClaimLines>
          <ClaimTrailer claimDetailLineCount="1"/>
        </ProfessionalClaim>
        <ProfessionalClaim paymentIndicator="P" claimProcessedDateTime="20180430120000279" claimEndDate="2018-04-17" claimStartDate="2018-04-17" sourceSystemId="abcd" claimActionCode="00">
          <PayToProvider id="676543"/>
          <Patient lastName="CLARK" memberId="U1672936601" firstName="DANIEL" eligSysId="CNCIEDW1" clientId="CNC"/>
          <ClaimLines>
            <ClaimLine  sourceSystemClaimLineNumber="1" />
            <ClaimLine  sourceSystemClaimLineNumber="2" />
          </ClaimLines>
          <ClaimTrailer claimDetailLineCount="2"/>
        </ProfessionalClaim>
        <ProfessionalClaim paymentIndicator="P" claimProcessedDateTime="20180430120000281" claimEndDate="2018-04-17" claimStartDate="2018-04-17" sourceSystemId="abcd" claimActionCode="00">
          <PayToProvider id="653473"/>
          <Patient lastName="VAUGHN" memberId="U1677077501" firstName="KAREN" eligSysId="CNCIEDW1" clientId="CNC"/>
          <ClaimLines>
            <ClaimLine sourceSystemClaimLineNumber="1"/>
            <ClaimLine sourceSystemClaimLineNumber="2"/>
          </ClaimLines>
          <ClaimTrailer claimDetailLineCount="2"/>
        </ProfessionalClaim>
      </Claims>
      <IcnCount total="4"/>
    </ClaimRequest>
  </Body>
</Dcn>

我必须在此 XML 文件中的标头标记之前添加注释标记,并填充以下详细信息。

<!-- Claims Paid_Start_Date="2018-04-02" Paid_End_Date="2018-04-17" Process_Start_Date="20180409" Process_End_Date="201804301" -->

这里,

  • Claim_Start_Date= 文件中的 ClaimStartDate 的最小值。
  • Claim_End_Date= 文件中的 ClaimEndDate 的最大值
  • Process_Start_Date= 文件中的第一个claimprocessedDateTime
  • Process_End_Date= 文件中的最后一个claimProcessedDateTime

这是我到目前为止所做的:

F_LINE=awk '/<professionalclaim/' test.xml | head -1

L_LINE=awk '/<professionalclaim/' test.xml | tail -1

现在我需要从这两行中提取所需的值并填充到注释标记中。

答案1

使用 XML 解析器,您可以像这样一次提取一个必要的组件。我们假设您的文件是x.xml.

# Parse the XML file for the relevant entries
#
minClaimStartDate=$(xmlstarlet sel -t -v '//_:ProfessionalClaim/@claimStartDate' -n x.xml | sort -V | head -n1)
maxClaimEndDate=$(xmlstarlet sel -t -v '//_:ProfessionalClaim/@claimStartDate' -n x.xml | sort -V | tail -n1)
...

# Grab the XML declaration header
#
header=$(head -n1 x.xml)    # Ugh, I can't find a way to do this properly

然后你可以像这样把这些碎片重新组合起来

# Output composite
#
echo "$header"
echo "<!-- Claims Paid_Start_Date="$minClaimStartDate" Paid_End_Date="$maxClaimEndDate" ... -->"
xmlstarlet fo --omit-decl x.xml

相关内容