0

我有以下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 创建两个单独的元素。一个是现有的,另一个是现有的,但只有在中指定的元素和属性必须被替换。

所以最终结果应该是

<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>

请帮帮我。

4

2 回答 2

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! 
于 2013-09-13T00:30:03.950 回答
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! 
于 2013-09-13T01:24:17.673 回答