[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Converting XML to CSV - how to eliminate output white

Subject: Converting XML to CSV - how to eliminate output whitespace and blank lines?
From: "ohaya ohaya@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 6 Nov 2023 02:13:22 -0000
 Converting XML to CSV - how to eliminate output  white
Hi,

I am trying to use XSLT to convert an XML file to a CSV, where the CSV has the node names+node values.

Thus far I have this XSLT (note:  I can only use XSLT 1.0 because I am going be using xsltproc):

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--    <xsl:output method="html" /> -->
<xsl:output method="text" encoding="UTF-8" disable-output-escaping="YES"/>

<xsl:template match="*[not(*)]">
    <xsl:value-of select="local-name()"/>
    <xsl:text>&#44;</xsl:text>
    <xsl:value-of select="."/>
</xsl:template>

</xsl:stylesheet>

Here's an example XML file:

<record>
        <rule>
                <customer>1234</customer>
                <schemaName>schema_name</schemaName>
                <schemaVersion>1.0</schemaVersion>
        </rule>
        <identifier>
                <PN_ID>1111111111</PN_ID>
        </identifier>
        <adrRecord>
                <PN_ID>1111111</PN_ID>
                <personnel>
                        <ADM_ORG_CD>urn:NORM:DEPT</ADM_ORG_CD>
                </personnel>
                <enterpriseUser>
                        <entUserRoles>a:b:c</entUserRoles>
                </enterpriseUser>
        </adrRecord>
</record>

and here's the output when I run the XSLT using xsltproc:

===========================================


                customer,1234
                schemaName,schema_name
                schemaVersion,1.0


                PN_ID,1111111111


                PN_ID,1111111

                        ADM_ORG_CD,urn:NORM:DEPT


                        entUserRoles,a:b:c
=============================================

Notice the whitespace in front of each line and also the blank lines between sub-nodes, e.g., so the output would be just:

=============================================
customer,1234
schemaName,schema_name
schemaVersion,1.0
PN_ID,1111111111
PN_ID,1111111
ADM_ORG_CD,urn:NORM:DEPT
entUserRoles,a:b:c
=============================================

Is it possible to remove/suppress both the whitespace and the blank lines using XSLT?

I am currently having to pipe the output through a couple of SEDs, and I'd prefer not to have to do that.

Thanks in advance!!

Jim

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.