XML转换在不同的部分中复制一次元素两次

时间:2016-01-14 10:52:14

标签: xml xslt copy transformation


我有一个XML需要一些转换才能正确导入我们的数据库。我做了大部分更改,但是我在复制一个元素时遇到了问题(ApprovedCountry标记中的E100customerKey)。不知道如何在这部分文件中复制它。请参阅以下代码:

从合作伙伴收到的示例代码:

<?xml version="1.0" encoding="UTF-8"?>
<CustomerList>
    <TransferHeader>
        <Apllication>ApllicationForTransferCustomer</Apllication>
        <Sender>Partenr</Sender>
        <MailAdr>mail@mail.pl</MailAdr>
        <GeneratedDate>20160114</GeneratedDate>
        <NumberOfCustomers>3</NumberOfCustomers>
    </TransferHeader>
    <CustomerList>
        <Customer>
            <E100customerKey>03-98</E100customerKey>
            <VATNumber>2222222222</VATNumber>
            <EUVATNumber>11111111111</EUVATNumber>
            <CustomerName1>CustomerName</CustomerName1>
            <LineOfBusiness>uslugi transportowe</LineOfBusiness>
            <NACEcode>4941</NACEcode>
            <RequiredForPrivateCompany/>
            <ApprovedCountriesList>
                <ApprovedCountry>
                    <CountryCode>ES</CountryCode>
                    <ServiceType>Normal</ServiceType>
                    <PartnerPercent>3</PartnerPercent>
                </ApprovedCountry>
            </ApprovedCountriesList>
        </Customer>
    </CustomerList>
</CustomerList>

需要的代码:

<?xml version="1.0" encoding="UTF-8"?>
<CustomerList>
    <CustomerList>
        <Customer>
            <E100customerKey>03-98</E100customerKey>
            <VATNumber>2222222222</VATNumber>
            <EUVATNumber>11111111111</EUVATNumber>
            <CustomerName1>CustomerName</CustomerName1>
            <LineOfBusiness>uslugi transportowe</LineOfBusiness>
            <NACEcode>4941</NACEcode>
            <ApprovedCountriesList>
                <ApprovedCountry>
                    <E100customerKey>03-98</E100customerKey>
                    <CountryCode>ES</CountryCode>
                    <ServiceType>Normal</ServiceType>
                    <PartnerPercent>3</PartnerPercent>
                </ApprovedCountry>
            </ApprovedCountriesList>
        </Customer>
    </CustomerList>
</CustomerList>

到目前为止我的XSL文件:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes" encoding="UTF-8" />

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

<!-- Probably needs to fix this part -->
 <xsl:template match="CustomerList/CustomerList/Customer/ApprovedCountriesList/ApprovedCountry">
  </xsl:template>

 <xsl:template match="CustomerList/TransferHeader|RequiredForPrivateCompany"/>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:5)

尝试:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="TransferHeader | RequiredForPrivateCompany"/>

<xsl:template match="ApprovedCountry">
    <xsl:copy>
        <xsl:copy-of select="../../E100customerKey"/>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>