xmlms-accessxslttransformationexport-to-xml

XSL transform MS Access 2007 output


I am exporting data from Access on regular basis. So far I used to export it and manually edit some tags to adjust it to Clients needs. Lately I found that that there is posibility to use XSL as a transformation pattern.

I am still beginner when comes to XSL but managed to create something like this:

<?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="dataroot/Kwerenda_x0020_Nota_x0020_Kredytowa">
    <xsl:element name="Faktury_od_nas">
        <xsl:apply-templates />
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

Generally it works ok but:
1. Normally when I export data it gives me every tag in a new line, now it is divided only by tag I changed.
2. I do not know how to rename dataroot. I tried to copy/paste same code but then I get dataroot everytime Faktury_od_nas appears...

Transformed sample data:

<dataroot generated="2016-01-12T13:54:11" xmlns:od="urn:schemas-microsoft-com:officedata"><Faktury_od_nas><No>1</No><InvoiceDate>20150715</InvoiceDate><InvoiceNumber>12345</InvoiceNumber><CustVATNumber>LT100004645417</CustVATNumber><E100customerKey>65-92</E100customerKey><CustomerName>Client_name</CustomerName><InvoiceCountry>BE</InvoiceCountry><VATpersent>21</VATpersent><VATBasis>106,36</VATBasis><VATamount>22,34</VATamount><Currency>EUR</Currency><VAT_x0020_recovery_x0020_fee_x0020_rate_x0020__x0028__x0025__x0029_>7.5</VAT_x0020_recovery_x0020_fee_x0020_rate_x0020__x0028__x0025__x0029_><Service_x0020_Type>Express</Service_x0020_Type><InvoiceScanFileName>scan_name</InvoiceScanFileName>
    </Faktury_od_nas></dararoot>

Desired sample data:

<Faktura>
<Faktury_od_nas>
<No>1</No>
<InvoiceDate>20150715</InvoiceDate>
<InvoiceNumber>12345</InvoiceNumber>
<CustVATNumber>LT100004645417</CustVATNumber>
<E100customerKey>65-92</E100customerKey>
<CustomerName>Client_name</CustomerName>
<InvoiceCountry>BE</InvoiceCountry>
<VATpersent>21</VATpersent>
<VATBasis>106,36</VATBasis>
<VATamount>22,34</VATamount>
<Currency>EUR</Currency>
<VAT_x0020_recovery_x0020_fee_x0020_rate_x0020__x0028__x0025__x0029_>7.5</VAT_x0020_recovery_x0020_fee_x0020_rate_x0020__x0028__x0025__x0029_>
<Service_x0020_Type>Express</Service_x0020_Type>
<InvoiceScanFileName>scan_name</InvoiceScanFileName>
</Faktury_od_nas>
</Faktura>

I appreciate any help.

Edit:

<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:template match="*">
    <xsl:element name="{local-name()}">
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>

<xsl:template match="dataroot">
    <Faktura>
        <xsl:apply-templates />
    </Faktura>
</xsl:template>

<xsl:template match="Kwerenda_x0020_Nota_x0020_Kredytowa">
    <Faktury_od_nas>
        <xsl:apply-templates />
    </Faktury_od_nas>
</xsl:template>

</xsl:stylesheet>

Solution

  • It would help seeing your source XML, but I believe you could use:

    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="*"/>
    
    <xsl:template match="*">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>
    
    <xsl:template match="dataroot">
        <Faktura>
            <xsl:apply-templates />
        </Faktura>
    </xsl:template>
    
    <xsl:template match="Kwerenda_x0020_Nota_x0020_Kredytowa">
        <Faktury_od_nas>
            <xsl:apply-templates />
        </Faktury_od_nas>
    </xsl:template>
    
    </xsl:stylesheet>