如何使用xslt删除重复的根元素

时间:2014-10-13 04:46:27

标签: xslt xslt-1.0 xslt-2.0

我需要使用XSLT从我的XML中删除重复的根标记。

我的XML看起来像这样:

<Invoice xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 UBLTR-Invoice-2.0.xsd">
<Invoice>
    <cbc:UBLVersionID>2.0</cbc:UBLVersionID>
    <cbc:CustomizationID>TR1.0</cbc:CustomizationID>
    <cbc:ProfileID>TEMELFATURA</cbc:ProfileID>
    <cbc:ID>MC201409221048</cbc:ID>
    <cbc:CopyIndicator>false</cbc:CopyIndicator>
    <cbc:UUID>4decdc2d-b712-4d57-b909-b7705d05ae3b</cbc:UUID>
    <cbc:IssueDate>2009-01-05</cbc:IssueDate>
    <cbc:InvoiceTypeCode>SATIS</cbc:InvoiceTypeCode>
    <cbc:DocumentCurrencyCode>TRL</cbc:DocumentCurrencyCode>
    <cbc:TaxCurrencyCode>TRL</cbc:TaxCurrencyCode>
    <cbc:PricingCurrencyCode>TRL</cbc:PricingCurrencyCode>
    <cbc:PaymentCurrencyCode>TRL</cbc:PaymentCurrencyCode>
    <cbc:LineCountNumeric>1</cbc:LineCountNumeric>
    <cac:InvoicePeriod>
        <cbc:StartDate>2008-12-05</cbc:StartDate>
        <cbc:EndDate>2009-01-05</cbc:EndDate>
    </cac:InvoicePeriod>
    <cac:AccountingSupplierParty>
        <cac:Party>
            <cbc:WebsiteURI>http://www.aaa.com.tr/</cbc:WebsiteURI>
            <cac:PartyIdentification>
                <cbc:ID schemeID="VKN">1288331521</cbc:ID>
            </cac:PartyIdentification>
            <cac:PartyName>
                <cbc:Name>AAA Anonim Åžirketi</cbc:Name>
            </cac:PartyName>
            <cac:PostalAddress>
                <cbc:StreetName>Papatya Caddesi Yasemin Sokak No: 21</cbc:StreetName>
                <cbc:CitySubdivisionName>BeÅŸiktaÅŸ</cbc:CitySubdivisionName>
                <cbc:CityName>Ä°stanbul</cbc:CityName>
                <cbc:PostalZone>34100</cbc:PostalZone>
                <cac:Country>
                    <cbc:IdentificationCode>TR</cbc:IdentificationCode>
                    <cbc:Name>Turkey</cbc:Name>
                </cac:Country>
            </cac:PostalAddress>
            <cac:Contact>
                <cbc:Telephone>(212) 925 51515</cbc:Telephone>
                <cbc:Telefax>(212) 925505015</cbc:Telefax>
                <cbc:ElectronicMail>aa@aaa.com.tr</cbc:ElectronicMail>
            </cac:Contact>
        </cac:Party>
    </cac:AccountingSupplierParty>
    <cac:AccountingCustomerParty>
        <cac:Party>
            <cbc:WebsiteURI>MyWebPage.tr</cbc:WebsiteURI>
            <cac:PartyIdentification>
                <cbc:ID schemeID="TCKN">12345678901</cbc:ID>
            </cac:PartyIdentification>
            <cac:PostalAddress>
                <cbc:StreetName>6. Sokak No.1/1</cbc:StreetName>
                <cbc:CitySubdivisionName>BeÅŸiktaÅŸ</cbc:CitySubdivisionName>
                <cbc:CityName>Ä°stanbul</cbc:CityName>
                <cbc:PostalZone>34100</cbc:PostalZone>
                <cac:Country>
                    <cbc:IdentificationCode>TR</cbc:IdentificationCode>
                    <cbc:Name>Turkey</cbc:Name>
                </cac:Country>
            </cac:PostalAddress>
            <cac:Contact>
                <cbc:ElectronicMail>1234567890@mydn.com.tr</cbc:ElectronicMail>
            </cac:Contact>
            <cac:Person>
                <cbc:FirstName>Ali</cbc:FirstName>
                <cbc:FamilyName>YILMAZ</cbc:FamilyName>
            </cac:Person>
        </cac:Party>
    </cac:AccountingCustomerParty>
    <cac:PaymentMeans>
        <cbc:PaymentMeansCode>1</cbc:PaymentMeansCode>
        <cbc:PaymentDueDate>2009-01-20</cbc:PaymentDueDate>
        <cac:PayeeFinancialAccount>
            <cbc:ID>TR5652214414</cbc:ID>
            <cbc:CurrencyCode>TRL</cbc:CurrencyCode>
            <cbc:PaymentNote>RRR Bankası Beşiktaş Şubesi TL Hesabı</cbc:PaymentNote>
        </cac:PayeeFinancialAccount>
    </cac:PaymentMeans>
    <cac:PaymentTerms>
        <cbc:Note>BBB Bank Otomatik Ödeme</cbc:Note>
    </cac:PaymentTerms>
    <cac:TaxTotal>
        <cbc:TaxAmount currencyID="TRL">0.00</cbc:TaxAmount>
        <cac:TaxSubtotal>
            <cbc:TaxableAmount currencyID="TRL">15.15</cbc:TaxableAmount>
            <cac:TaxCategory>
                <cac:TaxScheme>
                    <cbc:Name>0000</cbc:Name>
                </cac:TaxScheme>
            </cac:TaxCategory>
        </cac:TaxSubtotal>
        <cac:TaxSubtotal>
            <cbc:TaxableAmount currencyID="TRL">15.15</cbc:TaxableAmount>
            <cbc:Percent>0.18</cbc:Percent>
        </cac:TaxSubtotal>
    </cac:TaxTotal>
    <cac:LegalMonetaryTotal>
        <cbc:LineExtensionAmount currencyID="TRL">15.15</cbc:LineExtensionAmount>
        <cbc:TaxExclusiveAmount currencyID="TRL">15.15</cbc:TaxExclusiveAmount>
        <cbc:TaxInclusiveAmount currencyID="TRL">15.15</cbc:TaxInclusiveAmount>
        <cbc:AllowanceTotalAmount>0.00</cbc:AllowanceTotalAmount>
        <cbc:ChargeTotalAmount>0.00</cbc:ChargeTotalAmount>
        <cbc:PayableAmount currencyID="TRL">15.15</cbc:PayableAmount>
    </cac:LegalMonetaryTotal>
    <cac:InvoiceLine>
        <cbc:ID>1</cbc:ID>
        <cbc:InvoicedQuantity unitCode="KWH">101</cbc:InvoicedQuantity>
        <cbc:LineExtensionAmount currencyID="TRL">15.15</cbc:LineExtensionAmount>
        <cac:AllowanceCharge>
            <cbc:ChargeIndicator>false</cbc:ChargeIndicator>
        </cac:AllowanceCharge>
        <cac:TaxTotal>
            <cbc:TaxAmount>0.00</cbc:TaxAmount>
            <cac:TaxSubtotal>
                <cbc:TaxableAmount currencyID="TRL">15.15</cbc:TaxableAmount>
                <cbc:TaxAmount currencyID="TRL">0.00</cbc:TaxAmount>
                <cbc:CalculationSequenceNumeric>1</cbc:CalculationSequenceNumeric>
                <cbc:TransactionCurrencyTaxAmount currencyID="TRL">0</cbc:TransactionCurrencyTaxAmount>
                <cbc:Percent>0.18</cbc:Percent>
            </cac:TaxSubtotal>
        </cac:TaxTotal>
        <cac:Item>
            <cbc:Description languageID="tr">Elektrik Tüketim Bedeli</cbc:Description>
            <cbc:Name languageID="tr">Elektrik Tüketim Bedeli</cbc:Name>
            <cac:BuyersItemIdentification>
                <cbc:ID schemeID="Customer">ItemID Customer</cbc:ID>
            </cac:BuyersItemIdentification>
            <cac:SellersItemIdentification>
                <cbc:ID schemeID="Supplier">ItemID12345</cbc:ID>
            </cac:SellersItemIdentification>
            <cac:ManufacturersItemIdentification>
                <cbc:ID schemeID="Manufacturer">ItemID Manufacturer</cbc:ID>
            </cac:ManufacturersItemIdentification>
        </cac:Item>
        <cac:Price>
            <cbc:PriceAmount currencyID="TRL">0.15</cbc:PriceAmount>
        </cac:Price>
    </cac:InvoiceLine>
</Invoice>

如果您可以看到根元素<Invoice>重复了两次。

我的XSLT是:

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 UBLTR-Invoice-2.0.xsd"><xsl:output encoding="UTF-8" indent="yes" method="xml" version="1.0"/><xsl:template match="*[Invoice]"><xsl:apply-templates/></xsl:template><xsl:template match="node()|@*"><xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy></xsl:template></xsl:stylesheet>

我找不到任何与我有相同问题的参考文献。 我希望你能帮助我。谢谢!

1 个答案:

答案 0 :(得分:0)

以这种方式尝试:

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="/*">
    <xsl:apply-templates select="@*|node()"/>
</xsl:template>

</xsl:stylesheet>

请注意,只有一个根元素。另一个Invoice元素嵌套在其中。

假设根本发票中只有一个元素嵌套;否则你将得到一个格式错误的XML输出。