XSLT替换元素和属性

时间:2013-09-13 01:05:02

标签: xml xslt xpath

我有以下xml

 <ExternalAssessmentRequest>

</ExternalAssessmentRequest>

HElppppppppp请

1 个答案:

答案 0 :(得分:2)

这依赖于只需复制属性即可覆盖属性:

t:\ftemp>type attrs.xml 
 <ExternalAssessmentRequest>
    <ApplicationData Lender="Test">
        <LiabilityList>
            <RequestedLoan Identifier="New1" BaseAmount="250000" LoanAccountFees="100" LoanAccountLMI="2000" LoanTerm="25" LoanTermMonths="6" Product="Basic Variable" Repurposing="No" PrimaryPurpose="OwnerOccupied" TaxDeductible="No" InterestRate="0.075" ProductID="Product1" PaymentType="InterestOnly" ABSCode="123">
                <Applicant RelatedIdentifier="Applicant1" Percentage="0.5"/>
                <Applicant RelatedIdentifier="Applicant2" Percentage="0.5"/>
                <Feature Code="SupaPackage"/>
            </RequestedLoan>
        </LiabilityList>
        <AssetList>
            <Asset Identifier="Asset1" Security="Yes" Purpose="OwnerOccupied" Value="500000" Transaction="Owns" PurchasingPrice="0" PurchasingCosts="0" Priority="Second" PropertyType="String" Description="1 Test Street, Sydney" Basis="V" Type="DetachedHouse" PrimarySecurity="Yes" MaxLendingValLtdByGtee="0" ConstructionCosts="0" Postcode="2000" TitleType="Freehold" PropertyStatus="Established" OwnerBuilder="No" LandHectares="0">
                <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" Taxable="Yes" Type="Personal" Description="Rental Income" RentalAsset="Asset1" IncomeType="Rental" NonRecurring="No" Year="2013">
                <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 Smith" Partner="Applicant2" Partnered="Yes" CustomerDurationInMonths="0" PermanentResident="Yes" CountryOfResidence="Australia" DischargedBankrupts="0"/>
                <Person Identifier="Applicant2" Name="Jane Smith" Partner="Applicant1" Partnered="Yes" CustomerDurationInMonths="0" PermanentResident="Yes" CountryOfResidence="Australia" DischargedBankrupts="0"/>
            </Household>
            <Company Identifier="Company1" Name="Tardis">
                <Director RelatedIdentifier="Applicant1"/>
            </Company>
        </ApplicantList>
        <FeeList>
            <Fee Identifier="Fee1" Amount="100" PaidAmount="0" Description="Application Fee" DateOfPayment="1967-08-13" Capitalised="No"/>
        </FeeList>
    </ApplicationData>
    <AdditionalAssessment Lender="MegaBank">
        <RequestedLoan Product="Supa Variable" ProductID="Product2"/>
    </AdditionalAssessment>
