Xml XSLT替换属性
我有以下xmlXml XSLT替换属性,xml,xslt,xpath,Xml,Xslt,Xpath,我有以下xml <ExternalAssessmentRequest> <ApplicationData Lender="Test"> <LiabilityList> <RequestedLoan Identifier="New1" BaseAmount="250000" LoanAccountFees="100" LoanAccountLMI="2000" LoanTerm="25" LoanTermM
<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创建两个独立的元素。一个是现有的,另一个也是现有的,但只能替换中指定的元素和属性
因此,最终结果应该是
<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.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!
t:\ftemp>type attrs.xml
t:\ftemp>调用xslt attrs.xml attrs.xsl
t:\ftemp>类型attrs.xsl
t:\ftemp>rem完成!
此答案解决了最初未说明的要求,即
有许多任意命名的子元素:
t:\ftemp>type attrs.xml
t:\ftemp>调用xslt attrs.xml attrs.xsl
t:\ftemp>类型attrs.xsl
t:\ftemp>rem完成!
非常感谢您的回答……只是一件小事。您只在第二个ApplicationData元素中复制了RequestedLoan元素。我需要从原始ApplicationData复制所有内容,但只更改在中指定的元素。很抱歉,我知道我的问题中没有包括此内容:(申请数据中有所有其他元素,也应该复制。如果你没有充分说明你的要求,你会为志愿者做额外的工作。请找到我刚刚发布的另一个解决方案。我非常感谢你的回答,因此很抱歉给你带来不便。这不符合你的要求,因为,同样,你做到了没有完全说明您需要什么。您正在为志愿者做更多的额外工作。ApplicationData中除了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!