2013-10-02 42 views
1

我有下面的XMLXSL身份複製

<ExternalAssessmentRequest > 
    <ApplicationData Lender="TEST"> 
     <LiabilityList> 
      <RequestedLoan Identifier="New1" BaseAmount="250000"> 
       <Applicant RelatedIdentifier="Applicant1" Percentage="0.5"/> 
       <Applicant RelatedIdentifier="Applicant2" Percentage="0.5"/> 
       <Feature Code="SupaPackage"/> 
      </RequestedLoan> 
      <ExistingLoan Identifier="Existing1" Clearing="Yes" > 
       <Security RelatedIdentifier="Asset1"/> 
       <Applicant RelatedIdentifier="Applicant1" Percentage="0.5"/> 
       <Applicant RelatedIdentifier="Applicant2" Percentage="0.5"/> 
      </ExistingLoan> 
      <OtherLiability Identifier="Liability1" CreditCard="Yes" > 
       <Applicant RelatedIdentifier="Applicant1" Percentage="0.5"/> 
       <Applicant RelatedIdentifier="Applicant2" Percentage="0.5"/> 
      </OtherLiability> 
      <Expense Identifier="Expense1" Type="Transport" > 
       <Applicant RelatedIdentifier="Applicant1" Percentage="0.5"/> 
       <Applicant RelatedIdentifier="Applicant2" Percentage="0.5"/> 
      </Expense> 
     </LiabilityList> 
     <AssetList> 
      <Asset Identifier="Asset1" Security="Yes" > 
       <Applicant RelatedIdentifier="Applicant1" Percentage="0.5"/> 
       <Applicant RelatedIdentifier="Applicant2" Percentage="0.5"/> 
      </Asset> 
      <Fund Identifier="Fund1" Amount="1000" Description="Slush Fund"/> 
     </AssetList> 
     <IncomeList> 
      <Income Identifier="Income1" GrossAmount="80000" > 
       <Applicant RelatedIdentifier="Applicant1" Percentage="0.5"/> 
       <Applicant RelatedIdentifier="Applicant2" Percentage="0.5"/> 
      </Income> 
     </IncomeList> 
     <ApplicantList> 
      <Household Children="0" AdditionalAdults="0" Postcode="2000" Description="1 Test St, Sydney" Boarding="Yes"> 
       <Person Identifier="Applicant1" Name="John Test" /> 
       <Person Identifier="Applicant2" Name="Jane Test" /> 
      </Household> 
      <Company Identifier="Company1" Name="Tardis"> 
       <Director RelatedIdentifier="Applicant1"/> 
      </Company> 
     </ApplicantList> 
     <FeeList> 
      <Fee Identifier="Fee1" Amount="100" PaidAmount="0" /> 
     </FeeList> 
    </ApplicationData> 
    <AdditionalAssessment Lender="TESTLender"> 
     <RequestedLoan Product="Supa Variable" ProductID="ProductA"/> 
    </AdditionalAssessment> 
    <AdditionalAssessment Lender="TEST1"> 
     <RequestedLoan Product="Supa Variable" ProductID="ProductB"/> 
    </AdditionalAssessment> 
    <AdditionalAssessment Lender="TEST2"> 
     <RequestedLoan Product="Supa Variable" ProductID="ProductC"/> 
    </AdditionalAssessment> 
    <AdditionalAssessment Lender="TEST3"> 
     <RequestedLoan Product="Supa Variable" ProductID="ProductD"/> 
    </AdditionalAssessment> 
    <AdditionalAssessment Lender="TEST4"> 
     <RequestedLoan Product="Supa Variable" ProductID="ProductD"/> 
    </AdditionalAssessment> 
</ExternalAssessmentRequest> 

,我有這個樣式表:

<?xml version="1.0" encoding="ascii"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xsl"> 
    <xsl:output omit-xml-declaration="yes" method="xml" encoding="UTF-8"/> 
    <!--remember the location of the additional assessment information--> 
    <xsl:variable name="additional" select="/*/AdditionalAssessment"/> 
    <xsl:template match="ApplicationData"> 
     <!--preserve original data--> 
     <xsl:copy-of select="."/> 
     <!--now make duplicate--> 
     <ApplicationData> 
      <!--preserve data attributes--> 
      <xsl:copy-of select="@*"/> 
      <!--override with additional attributes--> 
      <xsl:copy-of select="$additional/@*"/> 
      <!--process children looking for modifications to attributes--> 
      <xsl:apply-templates select="node()" mode="overrideAttributes"/> 
     </ApplicationData> 
    </xsl:template> 
    <!--copy the element and override attributes form the additional assessment--> 
    <xsl:template match="*" mode="overrideAttributes"> 
     <xsl:copy> 
      <!--preserve data attributes--> 
      <xsl:copy-of select="@*"/> 
      <!--override with additional attributes--> 
      <xsl:copy-of select="$additional/*[name(.)=name(current())]/@*"/> 
      <!--manipulate the descendants--> 
      <xsl:apply-templates mode="overrideAttributes"/> 
     </xsl:copy> 
    </xsl:template> 
    <!--remove the original additional assessment--> 
    <xsl:template match="AdditionalAssessment"/> 
    <xsl:template match="@*|node()"> 
     <!--identity for all other nodes--> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="/ExternalAssessmentRequest"> 
     <xsl:element name="ApplicationDataBatch"> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 

