xml - 更改元素名称的样式表

标签 xml xslt xpath

我有以下 XML

<ExternalAssessmentRequest>

</ExternalAssessmentRequest>

如果有人可以帮助我,真的很感谢你的帮助。
问候

最佳答案

这是一个带有查找表的解决方案,您可以自己填写:

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 xslt2 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:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:data="urn:X-data"
                exclude-result-prefixes="xsd data"
                version="2.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="*[key('converts',name(.),document(''))]">
  <xsl:element name="{key('converts',name(.),document(''))/@new}"
               namespace="{namespace-uri()}">
    <xsl:apply-templates select="@*,node()"/>
  </xsl:element>
</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! 

编辑以确保在一般解决方案中保留命名空间,而不是假设元素不在命名空间中。感谢 Mads 在他发布的解决方案中首先这样做。

关于xml - 更改元素名称的样式表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18884199/

相关文章:

Java DOM : transforming a node to a root element

xslt - 如何使用 XSLT 添加父节点

Java-XPath 生成父子结构

c# - "all nodes exactly one below the base node?"的正确 XPath 是什么

mysql - 存储自定义 MySQL 元数据 - 最佳实践

sql-server - 为什么 OPENXML 只返回一个元素

xml - 如何将 XML 插入现有 XML 节点

xml - 如何选择唯一节点

css - 如何找到 div 元素的深度标签的 xpaths?

php - 使用 php + xpath 以文本形式获取节点值