我有以下XML

<ExternalAssessmentRequest>

</ExternalAssessmentRequest>


非常感谢您的帮助,如果有人可以帮助我。
问候

最佳答案

答案的XSLT 1.0版本,未解决与其他答案讨论的名称空间问题。简化的假设是,旧名称和新名称的列表不会更改名称空间前缀,并且使用与输入源文件相同的名称空间前缀。

t:\ftemp>type plural.xml
<ExternalAssessmentRequest>
    <ApplicationData Lender="MegaBank">
        <LiabilityList>
            <RequestedLoans>
                <Applicants Percentage="0.5" RelatedIdentifier="Applicant1"/>
                <Applicants Percentage="0.5" RelatedIdentifier="Applicant2"/>
                <Features Code="Test"/>
            </RequestedLoans>
            <ExistingLoans>
                <Securities RelatedIdentifier="Test"/>
                <Applicants Percentage="0.5" RelatedIdentifier="Applicant1"/>
                <Applicants Percentage="0.5" RelatedIdentifier="Applicant2"/>
            </ExistingLoans>
            <OtherLiabilities >
                <Applicants Percentage="0.5" RelatedIdentifier="Applicant1"/>
                <Applicants Percentage="0.5" RelatedIdentifier="Applicant2"/>
            </OtherLiabilities>
            <Expenses Amount="50" Description="Train Ticket" Identifier="Expense1" NonRecurring="Yes" Type="Transport" Year="2013">
                <Applicants Percentage="0.5" RelatedIdentifier="Applicant1"/>
                <Applicants Percentage="0.5" RelatedIdentifier="Applicant2"/>
            </Expenses>
        </LiabilityList>
        <AssetList>
            <Assets >
                <Applicants Percentage="0.5" RelatedIdentifier="Applicant1"/>
                <Applicants Percentage="0.5" RelatedIdentifier="Applicant2"/>
            </Assets>
            <Funds Amount="1000" Description="Slush Fund" Identifier="Fund1"/>
        </AssetList>
        <IncomeList>
            <Incomes >
                <Applicants Percentage="0.5" RelatedIdentifier="Applicant1"/>
                <Applicants Percentage="0.5" RelatedIdentifier="Applicant2"/>
            </Incomes>
        </IncomeList>
        <ApplicantList>
            <Households AdditionalAdults="0" Boarding="Yes" Children="0" Description="1 Test St, Sydney" Postcode="2000">
                <Persons CountryOfResidence="Australia" CustomerDurationInMonths="0" DischargedBankrupts="0" Identifier="Applicant1" Name="John Smith" Partner="Applicant2" Partnered="Yes" PermanentResident="Yes"/>
                <Persons CountryOfResidence="Australia" CustomerDurationInMonths="0" DischargedBankrupts="0" Identifier="Applicant2" Name="Jane Smith" Partner="Applicant1" Partnered="Yes" PermanentResident="Yes"/>
                <Guarantors/>
            </Households>
            <Companies Identifier="Company1" Name="Tardis">
                <Directors RelatedIdentifier="Applicant1"/>
            </Companies>
        </ApplicantList>
        <FeeList>
            <Fees Amount="100" Capitalised="Yes" DateOfPayment="1967-08-13" Description="Application Fee" Identifier="Fee1" PaidAmount="0"/>
        </FeeList>
    </ApplicationData>
    <AdditionalAssessments Lender="MegaBank">
        <RequestedLoans Product="Supa Variable" ProductID="Product2"/>
    </AdditionalAssessments>
</ExternalAssessmentRequest>
t:\ftemp>call xslt plural.xml plural.xsl
<?xml version="1.0" encoding="utf-8"?><ExternalAssessmentRequest>
    <ApplicationData Lender="MegaBank">
        <LiabilityList>
            <RequestedLoan>
                <Applicants Percentage="0.5" RelatedIdentifier="Applicant1"/>
                <Applicants Percentage="0.5" RelatedIdentifier="Applicant2"/>
                <Features Code="Test"/>
            </RequestedLoan>
            <ExistingLoans>
                <Securities RelatedIdentifier="Test"/>
                <Applicants Percentage="0.5" RelatedIdentifier="Applicant1"/>
                <Applicants Percentage="0.5" RelatedIdentifier="Applicant2"/>
            </ExistingLoans>
            <OtherLiabilities>
                <Applicants Percentage="0.5" RelatedIdentifier="Applicant1"/>
                <Applicants Percentage="0.5" RelatedIdentifier="Applicant2"/>
            </OtherLiabilities>
            <Expenses Amount="50" Description="Train Ticket" Identifier="Expense1" NonRecurring="Yes" Type="Transport" Year="2013">
                <Applicants Percentage="0.5" RelatedIdentifier="Applicant1"/>
                <Applicants Percentage="0.5" RelatedIdentifier="Applicant2"/>
            </Expenses>
        </LiabilityList>
        <AssetList>
            <Assets>
                <Applicants Percentage="0.5" RelatedIdentifier="Applicant1"/>
                <Applicants Percentage="0.5" RelatedIdentifier="Applicant2"/>
            </Assets>
            <Funds Amount="1000" Description="Slush Fund" Identifier="Fund1"/>
        </AssetList>
        <IncomeList>
            <Incomes>
                <Applicants Percentage="0.5" RelatedIdentifier="Applicant1"/>
                <Applicants Percentage="0.5" RelatedIdentifier="Applicant2"/>
            </Incomes>
        </IncomeList>
        <ApplicantList>
            <Households AdditionalAdults="0" Boarding="Yes" Children="0" Description="1 Test St, Sydney" Postcode="2000">
                <Persons CountryOfResidence="Australia" CustomerDurationInMonths="0" DischargedBankrupts="0" Identifier="Applicant1" Name="John Smith" Partner="Applicant2" Partnered="Yes" PermanentResident="Yes"/>
                <Persons CountryOfResidence="Australia" CustomerDurationInMonths="0" DischargedBankrupts="0" Identifier="Applicant2" Name="Jane Smith" Partner="Applicant1" Partnered="Yes" PermanentResident="Yes"/>
                <Guarantors/>
            </Households>
            <Company Identifier="Company1" Name="Tardis">
                <Directors RelatedIdentifier="Applicant1"/>
            </Company>
        </ApplicantList>
        <FeeList>
            <Fees Amount="100" Capitalised="Yes" DateOfPayment="1967-08-13" Description="Application Fee" Identifier="Fee1" PaidAmount="0"/>
        </FeeList>
    </ApplicationData>
    <AdditionalAssessments Lender="MegaBank">
        <RequestedLoan Product="Supa Variable" ProductID="Product2"/>
    </AdditionalAssessments>
</ExternalAssessmentRequest>
t:\ftemp>type plural.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:data="urn:X-data"
                exclude-result-prefixes="data"
                version="1.0">

<data:convert>
  <convert old="RequestedLoans" new="RequestedLoan"/>
  <convert old="Companies" new="Company"/>
  ...add more here...
</data:convert>

<xsl:key name="converts" match="convert" use="@old"/>

<xsl:template match="*[name(.)=document('')/*/data:convert/convert/@old]">
  <xsl:variable name="here" select="."/>
  <xsl:for-each select="document('')">
    <xsl:element name="{key('converts',name($here))/@new}"
                 namespace="{namespace-uri()}">
      <xsl:apply-templates select="$here/@*|$here/node()"/>
    </xsl:element>
  </xsl:for-each>
</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!

09-30 17:23
查看更多