將會產生以下的輸出:

<ApplicationDataBatch> 
    <ApplicationData Lender="TEST"> 
     <LiabilityList> 
      <RequestedLoan Identifier="New1" BaseAmount="250000"> 
       <Applicant RelatedIdentifier="Applicant1" Percentage="0.5"/> 
       <Applicant RelatedIdentifier="Applicant2" Percentage="0.5"/> 
       <Feature Code="SupaPackage"/> 
      </RequestedLoan> 
      <ExistingLoan Identifier="Existing1" Clearing="Yes"> 
       <Security RelatedIdentifier="Asset1"/> 
       <Applicant RelatedIdentifier="Applicant1" Percentage="0.5"/> 
       <Applicant RelatedIdentifier="Applicant2" Percentage="0.5"/> 
      </ExistingLoan> 
      <OtherLiability Identifier="Liability1" CreditCard="Yes"> 
       <Applicant RelatedIdentifier="Applicant1" Percentage="0.5"/> 
       <Applicant RelatedIdentifier="Applicant2" Percentage="0.5"/> 
      </OtherLiability> 
      <Expense Identifier="Expense1" Type="Transport"> 
       <Applicant RelatedIdentifier="Applicant1" Percentage="0.5"/> 
       <Applicant RelatedIdentifier="Applicant2" Percentage="0.5"/> 
      </Expense> 
     </LiabilityList> 
     <AssetList> 
      <Asset Identifier="Asset1" Security="Yes"> 
       <Applicant RelatedIdentifier="Applicant1" Percentage="0.5"/> 
       <Applicant RelatedIdentifier="Applicant2" Percentage="0.5"/> 
      </Asset> 
      <Fund Identifier="Fund1" Amount="1000" Description="Slush Fund"/> 
     </AssetList> 
     <IncomeList> 
      <Income Identifier="Income1" GrossAmount="80000"> 
       <Applicant RelatedIdentifier="Applicant1" Percentage="0.5"/> 
       <Applicant RelatedIdentifier="Applicant2" Percentage="0.5"/> 
      </Income> 
     </IncomeList> 
     <ApplicantList> 
      <Household Children="0" AdditionalAdults="0" Postcode="2000" Description="1 Test St, Sydney" Boarding="Yes"> 
       <Person Identifier="Applicant1" Name="John Test"/> 
       <Person Identifier="Applicant2" Name="Jane Test"/> 
      </Household> 
      <Company Identifier="Company1" Name="Tardis"> 
       <Director RelatedIdentifier="Applicant1"/> 
      </Company> 
     </ApplicantList> 
     <FeeList> 
      <Fee Identifier="Fee1" Amount="100" PaidAmount="0"/> 
     </FeeList> 
    </ApplicationData> 
    <ApplicationData Lender="TEST4"> 
     <LiabilityList> 
      <RequestedLoan Identifier="New1" BaseAmount="250000" Product="Supa Variable" ProductID="ProductD"> 
       <Applicant RelatedIdentifier="Applicant1" Percentage="0.5"/> 
       <Applicant RelatedIdentifier="Applicant2" Percentage="0.5"/> 
       <Feature Code="SupaPackage"/> 
      </RequestedLoan> 
      <ExistingLoan Identifier="Existing1" Clearing="Yes"> 
       <Security RelatedIdentifier="Asset1"/> 
       <Applicant RelatedIdentifier="Applicant1" Percentage="0.5"/> 
       <Applicant RelatedIdentifier="Applicant2" Percentage="0.5"/> 
      </ExistingLoan> 
      <OtherLiability Identifier="Liability1" CreditCard="Yes"> 
       <Applicant RelatedIdentifier="Applicant1" Percentage="0.5"/> 
       <Applicant RelatedIdentifier="Applicant2" Percentage="0.5"/> 
      </OtherLiability> 
      <Expense Identifier="Expense1" Type="Transport"> 
       <Applicant RelatedIdentifier="Applicant1" Percentage="0.5"/> 
       <Applicant RelatedIdentifier="Applicant2" Percentage="0.5"/> 
      </Expense> 
     </LiabilityList> 
     <AssetList> 
      <Asset Identifier="Asset1" Security="Yes"> 
       <Applicant RelatedIdentifier="Applicant1" Percentage="0.5"/> 
       <Applicant RelatedIdentifier="Applicant2" Percentage="0.5"/> 
      </Asset> 
      <Fund Identifier="Fund1" Amount="1000" Description="Slush Fund"/> 
     </AssetList> 
     <IncomeList> 
      <Income Identifier="Income1" GrossAmount="80000"> 
       <Applicant RelatedIdentifier="Applicant1" Percentage="0.5"/> 
       <Applicant RelatedIdentifier="Applicant2" Percentage="0.5"/> 
      </Income> 
     </IncomeList> 
     <ApplicantList> 
      <Household Children="0" AdditionalAdults="0" Postcode="2000" Description="1 Test St, Sydney" Boarding="Yes"> 
       <Person Identifier="Applicant1" Name="John Test"/> 
       <Person Identifier="Applicant2" Name="Jane Test"/> 
      </Household> 
      <Company Identifier="Company1" Name="Tardis"> 
       <Director RelatedIdentifier="Applicant1"/> 
      </Company> 
     </ApplicantList> 
     <FeeList> 
      <Fee Identifier="Fee1" Amount="100" PaidAmount="0"/> 
     </FeeList> 
    </ApplicationData> 
