样式表更改elment的名称

时间:2013-09-18 23:49:06

标签: xml xslt xpath

我有以下XML

<ExternalAssessmentRequest>

</ExternalAssessmentRequest>

如果有人能帮助我,真的很感谢你的帮助。 此致

4 个答案:

答案 0 :(得分:2)

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

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在他发布的解决方案中首先做到这一点。

答案 1 :(得分:1)

您没有显示您已经尝试过的内容。我们再一次不知道您是否在向我们寻求指导,或者您是否要求我们为您工作。

以下示例对复数作出以下假设,如果您有更多,则由您决定:

  • “ies” - &gt; “y” 的
  • “s” - &gt; “”

如果你列举了你想要容纳的所有“复数到单一”规则,那将会有所帮助。例如,如果您有一个名为<Class>的元素,那么下面的样式表将会破坏<Clas>。拼写中“复数”的概念根本不是很简单。我建议您使用查找表并枚举所需的更改。

同样,你的问题不完整,你需要额外的时间给志愿者提供帮助。

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>
                <Applicant Percentage="0.5" RelatedIdentifier="Applicant1"/>
                <Applicant Percentage="0.5" RelatedIdentifier="Applicant2"/>
                <Feature Code="Test"/>
            </RequestedLoan>
            <ExistingLoan>
                <Security RelatedIdentifier="Test"/>
                <Applicant Percentage="0.5" RelatedIdentifier="Applicant1"/>
                <Applicant Percentage="0.5" RelatedIdentifier="Applicant2"/>
            </ExistingLoan>
            <OtherLiability>
                <Applicant Percentage="0.5" RelatedIdentifier="Applicant1"/>
                <Applicant Percentage="0.5" RelatedIdentifier="Applicant2"/>
            </OtherLiability>
            <Expense Amount="50" Description="Train Ticket" Identifier="Expense1" NonRecurring="Yes" Type="Transport" Year="2013">
                <Applicant Percentage="0.5" RelatedIdentifier="Applicant1"/>
                <Applicant Percentage="0.5" RelatedIdentifier="Applicant2"/>
            </Expense>
        </LiabilityList>
        <AssetList>
            <Asset>
                <Applicant Percentage="0.5" RelatedIdentifier="Applicant1"/>
                <Applicant Percentage="0.5" RelatedIdentifier="Applicant2"/>
            </Asset>
            <Fund Amount="1000" Description="Slush Fund" Identifier="Fund1"/>
        </AssetList>
        <IncomeList>
            <Income>
                <Applicant Percentage="0.5" RelatedIdentifier="Applicant1"/>
                <Applicant Percentage="0.5" RelatedIdentifier="Applicant2"/>
            </Income>
        </IncomeList>
        <ApplicantList>
            <Household AdditionalAdults="0" Boarding="Yes" Children="0" Description="1 Test St, Sydney" Postcode="2000">
                <Person CountryOfResidence="Australia" CustomerDurationInMonths="0" DischargedBankrupts="0" Identifier="Applicant1" Name="John Smith" Partner="Applicant2" Partnered="Yes" PermanentResident="Yes"/>
                <Person CountryOfResidence="Australia" CustomerDurationInMonths="0" DischargedBankrupts="0" Identifier="Applicant2" Name="Jane Smith" Partner="Applicant1" Partnered="Yes" PermanentResident="Yes"/>
                <Guarantor/>
            </Household>
            <Company Identifier="Company1" Name="Tardis">
                <Director RelatedIdentifier="Applicant1"/>
            </Company>
        </ApplicantList>
        <FeeList>
            <Fee Amount="100" Capitalised="Yes" DateOfPayment="1967-08-13" Description="Application Fee" Identifier="Fee1" PaidAmount="0"/>
        </FeeList>
    </ApplicationData>
    <AdditionalAssessment Lender="MegaBank">
        <RequestedLoan Product="Supa Variable" ProductID="Product2"/>
    </AdditionalAssessment>
</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"
                exclude-result-prefixes="xsd"
                version="2.0">

<xsl:template match="*[ends-with(name(.),'ies')]" priority="1">
  <xsl:element name="{substring(name(.),1,string-length(name(.))-3)}y">
    <xsl:apply-templates select="@*,node()"/>
  </xsl:element>
</xsl:template>

<xsl:template match="*[ends-with(name(.),'s')]">
  <xsl:element name="{substring(name(.),1,string-length(name(.))-1)}">
    <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! 

答案 2 :(得分:1)

答案的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! 

答案 3 :(得分:0)

一种非常通用的XSLT 1.0解决方案,用于查找以s esies结尾的元素,并通过删除s和{生成元素名称的单数形式{1}}并将es替换为ies

y

更具体的样式表将指定您想要更改的每个元素名称,使用<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="*[substring(local-name(), string-length(local-name())) = 's']"> <xsl:element name="{substring(name(), 0, string-length(name()))}" namespace="{namespace-uri()}"> <xsl:apply-templates select="@*|node()"/> </xsl:element> </xsl:template> <xsl:template match="*[substring(local-name(), string-length(local-name())-1) = 'es']" priority="1"> <xsl:element name="{substring(name(), 0, string-length(name()))}" namespace="{namespace-uri()}"> <xsl:apply-templates select="@*|node()"/> </xsl:element> </xsl:template> <xsl:template match="*[substring(local-name(), string-length(local-name())-2) = 'ies']" priority="2"> <xsl:element name="{substring(name(), 0, string-length(name())-2)}y" namespace="{namespace-uri()}"> <xsl:apply-templates select="@*|node()"/> </xsl:element> </xsl:template> </xsl:stylesheet> union运算符匹配其中任何一个,并应用匹配模板中指定的逻辑来删除/更改结尾元素名称:

|