我有以下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>
</ApplicationData>
<AdditionalAssessment Lender="MegaBank">
<RequestedLoan Product="Supa Variable" ProductID="Product2"/>
</AdditionalAssessment>
</ExternalAssessmentRequest>
我需要使用Xpath创建两个单独的元素。一个是现有的,另一个是现有的BUT,只有必须替换指定的元素和属性。
所以最终结果应该是
<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>
</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>
</ApplicationData>
</ExternalAssessmentRequest>
请帮助我。
答案 0 :(得分:0)
答案依赖于知道在开始添加元素的内容之前可以多次重新定义元素的属性值。在我的解决方案中,我复制数据属性,然后只需使用其他属性复制它们。
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>
</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>
</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></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">
<xsl:template match="AdditionalAssessment">
<ApplicationData>
<!--preserve data attributes-->
<xsl:copy-of select="/*/ApplicationData/@*"/>
<!--override with additional attributes-->
<xsl:copy-of select="@*"/>
<LiabilityList>
<RequestedLoan>
<!--preserve data attributes-->
<xsl:copy-of
select="/*/ApplicationData/LiabilityList/RequestedLoan/@*"/>
<!--override with additional attributes-->
<xsl:copy-of select="RequestedLoan/@*"/>
<!--preserve data descendants-->
<xsl:copy-of
select="/*/ApplicationData/LiabilityList/RequestedLoan/node()"/>
</RequestedLoan>
</LiabilityList>
</ApplicationData>
</xsl:template>
<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!
答案 1 :(得分:0)
这个答案解决了最初未说明的要求,即<LiabilityList>
有一些任意命名的子元素:
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>
</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>
</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></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">
<xsl:template match="AdditionalAssessment">
<ApplicationData>
<!--preserve data attributes-->
<xsl:copy-of select="/*/ApplicationData/@*"/>
<!--override with additional attributes-->
<xsl:copy-of select="@*"/>
<LiabilityList>
<!--remember the location of the additional assessment information-->
<xsl:variable name="additional" select="."/>
<!--preserve each of the items in the liability list-->
<xsl:for-each select="/*/ApplicationData/LiabilityList/*">
<!--preserve data element-->
<xsl:copy>
<!--preserve data attributes-->
<xsl:copy-of select="@*"/>
<!--override with additional attributes-->
<xsl:copy-of select="$additional/*[name(.)=name(current())]/@*"/>
<!--preserve data descendants-->
<xsl:copy-of select="node()"/>
</xsl:copy>
</xsl:for-each>
</LiabilityList>
</ApplicationData>
</xsl:template>
<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!