</ExternalAssessmentRequest>
t:\ftemp>call xslt attrs.xml attrs.xsl 
<?xml version="1.0" encoding="utf-8"?><ExternalAssessmentRequest>
    <ApplicationData Lender="Test">
        <LiabilityList>
            <RequestedLoan Identifier="New1" BaseAmount="250000" LoanAccountFees="100" LoanAccountLMI="2000" LoanTerm="25" LoanTermMonths="6" Product="Basic Variable" Repurposing="No" PrimaryPurpose="OwnerOccupied" TaxDeductible="No" InterestRate="0.075" ProductID="Product1" PaymentType="InterestOnly" ABSCode="123">
                <Applicant RelatedIdentifier="Applicant1" Percentage="0.5"/>
                <Applicant RelatedIdentifier="Applicant2" Percentage="0.5"/>
                <Feature Code="SupaPackage"/>
            </RequestedLoan>
        </LiabilityList>
        <AssetList>
            <Asset Identifier="Asset1" Security="Yes" Purpose="OwnerOccupied" Value="500000" Transaction="Owns" PurchasingPrice="0" PurchasingCosts="0" Priority="Second" PropertyType="String" Description="1 Test Street, Sydney" Basis="V" Type="DetachedHouse" PrimarySecurity="Yes" MaxLendingValLtdByGtee="0" ConstructionCosts="0" Postcode="2000" TitleType="Freehold" PropertyStatus="Established" OwnerBuilder="No" LandHectares="0">
                <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" Taxable="Yes" Type="Personal" Description="Rental Income" RentalAsset="Asset1" IncomeType="Rental" NonRecurring="No" Year="2013">
                <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 Smith" Partner="Applicant2" Partnered="Yes" CustomerDurationInMonths="0" PermanentResident="Yes" CountryOfResidence="Australia" DischargedBankrupts="0"/>
                <Person Identifier="Applicant2" Name="Jane Smith" Partner="Applicant1" Partnered="Yes" CustomerDurationInMonths="0" PermanentResident="Yes" CountryOfResidence="Australia" DischargedBankrupts="0"/>
            </Household>
            <Company Identifier="Company1" Name="Tardis">
                <Director RelatedIdentifier="Applicant1"/>
            </Company>
        </ApplicantList>
        <FeeList>
            <Fee Identifier="Fee1" Amount="100" PaidAmount="0" Description="Application Fee" DateOfPayment="1967-08-13" Capitalised="No"/>
        </FeeList>
    </ApplicationData><ApplicationData Lender="MegaBank">
        <LiabilityList>
            <RequestedLoan Identifier="New1" BaseAmount="250000" LoanAccountFees="100" LoanAccountLMI="2000" LoanTerm="25" LoanTermMonths="6" Product="Supa Variable" Repurposing="No" PrimaryPurpose="OwnerOccupied" TaxDeductible="No" InterestRate="0.075" ProductID="Product2" PaymentType="InterestOnly" ABSCode="123">
                <Applicant RelatedIdentifier="Applicant1" Percentage="0.5"/>
                <Applicant RelatedIdentifier="Applicant2" Percentage="0.5"/>
                <Feature Code="SupaPackage"/>
            </RequestedLoan>
        </LiabilityList>
        <AssetList>
            <Asset Identifier="Asset1" Security="Yes" Purpose="OwnerOccupied" Value="500000" Transaction="Owns" PurchasingPrice="0" PurchasingCosts="0" Priority="Second" PropertyType="String" Description="1 Test Street, Sydney" Basis="V" Type="DetachedHouse" PrimarySecurity="Yes" MaxLendingValLtdByGtee="0" ConstructionCosts="0" Postcode="2000" TitleType="Freehold" PropertyStatus="Established" OwnerBuilder="No" LandHectares="0">
                <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" Taxable="Yes" Type="Personal" Description="Rental Income" RentalAsset="Asset1" IncomeType="Rental" NonRecurring="No" Year="2013">
                <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 Smith" Partner="Applicant2" Partnered="Yes" CustomerDurationInMonths="0" PermanentResident="Yes" CountryOfResidence="Australia" DischargedBankrupts="0"/>
                <Person Identifier="Applicant2" Name="Jane Smith" Partner="Applicant1" Partnered="Yes" CustomerDurationInMonths="0" PermanentResident="Yes" CountryOfResidence="Australia" DischargedBankrupts="0"/>
            </Household>
            <Company Identifier="Company1" Name="Tardis">
                <Director RelatedIdentifier="Applicant1"/>
            </Company>
        </ApplicantList>
        <FeeList>
            <Fee Identifier="Fee1" Amount="100" PaidAmount="0" Description="Application Fee" DateOfPayment="1967-08-13" Capitalised="No"/>
        </FeeList>
    </ApplicationData>

</ExternalAssessmentRequest>
t:\ftemp>type attrs.xsl 
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

<!--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:stylesheet>
t:\ftemp>rem Done!