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 を使用して 2 つの個別の要素を作成する必要があります。1 つは既存のもので、もう 1 つは既存のものですが、で指定された要素と属性のみを置き換える必要があります。

したがって、最終結果は

<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 に答える