</ApplicationDataBatch> 

一切都是理所應當只有一件事是缺失的。 我需要爲每個AdditionalAssessment節點創建一個ApplicationData節點。當前樣式表只創建一個而不是5(+1原始應用程序數據)。 樣式表需要爲每個AdditionalAssessment節點創建ApplicationData節點,只需更改每個附加評估中指定的元素即可。

回答

1

假設原始文檔只有一個ApplicationData元素是否安全?如果是這樣,那麼這應該做的伎倆:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       exclude-result-prefixes="xsl"> 
    <xsl:output omit-xml-declaration="yes" method="xml" encoding="UTF-8"/> 

    <xsl:template match="@*|node()"> 
    <!--identity for all other nodes--> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="/ExternalAssessmentRequest"> 
    <ApplicationDataBatch> 
     <xsl:apply-templates select="@*|node()"/> 
    </ApplicationDataBatch> 
    </xsl:template> 

    <xsl:template match="AdditionalAssessment"> 
    <xsl:apply-templates select ="../ApplicationData" mode="overrideAttributes"> 
     <xsl:with-param name="additional" select="." /> 
    </xsl:apply-templates> 
    </xsl:template> 

    <!--copy the element and override attributes form the additional assessment--> 
    <xsl:template match="*" mode="overrideAttributes"> 
    <xsl:param name="additional" /> 

    <xsl:variable name="currentIsAD" select="self::ApplicationData" /> 

    <xsl:copy> 
     <!--preserve data attributes--> 
     <xsl:copy-of select="@*"/> 
     <!--override with additional attributes--> 
     <xsl:copy-of select="$additional[$currentIsAD]/@*"/> 

     <xsl:copy-of 
      select="$additional[not($currentIsAD)]/*[name(.)=name(current())]/@*"/> 
     <!--manipulate the descendants--> 
     <xsl:apply-templates mode="overrideAttributes"> 
     <xsl:with-param name="additional" select="$additional" /> 
     </xsl:apply-templates> 
    </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 
2

這XSLT樣式表似乎做你想要什麼:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xsl"> 
    <xsl:output omit-xml-declaration="yes" method="xml" encoding="UTF-8"/> 

    <!-- The identity transform. --> 
    <xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
    </xsl:template> 

    <!-- When we get to AdditionalAssessment elements, match the ApplicationData element again in override-attributes 
     mode, passing through the AdditionalAssessment element so we can copy things from it. --> 
    <xsl:template match="AdditionalAssessment"> 
    <xsl:apply-templates select="../ApplicationData" mode="override-attributes"> 
     <xsl:with-param name="additional" select="."/> 
    </xsl:apply-templates> 
    </xsl:template> 

    <!-- Copying out elements and modifying their attributes according to the 'additional' element we pass in as a 
     parameter. --> 
    <xsl:template match="node()|@*" mode="override-attributes"> 
    <xsl:param name="additional" /> 
    <xsl:copy> 
     <!-- Preserve data attributes. --> 
     <xsl:copy-of select="@*"/> 

     <!--Override with additional attributes. --> 
     <xsl:copy-of select="$additional/*[name(.)=name(current())]/@*"/> 

     <!-- Override the lender if this is the ApplicationData element. A bit annoying to have to do this, but I couldn't 
      get it to work in a nicer way. --> 
     <xsl:if test="self::ApplicationData"> 
     <xsl:copy-of select="$additional/@Lender"/> 
     </xsl:if> 

     <!-- Manipulate the descendants, excluding the attributes because we've already copied/overridden them. --> 
     <xsl:apply-templates select="node()" mode="override-attributes"> 
     <xsl:with-param name="additional" select="$additional"/> 
     </xsl:apply-templates> 
    </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet>