jueves, 16 de mayo de 2019

How to remove namespaces from XML using XSLT

Sometimes, XML documents you receive could contain unneeded or incorrect namespace information. You can use XSLT stylesheets to remove the namespace information in these documents.

XML Input
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns1:reportRequest xmlns:ns1="http://v1.dto.reports.rolandopalermo.com">
    <ns1:metadata>
        <ns1:account>12345678</ns1:account>
        <ns1:reportName>Current_Payment</ns1:reportName>
    </ns1:metadata>
    <ns1:report>
        <ns1:reportId>1</ns1:reportId>
        <ns1:header>
            <ns1:id>999999999</ns1:id>
            <ns1:name>John Smith</ns1:name>
        </ns1:header>
        <ns1:invoices>
            <ns1:invoice>
                <ns1:invoiceNumber>0000000000</ns1:invoiceNumber>
                <ns1:dueDate>07/01/18</ns1:dueDate>
                <ns1:invoiceAmount>14.00</ns1:invoiceAmount>
                <ns1:amountPaid>14.00</ns1:amountPaid>
            </ns1:invoice>
        </ns1:invoices>
        <ns1:paymentMethodInformation>
            <ns1:cardType>VISA</ns1:cardType>
            <ns1:cardName>************1111</ns1:cardName>
            <ns1:cardHolder>Juan Pérez</ns1:cardHolder>
            <ns1:authorizationCode>dd92c0f864ea9ff1016501e880d03451</ns1:authorizationCode>
        </ns1:paymentMethodInformation>
    </ns1:report>
</ns1:reportRequest>
XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:ns1="http://v1.dto.reports.rolandopalermo.com">
    <xsl:template match="/">
        <xsl:apply-templates
            select="ns1:reportRequest/ns1:report" />
    </xsl:template>
    <xsl:template match="*">
        <xsl:element name="{local-name(.)}">
            <xsl:apply-templates />
        </xsl:element>
    </xsl:template>
    <xsl:template match="@*">
        <xsl:copy />
    </xsl:template>
</xsl:stylesheet>
XML Output
<?xml version="1.0" encoding="UTF-8"?>
<report>
    <reportId>1</reportId>
    <header>
        <id>999999999</id>
        <name>John Smith</name>
    </header>
    <invoices>
        <invoice>
            <invoiceNumber>0000000000</invoiceNumber>
            <dueDate>07/01/18</dueDate>
            <invoiceAmount>14.00</invoiceAmount>
            <amountPaid>14.00</amountPaid>
        </invoice>
    </invoices>
    <paymentMethodInformation>
        <cardType>VISA</cardType>
        <cardName>************1111</cardName>
        <cardHolder>Juan Pérez</cardHolder>
        <authorizationCode>dd92c0f864ea9ff1016501e880d03451</authorizationCode>
    </paymentMethodInformation>
</report>

No hay comentarios.:

Publicar un comentario