XSLT 替换元素和属性 [英] XSLT replace elemetns and attributes

查看:23
本文介绍了XSLT 替换元素和属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 xml

 <ExternalAssessmentRequest>

</ExternalAssessmentRequest>

请帮帮忙

推荐答案

这依赖于能够通过简单地复制属性来覆盖它们:

This relies on being able to override attributes simply by copying over them:

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!

这篇关于XSLT 替换元素和属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