0

I have a XML document and I need pass it to CSV with XSL. I managed to perform this process, but I have problems with special characters, ie "& quot;, & amp; ... I need you to make the change these characters are not changed and remain represented in the same way. I have the following:

XML file

<?xml version="1.0" encoding="UTF-8"?>
<pwlist>
<pwentry>
    <group>General</group>
    <title>Sample Entry</title>
    <username>Greg</username>
    <url>http://www.web.com</url>
    <password>sVoVd2HohmC7hpKYV5Bs</password>
    <notes>This entry is stored in the &#39;General&#39; group.</notes>
    <uuid>4d9a9420ac7c4a8ae688762eac8871a9</uuid>
    <image>0</image>
    <creationtime>2006-12-31T11:52:01</creationtime>
    <lastmodtime>2006-12-31T11:52:01</lastmodtime>
    <lastaccesstime>2006-12-31T11:52:01</lastaccesstime>
    <expiretime expires="false">2999-12-28T23:59:59</expiretime>
</pwentry>
<pwentry>
    <group tree="General">Windows</group>
    <title>Sample Entry #2</title>
    <username>michael@web.com</username>
    <url>http://www.web.com</url>
    <password>L2shNNvQLmOWug68Rz2V</password>
    <notes>This entry is stored in the &#39;Windows&#39; subgroup of the &#39;General&#39; group.</notes>
    <uuid>bddda53fad29037bba34e7e42923c676</uuid>
    <image>0</image>
    <creationtime>2006-12-31T11:52:38</creationtime>
    <lastmodtime>2006-12-31T11:52:38</lastmodtime>
    <lastaccesstime>2006-12-31T11:52:38</lastaccesstime>
    <expiretime expires="false">2999-12-28T23:59:59</expiretime>
</pwentry>
<pwentry>
    <group tree="General">Windows</group>
    <title>Expiring Entry</title>
    <username>me@website.org</username>
    <url>http://www.website.org</url>
    <password>USYbBOdTjaerrN/3l0VK</password>
    <notes>This entry expires on 28.12.2008, at 23:59:59.</notes>
    <uuid>c2674112d67f2787e822d845cde52858</uuid>
    <image>0</image>
    <creationtime>2006-12-31T11:53:38</creationtime>
    <lastmodtime>2006-12-31T11:53:38</lastmodtime>
    <lastaccesstime>2006-12-31T11:53:38</lastaccesstime>
    <expiretime expires="true">2008-12-28T23:59:59</expiretime>
