2013-09-13 7 views
0

У меня есть следующий XMLXSLT заменить атрибуты

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

Pleaseeee помочь мне.

ответ

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! 
+0

Это не соответствует вашему требованию, потому что вы еще не полностью определили, что вам нужно сделать еще большую работу для добровольцев. –

+0

В ApplicationData будут другие элементы, кроме LiabilityList, есть и некоторые другие элементы, которые также должны быть скопированы – user1457881

+0

Я разместил более подробный вопрос здесь: http://stackoverflow.com/questions/18776631/xslt- replace-elemetns-and-attributes – user1457881

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! 
+0

Thanksss много за ответ .... только небольшая вещь. Вы только скопировали элемент RequestedLoan во втором элементе ApplicationData. Мне нужно скопировать все из исходной ApplicationData, но ТОЛЬКО изменить элементы, указанные в . – user1457881

+0

К сожалению, я знаю, что я не включил это в мой вопрос :( – user1457881

+0

Есть все эти другие элементы внутри данных applciation, которые должны быть скопированы aswel – user1457881

Смежные вопросы