0

我正在尝试将两个 xml 文件与基于条件的更新合并。在第一个文件中,您会注意到有 3 个 SECTION_SUMMARY 序列。第 3 个序列的 LockAuthority 属性为“1”。

    File1.xml
     <DOCUMENT>
      <LOANS>
       <LOAN SequenceNumber="1" xlink:label="LOAN1" LoanRoleType="SubjectLoan">
    <DOCUMENT_SETS>
     <DOCUMENT_SET>
      <GFE>
       <SECTION_SUMMARIES>
        <SECTION_SUMMARY SequenceNumber="1">
         <Amount>1975.00</Amount>
         <Type>A</Type>
        </SECTION_SUMMARY>
        <SECTION_SUMMARY SequenceNumber="2">    
         <Amount>9751.44</Amount>   
         <Type>B</Type>                    
        </SECTION_SUMMARY>
        <SECTION_SUMMARY SequenceNumber="3" LockAuthority="1">
         <Amount>3750.00</Amount>
         <Type>A</Type>
        </SECTION_SUMMARY>
       </SECTION_SUMMARIES>
      </GFE>
     </DOCUMENT_SET>
    </DOCUMENT_SETS>
    <ESCROW>
     <ESCROW_DETAIL>
      <InsuranceIndicator>true</InsuranceIndicator>
      <AllPropertyTaxesIndicator>true</AllPropertyTaxesIndicator>
      <OtherDescription>HOA</OtherDescription>
     </ESCROW_DETAIL>
    </ESCROW>
       </LOAN>
      </LOANS>
     </DOCUMENT>

在第二个文件中有序列 3 到 5。出现在两个文件中的序列 3 具有值为“2”的 LockAuthority 属性。如果值大于或等于文件 1 中的值,则值来自文件 2,否则值来自文件 1。我还想要合并 File1 中没有 LockAuthority 的任何数据点。如果没有 LockAuthority,则以 File2 中的数据为准。

File2.xml
   <DOCUMENT>
    <LOANS>
     <LOAN SequenceNumber="1" xlink:label="LOAN1" LoanRoleType="SubjectLoan">
       <DOCUMENT_SETS>
    <DOCUMENT_SET>
     <GFE>
      <SECTION_SUMMARIES>
       <SECTION_SUMMARY SequenceNumber="3" LockAuthority="2">
        <Amount>200.00</Amount>
        <Type>Header</Type>
       </SECTION_SUMMARY>
       <SECTION_SUMMARY SequenceNumber="4"> 
        <Amount>-3000.00</Amount>   
        <Type>Two</Type>                       
       </SECTION_SUMMARY>
       <SECTION_SUMMARY SequenceNumber="5">
        <Amount>5392.25</Amount>
        <Type>Three</Type>
       </SECTION_SUMMARY>
      </SECTION_SUMMARIES>
     </GFE>
    </DOCUMENT_SET>
       </DOCUMENT_SETS>
     </LOAN>
    </LOANS>
   </DOCUMENT>

生成的文件应如下所示。

   <DOCUMENT>
    <LOANS>
     <LOAN SequenceNumber="1" xlink:label="LOAN1" LoanRoleType="SubjectLoan">
       <DOCUMENT_SETS>
    <DOCUMENT_SET>
     <GFE>
      <SECTION_SUMMARIES>
       <SECTION_SUMMARY SequenceNumber="1">
        <Amount>1975.00</Amount>
        <Type>A</Type>
       </SECTION_SUMMARY>
       <SECTION_SUMMARY SequenceNumber="2"> 
        <Amount>9751.44</Amount>    
        <Type>B</Type>                     
       </SECTION_SUMMARY>
       <SECTION_SUMMARY SequenceNumber="3" LockAuthority="2">
        <Amount>200.00</Amount>
        <Type>Header</Type>
       </SECTION_SUMMARY>
       <SECTION_SUMMARY SequenceNumber="4"> 
        <Amount>-3000.00</Amount>   
        <Type>Two</Type>                       
       </SECTION_SUMMARY>
       <SECTION_SUMMARY SequenceNumber="5">
        <Amount>5392.25</Amount>
        <Type>Three</Type>
       </SECTION_SUMMARY>
      </SECTION_SUMMARIES>
     </GFE>
    </DOCUMENT_SET>
       </DOCUMENT_SETS>
       <ESCROW>
    <ESCROW_DETAIL>
     <InsuranceIndicator>true</InsuranceIndicator>
     <AllPropertyTaxesIndicator>true</AllPropertyTaxesIndicator>
     <OtherDescription>HOA</OtherDescription>
    </ESCROW_DETAIL>
       </ESCROW>
     </LOAN>
    </LOANS>
   </DOCUMENT>

非常感谢任何和所有帮助!

4

1 回答 1

0

这是简单的解决方案(可以通过使用 xsl:key 进行改进)。

 <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" indent="yes"/>

    <xsl:variable name="f2" select="document('ic4_2.xml')"/>
    <xsl:variable name="SECTION_SUMMARY_F1" select="//SECTION_SUMMARY"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="SECTION_SUMMARY" mode="copy">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>

    </xsl:template>

    <xsl:template match="SECTION_SUMMARY[@LockAuthority]">
        <xsl:variable name="SECTION_SUMMARY_F2" select="$f2//SECTION_SUMMARY[@SequenceNumber = current()/@SequenceNumber]" />
        <xsl:choose>
            <xsl:when test="$SECTION_SUMMARY_F2/@LockAuthority > @LockAuthority">
                <xsl:apply-templates select="$SECTION_SUMMARY_F2" mode="copy"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates select="." mode="copy"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="SECTION_SUMMARIES">
        <xsl:copy>
            <!-- from file 1-->
            <xsl:apply-templates />
            <!-- from file 2-->
            <xsl:for-each select="$f2//SECTION_SUMMARY[not(@SequenceNumber = $SECTION_SUMMARY_F1/@SequenceNumber)]" >
                <xsl:copy-of select="."/>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="/" >
        <xsl:apply-templates />
    </xsl:template>

</xsl:stylesheet>
于 2013-06-04T20:41:59.223 回答