</pwentry>
<pwentry>
    <group tree="General">Windows</group>
    <title>Special Characters Test</title>
    <username>!&quot;§$%&amp;/()=?`´²³{[]}\</username>
    <url></url>
    <password>öüäÖÜÄß&lt;&gt;@€µ®“«</password>
    <notes>The user name and password fields contain special characters.</notes>
    <uuid>6a4aae3134cb7b7d550d0bb7c98bc203</uuid>
    <image>34</image>
    <creationtime>2006-12-31T11:55:57</creationtime>
    <lastmodtime>2006-12-31T11:56:06</lastmodtime>
    <lastaccesstime>2006-12-31T11:56:06</lastaccesstime>
    <expiretime expires="true">2008-12-28T23:59:59</expiretime>
</pwentry>
<pwentry>
    <group tree="General">Windows</group>
    <title>Multi-Line Test</title>
    <username>user@website.com</username>
    <url>http://www.website.com</url>
    <password>v9ffIWkd/jPw/GPLFViW</password>
    <notes>This is a multi-line comment.
This is a multi-line comment.
This is a multi-line comment.
This is a multi-line comment.
This is a multi-line comment.
This is a multi-line comment.</notes>
    <uuid>3d4fe2dc30d4ef0b62da6c7bf85e04ba</uuid>
    <image>0</image>
    <creationtime>2006-12-31T11:56:49</creationtime>
    <lastmodtime>2006-12-31T11:56:49</lastmodtime>
    <lastaccesstime>2006-12-31T11:56:49</lastaccesstime>
    <expiretime expires="false">2999-12-28T23:59:59</expiretime>
</pwentry>
</pwlist>

XSL file:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>

    <xsl:variable name="delimiter" select="','" />

    <xsl:variable name="fieldArray" >
        <field>group</field>
        <field>title</field>
        <field>username</field>
        <field>url</field>
        <field>password</field>
        <field>notes</field>
        <field>uuid</field>
        <field>image</field>
        <field>creationtime</field>
        <field>lastmodtime</field>
        <field>lastaccesstime</field>
        <field>expiretime</field>
    </xsl:variable>
    <xsl:param name="fields" select="document('')/*/xsl:variable[@name='fieldArray']/*"/>

    <xsl:template match="/">

        <xsl:for-each select="$fields">
            <xsl:if test="position() !=1">
                <xsl:value-of select="$delimiter"/>
            </xsl:if>
            <xsl:value-of select="." />
        </xsl:for-each>

        <xsl:text>&#xa;</xsl:text>

        <xsl:apply-templates select="pwlist/pwentry"/>
    </xsl:template>

    <xsl:template match="pwentry">
        <xsl:variable name="currNode" select="." />

        <xsl:for-each select="$fields">
            <xsl:if test="position() !=1">
                <xsl:value-of select="$delimiter"/>
            </xsl:if>
        <xsl:text>"</xsl:text>
            <xsl:value-of select="$currNode/*[name() = current()]" />
        <xsl:text>"</xsl:text>
    </xsl:for-each>

    <xsl:text>&#xa;</xsl:text>
    </xsl:template>
</xsl:stylesheet>

I tried disable-output-escaping="no" but doesn't work, can someone help me? Thanks!

Joe
  • 1
  • 1
  • 3
    **1.** Please minimize your example to only what's necessary to demonstrate the problem, and show the expected result. --- **2.** Which processor are you using? Your stylesheet is tagged as `version="1.0"`, but `byte-order-mark` is only allowed in XSLT 2.0. – michael.hor257k Nov 29 '15 at 00:39
  • Hi Michael, I am using the xsltproc process in linux terminal. With that I successfully recover the data using xml and xsl to csv. The problem is basically the transformation to .csv, in process the special characters such as " $#39; >, < ... are represented as ", >, <, ', etc. On the byte-order-mark is only there because I was doing some tests and I forgot to remove. So, the transformation works correctly but I can't get special characters remain as they are in the XML without being processed in the resulting CSV. – Joe Nov 29 '15 at 07:58
  • 1
    Why is this a problem? If you are outputting CSV, all you need to do is escape the `"`quotes as `""`, as shown here: http://stackoverflow.com/a/25003101/3016153 – michael.hor257k Nov 29 '15 at 09:08
  • Because I need to import the resulting CSV in a program, such as special characters are not imported as in the xml, this can bring problems of interpretation of the file. So I need that CSV contains the same special characters that in XML. – Joe Nov 29 '15 at 12:42
  • 1
    XSLT does not see them as special characters. XSLT works on parsed data, and cannot tell the difference between `"` and `"` in the input. If you have a list of "special characters" that need to be escaped on output, you will have to use a recursive named template (or several such templates) to produce the escaping sequence - see an example here: http://stackoverflow.com/questions/27631190/xsl-replace-single-and-double-quotes-with-apos-and-quot/27633138#27633138 – michael.hor257k Nov 29 '15 at 13:09
  • How would the program know that you are using XML-like character references? That's not typical for CSV. There is scarcely a spec for CSV so interpretation problems are common—especially if the CSV document is not accompanied by its custom spec. Can you use a format that is more standard, for example SpreadsheetML, Open XML Spreadsheet, OpenDocument Spreadsheet, ADO .NET DataSet, JSON, OData JSON, …? No matter how you cut it, you can't convert from XML to just CSV, it has to be CSV+specification. – Tom Blodget Nov 29 '15 at 22:33

0 Answers0